diff --git a/Axuno.Tools/GeoSpatial/Latitude.cs b/Axuno.Tools/GeoSpatial/Latitude.cs index 0c71719..9ce5d3d 100644 --- a/Axuno.Tools/GeoSpatial/Latitude.cs +++ b/Axuno.Tools/GeoSpatial/Latitude.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System; +using System.Globalization; namespace Axuno.Tools.GeoSpatial; diff --git a/Axuno.Tools/GeoSpatial/Location.Parser.cs b/Axuno.Tools/GeoSpatial/Location.Parser.cs index d32770f..c30db78 100644 --- a/Axuno.Tools/GeoSpatial/Location.Parser.cs +++ b/Axuno.Tools/GeoSpatial/Location.Parser.cs @@ -8,6 +8,9 @@ namespace Axuno.Tools.GeoSpatial; /// public partial class Location { + // Default timeout value for the regular expressions + private static int _regExTimeout = 1000; + /// Parsing latitude and longitude information. /// /// User input: @@ -83,15 +86,15 @@ private static class Parser private const RegexOptions Options = RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase; - private static readonly Regex DegreeRegex = new(DegreePattern, Options); + private static readonly Regex DegreeRegex = new(DegreePattern, Options, TimeSpan.FromMilliseconds(_regExTimeout)); - private static readonly Regex DegreeMinuteRegex = new(DegreeMinutePattern, Options); + private static readonly Regex DegreeMinuteRegex = new(DegreeMinutePattern, Options, TimeSpan.FromMilliseconds(_regExTimeout)); - private static readonly Regex DegreeMinuteSecondRegex = new(DegreeMinuteSecondPattern, Options); + private static readonly Regex DegreeMinuteSecondRegex = new(DegreeMinuteSecondPattern, Options, TimeSpan.FromMilliseconds(_regExTimeout)); private static readonly Regex IsoRegex = new(IsoPattern, - RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace); + RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(_regExTimeout)); private static Location? CreateLocation(Angle? latitude, Angle? longitude, double? altitude) { @@ -216,7 +219,7 @@ private static double MakeSameSign(double source, double value) } /// - /// Parses the input string for a value containg a pair of degree + /// Parses the input string for a value containing a pair of degree /// minute second values. /// /// The input to parse. @@ -278,13 +281,13 @@ private static double MakeSameSign(double source, double value) break; case 3: // sign + MM : value is degrees and minutes angle = Angle.FromDegrees( - int.Parse(value.Substring(1, degreeDigits), CultureInfo.InvariantCulture), + int.Parse(value.AsSpan(1, degreeDigits), CultureInfo.InvariantCulture), double.Parse(value[(degreeDigits + 1)..], CultureInfo.InvariantCulture)); break; case 5: // sign + MM + SS : value is degrees, minutes and seconds angle = Angle.FromDegrees( - int.Parse(value.Substring(1, degreeDigits), CultureInfo.InvariantCulture), - int.Parse(value.Substring(degreeDigits + 1, 2), CultureInfo.InvariantCulture), + int.Parse(value.AsSpan(1, degreeDigits), CultureInfo.InvariantCulture), + int.Parse(value.AsSpan(degreeDigits + 1, 2), CultureInfo.InvariantCulture), double.Parse(value[(degreeDigits + 3)..], CultureInfo.InvariantCulture)); break; default: @@ -301,7 +304,7 @@ private static bool TryGetValue(Match match, string groupName, out string? resul var group = match.Groups[groupName]; // Need to check that only a single capture occurred, as the suffixes are used more than once - if (group.Success && group.Captures.Count == 1) + if (group is { Success: true, Captures.Count: 1 }) { result = group.Value; return true; diff --git a/Axuno.Tools/GeoSpatial/Location.cs b/Axuno.Tools/GeoSpatial/Location.cs index 8549931..09db794 100644 --- a/Axuno.Tools/GeoSpatial/Location.cs +++ b/Axuno.Tools/GeoSpatial/Location.cs @@ -202,7 +202,7 @@ public Angle Azimuth(Location point) /// /// Calculates the great circle distance, in meters, between this instance - /// and the specified value. + /// and the specified value, using the Haversine formula. /// /// The location of the other point. /// The great circle distance, in meters. diff --git a/Axuno.Tools/GeoSpatial/LocationCollection.cs b/Axuno.Tools/GeoSpatial/LocationCollection.cs index a24312b..d44680e 100644 --- a/Axuno.Tools/GeoSpatial/LocationCollection.cs +++ b/Axuno.Tools/GeoSpatial/LocationCollection.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Globalization; using System.Text; using System.Xml; diff --git a/Axuno.Tools/GeoSpatial/LocationStyles.cs b/Axuno.Tools/GeoSpatial/LocationStyles.cs index a3d6eaf..e4e549b 100644 --- a/Axuno.Tools/GeoSpatial/LocationStyles.cs +++ b/Axuno.Tools/GeoSpatial/LocationStyles.cs @@ -36,4 +36,4 @@ public enum LocationStyles /// and Iso styles are used. /// Any = Degrees | DegreesMinutes | DegreesMinutesSeconds | Iso -} \ No newline at end of file +} diff --git a/Axuno.Tools/GeoSpatial/MaidenheadLocator.cs b/Axuno.Tools/GeoSpatial/MaidenheadLocator.cs index c55eb44..85cf62f 100644 --- a/Axuno.Tools/GeoSpatial/MaidenheadLocator.cs +++ b/Axuno.Tools/GeoSpatial/MaidenheadLocator.cs @@ -29,6 +29,9 @@ namespace Axuno.Tools.GeoSpatial; /// public class MaidenheadLocator { + // Default timeout for regular expressions + private static int _regExTimeout = 1000; + /// /// Checks whether a string is a valid Maidenhead locator /// @@ -36,7 +39,7 @@ public class MaidenheadLocator /// Return true, if the string is a valid Maidenhead locator public static bool IsLocator(string locator) { - return Regex.IsMatch(locator, "^[A-R]{2}[0-9]{2}[A-X]{2}$"); + return Regex.IsMatch(locator, "^[A-R]{2}[0-9]{2}[A-X]{2}$", RegexOptions.None, TimeSpan.FromMicroseconds(_regExTimeout)); } /// @@ -95,4 +98,4 @@ public static Location LocatorToLocation(string locator) return location; } -} \ No newline at end of file +}