Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored and deannagarcia committed May 15, 2023
1 parent b29af4e commit 9d065a3
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,35 +187,24 @@ private static void ValidateSymbolName(IDescriptor descriptor)
throw new DescriptorValidationException(descriptor, "Missing name.");
}

if (!IsValidSymbolNameFormat(descriptor.Name))
{
throw new DescriptorValidationException(descriptor,
"\"" + descriptor.Name + "\" is not a valid identifier.");
}
}

/// <summary>
/// Symbol name must start with a letter or underscore, and it can contain letters, numbers and underscores.
/// </summary>
private static bool IsValidSymbolNameFormat(string name)
{
// 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] != '_')
{
return false;
ThrowInvalidSymbolNameException(descriptor);
}

for (int i = 1; i < name.Length; i++)
{
if (!IsAsciiLetter(name[i]) && !IsAsciiDigit(name[i]) && name[i] != '_')
{
return false;
ThrowInvalidSymbolNameException(descriptor);
}
}

return true;

static bool IsAsciiLetter(char c) => (uint) ((c | 0x20) - 'a') <= 'z' - 'a';
static bool IsAsciiDigit(char c) => (uint) (c - '0') <= ('9' - '0');
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

0 comments on commit 9d065a3

Please sign in to comment.