Skip to content

Commit

Permalink
compiled regex
Browse files Browse the repository at this point in the history
  • Loading branch information
pauld-msft committed Aug 19, 2024
1 parent d602cff commit 49d845e
Show file tree
Hide file tree
Showing 2 changed files with 322 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ internal class PipReportUtilities
private const string ClassifierFieldSeparator = " :: ";
private const string ClassifierFieldLicensePrefix = "License";

// Python regular expression for version schema:
// https://www.python.org/dev/peps/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
public static readonly Regex CanonicalVersionPatternMatch = new Regex(
@"^([1-9]\d*!)?(0|[1-9]\d*)(\.(0|[1-9]\d*))*((a|b|rc)(0|[1-9]\d*))?(\.post(0|[1-9]\d*))?(\.dev(0|[1-9]\d*))?(\+(?:(?<local>[a-z0-9]+(?:[.][a-z0-9]+)*))?)?$",
RegexOptions.Compiled);

/// <summary>
/// Normalize the package name format to the standard Python Packaging format.
/// See https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization.
Expand Down Expand Up @@ -71,9 +77,6 @@ public static string GetLicenseFromInstalledItem(PipInstallationReportItem compo

public static bool IsCanonicalVersion(string version)
{
// python regular expression for version schema: https://www.python.org/dev/peps/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
var canonicalVersionPattern = @"^([1-9]\d*!)?(0|[1-9]\d*)(\.(0|[1-9]\d*))*((a|b|rc)(0|[1-9]\d*))?(\.post(0|[1-9]\d*))?(\.dev(0|[1-9]\d*))?(\+(?:(?<local>[a-z0-9]+(?:[.][a-z0-9]+)*))?)?$";

return Regex.IsMatch(version, canonicalVersionPattern);
return CanonicalVersionPatternMatch.Match(version).Success;
}
}
Loading

0 comments on commit 49d845e

Please sign in to comment.