Skip to content

Commit

Permalink
[C#] Replace regex that validates descriptor names (#12174)
Browse files Browse the repository at this point in the history
This PR replaces the descriptor name validation regex with a validation method. This change allows the `System.Text.RegularExpressions` engine to be trimmed away in published apps that do standard protobuf serialization.

There are some other usages of `Regex` in Google.Protobuf, but they in `JsonParser`. They are only included in a published app if `JsonParser` is used.

Another benefit is a slightly faster app startup time. The removed regex was compiled, which has a high-ish fixed cost.

cc @jskeet @jtattermusch

Closes #12174

COPYBARA_INTEGRATE_REVIEW=#12174 from JamesNK:jamesnk/remove-regex 9d065a3
PiperOrigin-RevId: 532210203
  • Loading branch information
JamesNK authored and copybara-github committed May 15, 2023
1 parent 7137592 commit cfb702a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
24 changes: 17 additions & 7 deletions csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Google.Protobuf.Reflection
{
Expand Down Expand Up @@ -176,8 +175,6 @@ internal void AddSymbol(IDescriptor descriptor)
descriptorsByName[fullName] = descriptor;
}

private static readonly Regex ValidationRegex = new Regex("^[_A-Za-z][_A-Za-z0-9]*$", FrameworkPortability.CompiledRegexWhereAvailable);

/// <summary>
/// Verifies that the descriptor's name is valid (i.e. it contains
/// only letters, digits and underscores, and does not start with a digit).
Expand All @@ -189,11 +186,24 @@ private static void ValidateSymbolName(IDescriptor descriptor)
{
throw new DescriptorValidationException(descriptor, "Missing name.");
}
if (!ValidationRegex.IsMatch(descriptor.Name))
{
throw new DescriptorValidationException(descriptor,
"\"" + descriptor.Name + "\" is not a valid identifier.");

// Symbol name must start with a letter or underscore, and it can contain letters,
// numbers and underscores.
string name = descriptor.Name;
if (!IsAsciiLetter(name[0]) && name[0] != '_') {
ThrowInvalidSymbolNameException(descriptor);
}
for (int i = 1; i < name.Length; i++) {
if (!IsAsciiLetter(name[i]) && !IsAsciiDigit(name[i]) && name[i] != '_') {
ThrowInvalidSymbolNameException(descriptor);
}
}

static bool IsAsciiLetter(char c) => (uint)((c | 0x20) - 'a') <= 'z' - 'a';
static bool IsAsciiDigit(char c) => (uint)(c - '0') <= '9' - '0';
static void ThrowInvalidSymbolNameException(IDescriptor descriptor) =>
throw new DescriptorValidationException(
descriptor, "\"" + descriptor.Name + "\" is not a valid identifier.");
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion objectivec/GPBAny.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfb702a

Please sign in to comment.