Skip to content

Commit

Permalink
Simplify proto2::(anonymous namespace)::FlatAllocatorImpl::GetFieldNa…
Browse files Browse the repository at this point in the history
…meCase and reuse absl's ascii lower/uppercasing.

By just checking for upper-cased characters, rather than digits and lower-cased characters, we have to perform fewer comparisons.
This should be safe because absl::AsciiStrToLower only operates on upper-case characters.

PiperOrigin-RevId: 688453016
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Oct 22, 2024
1 parent 2ac862f commit f549fc3
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,21 +552,17 @@ class FlatAllocatorImpl {
return pointers_.template Get<char>() != nullptr;
}

static bool IsLower(char c) { return 'a' <= c && c <= 'z'; }
static bool IsDigit(char c) { return '0' <= c && c <= '9'; }
static bool IsLowerOrDigit(char c) { return IsLower(c) || IsDigit(c); }

enum class FieldNameCase { kAllLower, kSnakeCase, kOther };
FieldNameCase GetFieldNameCase(const absl::string_view name) {
if (!name.empty() && !IsLower(name[0])) return FieldNameCase::kOther;
if (!name.empty() && !absl::ascii_islower(name[0])) {
return FieldNameCase::kOther;
}
FieldNameCase best = FieldNameCase::kAllLower;
for (char c : name) {
if (IsLowerOrDigit(c)) {
// nothing to do
if (absl::ascii_isupper(c)) {
return FieldNameCase::kOther;
} else if (c == '_') {
best = FieldNameCase::kSnakeCase;
} else {
return FieldNameCase::kOther;
}
}
return best;
Expand Down

0 comments on commit f549fc3

Please sign in to comment.