Skip to content

Commit

Permalink
chore: use compiled regex (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: Sajay Antony <[email protected]>
  • Loading branch information
sajayantony authored Dec 19, 2023
1 parent 52fcd19 commit cb20f94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Oras/Content/DigestUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ internal static class DigestUtility
/// <summary>
/// digestRegexp checks the digest.
/// </summary>
private const string digestRegexp = @"[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+";
private const string digestRegexPattern = @"[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+";
static Regex digestRegex = new Regex(digestRegexPattern, RegexOptions.Compiled);

/// <summary>
/// ParseDigest verifies the digest header and throws an exception if it is invalid.
/// </summary>
/// <param name="digest"></param>
internal static string ParseDigest(string digest)
{
if (!Regex.IsMatch(digest, digestRegexp))
if (!digestRegex.IsMatch(digest))
{
throw new InvalidDigestException($"Invalid digest: {digest}");
}
Expand Down
13 changes: 9 additions & 4 deletions Oras/Remote/RemoteReference.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Oras.Content;
using Oras.Exceptions;
using System;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;

namespace Oras.Remote
Expand Down Expand Up @@ -36,14 +37,18 @@ public class RemoteReference
/// - https://github.com/distribution/distribution/blob/v2.7.1/reference/regexp.go#L53
/// - https://github.com/opencontainers/distribution-spec/blob/v1.0.1/spec.md#pulling-manifests
/// </summary>
private const string repositoryRegexp = @"^[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)*$";
private const string repositoryRegexPattern = @"^[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)*$";

private static Regex repositoryRegex = new Regex(repositoryRegexPattern, RegexOptions.Compiled);

/// <summary>
/// tagRegexp checks the tag name.
/// The docker and OCI spec have the same regular expression.
/// Reference: https://github.com/opencontainers/distribution-spec/blob/v1.0.1/spec.md#pulling-manifests
/// </summary>
private const string tagRegexp = @"^[\w][\w.-]{0,127}$";
private const string tagRegexPattern = @"^[\w][\w.-]{0,127}$";

private static Regex tagRegex = new Regex(tagRegexPattern, RegexOptions.Compiled);

public static RemoteReference ParseReference(string artifact)
{
Expand Down Expand Up @@ -124,7 +129,7 @@ public void ValidateReferenceAsDigest()
/// <returns></returns>
public void ValidateRepository()
{
if (!Regex.IsMatch(Repository, repositoryRegexp))
if (!repositoryRegex.IsMatch(Repository))
{
throw new InvalidReferenceException("Invalid Respository");
}
Expand All @@ -146,7 +151,7 @@ public void ValidateRegistry()

public void ValidateReferenceAsTag()
{
if (!Regex.IsMatch(Reference, tagRegexp))
if (!tagRegex.IsMatch(Reference))
{
throw new InvalidReferenceException("Invalid Tag");
}
Expand Down

0 comments on commit cb20f94

Please sign in to comment.