diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs index 5128ad9a6a83..3956b55e353d 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs @@ -33,7 +33,6 @@ using System; using System.Collections.Generic; using System.Text; -using System.Text.RegularExpressions; namespace Google.Protobuf.Reflection { @@ -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); - /// /// Verifies that the descriptor's name is valid (i.e. it contains /// only letters, digits and underscores, and does not start with a digit). @@ -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."); } /// diff --git a/objectivec/GPBAny.pbobjc.h b/objectivec/GPBAny.pbobjc.h index f4efbb23be2c..c0d389f0399c 100644 --- a/objectivec/GPBAny.pbobjc.h +++ b/objectivec/GPBAny.pbobjc.h @@ -157,7 +157,8 @@ GPB_FINAL @interface GPBAny : GPBMessage * * Note: this functionality is not currently available in the official * protobuf release, and it is not used for type URLs beginning with - * type.googleapis.com. + * type.googleapis.com. As of May 2023, there are no widely used type server + * implementations and no plans to implement one. * * Schemes other than `http`, `https` (or the empty scheme) might be * used with implementation specific semantics.