diff options
Diffstat (limited to 'absl/strings/string_view.h')
-rw-r--r-- | absl/strings/string_view.h | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h index 9c03108b4439..6bcd3c4e341f 100644 --- a/absl/strings/string_view.h +++ b/absl/strings/string_view.h @@ -173,8 +173,19 @@ class string_view { // Implicit constructor of a `string_view` from nul-terminated `str`. When // accepting possibly null strings, use `absl::NullSafeStringView(str)` // instead (see below). +#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \ + (defined(__GNUC__) && !defined(__clang__)) + // GCC has __builtin_strlen according to + // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but + // ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above. + // __builtin_strlen is constexpr. + constexpr string_view(const char* str) // NOLINT(runtime/explicit) + : ptr_(str), + length_(CheckLengthInternal(str ? __builtin_strlen(str) : 0)) {} +#else constexpr string_view(const char* str) // NOLINT(runtime/explicit) - : ptr_(str), length_(CheckLengthInternal(StrLenInternal(str))) {} + : ptr_(str), length_(CheckLengthInternal(str ? strlen(str) : 0)) {} +#endif // Implicit constructor of a `string_view` from a `const char*` and length. constexpr string_view(const char* data, size_type len) @@ -481,22 +492,6 @@ class string_view { static constexpr size_type kMaxSize = std::numeric_limits<difference_type>::max(); - // check whether __builtin_strlen is provided by the compiler. - // GCC doesn't have __has_builtin() - // (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66970), - // but has __builtin_strlen according to - // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html. -#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \ - (defined(__GNUC__) && !defined(__clang__)) - static constexpr size_type StrLenInternal(const char* str) { - return str ? __builtin_strlen(str) : 0; - } -#else - static constexpr size_type StrLenInternal(const char* str) { - return str ? strlen(str) : 0; - } -#endif - static constexpr size_type CheckLengthInternal(size_type len) { return ABSL_ASSERT(len <= kMaxSize), len; } |