From d1027eb286433b5726e6778ee3a7ed85e4ca23d0 Mon Sep 17 00:00:00 2001 From: Giuseppe Ottaviano Date: Thu, 22 Oct 2015 12:11:59 -0700 Subject: [PATCH] Clang support for constexpr StringPiece constructor Summary: Clang's `strlen` is not `constexpr`, but `__builtin_strlen` is, so we can have an unconditional `constexpr` `StringPiece` constructor. Reviewed By: luciang, yfeldblum Differential Revision: D2561782 fb-gh-sync-id: 51e76a0d50355cc5ead1148ba2389b640a6888de --- folly/Portability.h | 9 +++++++++ folly/Range.h | 9 ++------- folly/test/RangeTest.cpp | 2 -- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/folly/Portability.h b/folly/Portability.h index c059a3cb..594ab9aa 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -17,6 +17,8 @@ #ifndef FOLLY_PORTABILITY_H_ #define FOLLY_PORTABILITY_H_ +#include + #include #ifndef FOLLY_NO_CONFIG @@ -375,6 +377,13 @@ inline void asm_pause() { #endif } +constexpr size_t constexpr_strlen(const char* s) { +#if defined(__clang__) + return __builtin_strlen(s); +#else + return strlen(s); +#endif } +} // namespace folly #endif // FOLLY_PORTABILITY_H_ diff --git a/folly/Range.h b/folly/Range.h index b074b0e8..8e517607 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -200,15 +200,10 @@ public: constexpr Range(Iter start, size_t size) : b_(start), e_(start + size) { } -#if FOLLY_HAVE_CONSTEXPR_STRLEN template ::type = 0> constexpr /* implicit */ Range(Iter str) - : b_(str), e_(str + strlen(str)) {} -#else - template ::type = 0> - /* implicit */ Range(Iter str) - : b_(str), e_(str + strlen(str)) {} -#endif + : b_(str), e_(str + constexpr_strlen(str)) {} + template ::const_type = 0> /* implicit */ Range(const std::string& str) : b_(str.data()), e_(b_ + str.size()) {} diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index 3038bff4..f9ffda97 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -290,7 +290,6 @@ TEST(StringPiece, InvalidRange) { EXPECT_THROW(a.subpiece(6), std::out_of_range); } -#if FOLLY_HAVE_CONSTEXPR_STRLEN constexpr char helloArray[] = "hello"; TEST(StringPiece, Constexpr) { @@ -300,7 +299,6 @@ TEST(StringPiece, Constexpr) { constexpr StringPiece hello2(helloArray); EXPECT_EQ("hello", hello2); } -#endif TEST(StringPiece, Prefix) { StringPiece a("hello"); -- 2.34.1