Skip to content

Commit

Permalink
Merge pull request #1663 from qdraw/feature/202408-GeoParser-timeout
Browse files Browse the repository at this point in the history
fix timeout
  • Loading branch information
qdraw authored Aug 22, 2024
2 parents 907c77f + ef7c00b commit 2c56b88
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions starsky/starsky.foundation.readmeta/Helpers/GeoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

namespace starsky.foundation.readmeta.Helpers
{
internal static partial class GeoParserInvalidCharactersRegex
{
/// <summary>
/// Generated regex for removing invalid characters
/// </summary>
/// <returns></returns>
[GeneratedRegex("[^0-9\\., ]",
RegexOptions.CultureInvariant,
matchTimeoutMilliseconds: 1000)]
public static partial Regex GetRegex();
}

public static class GeoParser
{

Expand All @@ -19,12 +31,11 @@ public static double ConvertDegreeMinutesSecondsToDouble(string point, string re
//Example: 17.21.18S
// DD°MM’SS.s” usage

var multiplier = ( refGps.Contains('S') || refGps.Contains('W') ) ? -1 : 1; //handle south and west

point = Regex.Replace(point, "[^0-9\\., ]", "", RegexOptions.CultureInvariant,
TimeSpan.FromMilliseconds(100)); //remove the characters
var multiplier = ( refGps.Contains('S') || refGps.Contains('W') ) ? -1 : 1; // handle south and west

point = GeoParserInvalidCharactersRegex.GetRegex().Replace(point, ""); // remove the characters

// When you use an localisation where commas are used instead of a dot
// When you use a localisation where commas are used instead of a dot
point = point.Replace(",", ".");

var pointArray = point.Split(' '); //split the string.
Expand Down Expand Up @@ -53,8 +64,7 @@ public static double ConvertDegreeMinutesToDouble(string point, string refGps)
var multiplier = ( refGps.Contains('S') || refGps.Contains('W') ) ? -1 : 1; // handle south and west

point = point.Replace(",", " ");
point = Regex.Replace(point, "[^0-9\\., ]", "", RegexOptions.CultureInvariant,
TimeSpan.FromMilliseconds(100)); //remove the characters
point = GeoParserInvalidCharactersRegex.GetRegex().Replace(point, ""); // remove the characters

var pointArray = point.Split(' '); //split the string.
var degrees = double.Parse(pointArray[0], CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -98,7 +108,7 @@ public static GeoListItem ParseIsoString(string isoStr)
isoStr = isoStr.Remove(isoStr.Length - 1); // Remove trailing slash

var parts = isoStr.Split(Separator, StringSplitOptions.None);
if ( parts.Length < 3 || parts.Length > 4 )
if ( parts.Length is < 3 or > 4 )
{
// Check for parts count
return geoListItem;
Expand Down

0 comments on commit 2c56b88

Please sign in to comment.