Use std::char_traits::find() with ABSL_ASSIGN_OR_RETURN() instead of a constexpr loop to reduce load on the compiler PiperOrigin-RevId: 992406059 Change-Id: Ic360cf115890dc4ab585db001cdb949032e20a0e
diff --git a/absl/status/status_macros.h b/absl/status/status_macros.h index b823e0d..160e65b 100644 --- a/absl/status/status_macros.h +++ b/absl/status/status_macros.h
@@ -22,6 +22,7 @@ #define ABSL_STATUS_STATUS_MACROS_H_ #include <cstddef> +#include <string> #include <type_traits> #include <utility> @@ -174,21 +175,10 @@ ABSL_INTERNAL_STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_2_)) \ (return_keyword, __VA_ARGS__) -constexpr bool HasPotentialConditionalOperator(const char* lhs, int size) { - for (int i = 0; i < size; ++i) { - if (lhs[i] == '?') { - return true; - } - } - return false; -} - -template <std::size_t N> -constexpr bool IsEnclosedByParentheses(const char (&lhs)[N]) { - if (N < 2) { - return false; - } - return lhs[0] == '(' && lhs[N - 2] == ')'; +constexpr bool HasPotentiallyUnsafeConditionalOperator(const char* lhs, + size_t size) { + return lhs[0] == '(' && lhs[size - 1] == ')' && + std::char_traits<char>::find(lhs, size, '?') != nullptr; } namespace absl { @@ -249,8 +239,7 @@ } \ { \ static_assert( \ - !IsEnclosedByParentheses(#lhs) || \ - !HasPotentialConditionalOperator(#lhs, sizeof(#lhs) - 2), \ + !HasPotentiallyUnsafeConditionalOperator(#lhs, sizeof(#lhs) - 1), \ "Identified potential conditional operator, consider not " \ "using ABSL_ASSIGN_OR_RETURN"); \ } \