Refactors checker.h to use if statements instead of ternary operators for better readability.

PiperOrigin-RevId: 471621696
Change-Id: I603e5707d896deef3a015c70ceac9778e360f72f
pull/1269/head
Abseil Team 3 years ago committed by Copybara-Service
parent 30e43220e4
commit c0b0f63fc5
  1. 125
      absl/strings/internal/str_format/checker.h

@ -82,9 +82,11 @@ constexpr string_view ConsumeFront(string_view str, size_t len = 1) {
} }
constexpr string_view ConsumeAnyOf(string_view format, const char* chars) { constexpr string_view ConsumeAnyOf(string_view format, const char* chars) {
return ContainsChar(chars, GetChar(format, 0)) if (ContainsChar(chars, GetChar(format, 0))) {
? ConsumeAnyOf(ConsumeFront(format), chars) return ConsumeAnyOf(ConsumeFront(format), chars);
: format; } else {
return format;
}
} }
constexpr bool IsDigit(char c) { return c >= '0' && c <= '9'; } constexpr bool IsDigit(char c) { return c >= '0' && c <= '9'; }
@ -98,16 +100,21 @@ struct Integer {
// If the next character is a '$', consume it. // If the next character is a '$', consume it.
// Otherwise, make `this` an invalid positional argument. // Otherwise, make `this` an invalid positional argument.
constexpr Integer ConsumePositionalDollar() const { constexpr Integer ConsumePositionalDollar() const {
return GetChar(format, 0) == '$' ? Integer{ConsumeFront(format), value} if (GetChar(format, 0) == '$') {
: Integer{format, 0}; return Integer{ConsumeFront(format), value};
} else {
return Integer{format, 0};
}
} }
}; };
constexpr Integer ParseDigits(string_view format, int value = 0) { constexpr Integer ParseDigits(string_view format, int value = 0) {
return IsDigit(GetChar(format, 0)) if (IsDigit(GetChar(format, 0))) {
? ParseDigits(ConsumeFront(format), return ParseDigits(ConsumeFront(format),
10 * value + GetChar(format, 0) - '0') 10 * value + GetChar(format, 0) - '0');
: Integer{format, value}; } else {
return Integer{format, value};
}
} }
// Parse digits for a positional argument. // Parse digits for a positional argument.
@ -163,30 +170,36 @@ class ConvParser {
// If it is '*', we verify that it matches `args_`. `error_` is set if it // If it is '*', we verify that it matches `args_`. `error_` is set if it
// doesn't match. // doesn't match.
constexpr ConvParser ParseWidth() const { constexpr ConvParser ParseWidth() const {
return IsDigit(GetChar(format_, 0)) char first_char = GetChar(format_, 0);
? SetFormat(ParseDigits(format_).format)
: GetChar(format_, 0) == '*' if (IsDigit(first_char)) {
? is_positional_ return SetFormat(ParseDigits(format_).format);
? VerifyPositional( } else if (first_char == '*') {
ParsePositional(ConsumeFront(format_)), '*') if (is_positional_) {
: SetFormat(ConsumeFront(format_)) return VerifyPositional(ParsePositional(ConsumeFront(format_)), '*');
.ConsumeNextArg('*') } else {
: *this; return SetFormat(ConsumeFront(format_)).ConsumeNextArg('*');
}
} else {
return *this;
}
} }
// Consume the precision. // Consume the precision.
// If it is '*', we verify that it matches `args_`. `error_` is set if it // If it is '*', we verify that it matches `args_`. `error_` is set if it
// doesn't match. // doesn't match.
constexpr ConvParser ParsePrecision() const { constexpr ConvParser ParsePrecision() const {
return GetChar(format_, 0) != '.' if (GetChar(format_, 0) != '.') {
? *this return *this;
: GetChar(format_, 1) == '*' } else if (GetChar(format_, 1) == '*') {
? is_positional_ if (is_positional_) {
? VerifyPositional( return VerifyPositional(ParsePositional(ConsumeFront(format_, 2)), '*');
ParsePositional(ConsumeFront(format_, 2)), '*') } else {
: SetFormat(ConsumeFront(format_, 2)) return SetFormat(ConsumeFront(format_, 2)).ConsumeNextArg('*');
.ConsumeNextArg('*') }
: SetFormat(ParseDigits(ConsumeFront(format_)).format); } else {
return SetFormat(ParseDigits(ConsumeFront(format_)).format);
}
} }
// Consume the length characters. // Consume the length characters.
@ -197,11 +210,14 @@ class ConvParser {
// Consume the conversion character and verify that it matches `args_`. // Consume the conversion character and verify that it matches `args_`.
// `error_` is set if it doesn't match. // `error_` is set if it doesn't match.
constexpr ConvParser ParseConversion() const { constexpr ConvParser ParseConversion() const {
return is_positional_ char first_char = GetChar(format_, 0);
? VerifyPositional({ConsumeFront(format_), arg_position_},
GetChar(format_, 0)) if (is_positional_) {
: ConsumeNextArg(GetChar(format_, 0)) return VerifyPositional({ConsumeFront(format_), arg_position_},
.SetFormat(ConsumeFront(format_)); first_char);
} else {
return ConsumeNextArg(first_char).SetFormat(ConsumeFront(format_));
}
} }
constexpr ConvParser(string_view format, ConvList args, bool error, constexpr ConvParser(string_view format, ConvList args, bool error,
@ -224,8 +240,13 @@ class ConvParser {
// `format()` will be set to the character after the conversion character. // `format()` will be set to the character after the conversion character.
// `error()` will be set if any of the arguments do not match. // `error()` will be set if any of the arguments do not match.
constexpr ConvParser Run() const { constexpr ConvParser Run() const {
return (is_positional_ ? ParseArgPosition(ParsePositional(format_)) : *this) ConvParser parser = *this;
.ParseFlags()
if (is_positional_) {
parser = ParseArgPosition(ParsePositional(format_));
}
return parser.ParseFlags()
.ParseWidth() .ParseWidth()
.ParsePrecision() .ParsePrecision()
.ParseLength() .ParseLength()
@ -264,27 +285,37 @@ class FormatParser {
// This increases the limit from 512 to ~512*limit. // This increases the limit from 512 to ~512*limit.
static constexpr string_view ConsumeNonPercentInner(string_view format, static constexpr string_view ConsumeNonPercentInner(string_view format,
int limit = 20) { int limit = 20) {
return FoundPercent(format) || !limit if (FoundPercent(format) || !limit) {
? format return format;
: ConsumeNonPercentInner( } else {
ConsumeFront(format, GetChar(format, 0) == '%' && size_t len = 0;
GetChar(format, 1) == '%'
? 2 if (GetChar(format, 0) == '%' && GetChar(format, 1) == '%') {
: 1), len = 2;
limit - 1); } else {
len = 1;
}
return ConsumeNonPercentInner(ConsumeFront(format, len), limit - 1);
}
} }
// Consume characters until the next conversion spec %. // Consume characters until the next conversion spec %.
// It skips %%. // It skips %%.
static constexpr string_view ConsumeNonPercent(string_view format) { static constexpr string_view ConsumeNonPercent(string_view format) {
return FoundPercent(format) if (FoundPercent(format)) {
? format return format;
: ConsumeNonPercent(ConsumeNonPercentInner(format)); } else {
return ConsumeNonPercent(ConsumeNonPercentInner(format));
}
} }
static constexpr bool IsPositional(string_view format) { static constexpr bool IsPositional(string_view format) {
return IsDigit(GetChar(format, 0)) ? IsPositional(ConsumeFront(format)) if (IsDigit(GetChar(format, 0))) {
: GetChar(format, 0) == '$'; return IsPositional(ConsumeFront(format));
} else {
return GetChar(format, 0) == '$';
}
} }
constexpr bool RunImpl(bool is_positional) const { constexpr bool RunImpl(bool is_positional) const {

Loading…
Cancel
Save