Skip to content

Commit

Permalink
#25: Where possible, use generated Regexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsagara committed Mar 9, 2024
1 parent 4b9707e commit 2674a50
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/IdParser.Core/Barcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record BarcodeParseResult(
IdentificationCard Card
);

public static class Barcode
public static partial class Barcode
{
/// <summary>
/// The text should begin with an '@' character (ASCII Decimal 64, Hex 0x40).
Expand Down Expand Up @@ -265,7 +265,8 @@ private static IdentificationCard GetIdCardInstance(string rawPdf417Input, AAMVA
}


private static readonly Regex _rxSubfile = new Regex("(DL|ID)([\\d\\w]{3,8})(DL|ID|Z\\w)([DZ][A-Z]{2})", RegexOptions.Compiled);
[GeneratedRegex(@"(DL|ID)([\d\w]{3,8})(DL|ID|Z\w)([DZ][A-Z]{2})")]
private static partial Regex SubfileRegex();

/// <summary>
/// Get the index of the subfile starting position.
Expand Down Expand Up @@ -320,7 +321,7 @@ private static int ParseSubfileOffset(string rawPdf417Input, AAMVAVersion versio
{
// Some jurisdictions, like Ontario, have a zero offset, which is incorrect.
// Set the offset to the start of the subfile type indicator.
var match = _rxSubfile.Match(rawPdf417Input);
var match = SubfileRegex().Match(rawPdf417Input);

if (match.Success)
{
Expand Down
8 changes: 5 additions & 3 deletions src/IdParser.Core/Parsers/Id/PostalCodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace IdParser.Core.Parsers.Id;

internal static class PostalCodeParser
internal static partial class PostalCodeParser
{
private static readonly Regex _rxNonAlphaNumeric = new Regex(@"[^\w\d]", RegexOptions.Compiled);
[GeneratedRegex(@"[^\w\d]")]
private static partial Regex NonAlphaNumericRegex();


internal static Field<string?> Parse(string elementId, string? rawValue)
{
Expand All @@ -26,7 +28,7 @@ internal static class PostalCodeParser
rawValue = rawValue.Substring(0, indexOfSpaces);
}

postalCode = _rxNonAlphaNumeric
postalCode = NonAlphaNumericRegex()
.Replace(rawValue, "")
.Replace("0000", "", StringComparison.Ordinal);
}
Expand Down
7 changes: 4 additions & 3 deletions src/IdParser.Core/Parsers/Id/WeightInPoundsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace IdParser.Core.Parsers.Id;

internal static class WeightInPoundsParser
internal static partial class WeightInPoundsParser
{
internal static Field<Weight?> Parse(string elementId, string? rawValue)
{
Expand All @@ -27,7 +27,8 @@ internal static class WeightInPoundsParser
}


private static readonly Regex _rxMetricWeight = new Regex("(?<Weight>\\d+)+\\s*KG", RegexOptions.Compiled);
[GeneratedRegex(@"(?<Weight>\d+)+\s*KG")]
private static partial Regex MetricWeightRegex();

/// <summary>
/// Alberta put the weight in kilograms in the weight in pounds parser ¯\_(ツ)_/¯
Expand All @@ -36,7 +37,7 @@ private static bool TryParseMetric(string input, [NotNullWhen(true)] out Weight?
{
weight = null;

var match = _rxMetricWeight.Match(input);
var match = MetricWeightRegex().Match(input);

if (match.Success)
{
Expand Down

0 comments on commit 2674a50

Please sign in to comment.