Commit d1027eb2 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by facebook-github-bot-1

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
parent 50b33d29
......@@ -17,6 +17,8 @@
#ifndef FOLLY_PORTABILITY_H_
#define FOLLY_PORTABILITY_H_
#include <string.h>
#include <cstddef>
#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_
......@@ -200,15 +200,10 @@ public:
constexpr Range(Iter start, size_t size)
: b_(start), e_(start + size) { }
#if FOLLY_HAVE_CONSTEXPR_STRLEN
template <class T = Iter, typename detail::IsCharPointer<T>::type = 0>
constexpr /* implicit */ Range(Iter str)
: b_(str), e_(str + strlen(str)) {}
#else
template <class T = Iter, typename detail::IsCharPointer<T>::type = 0>
/* implicit */ Range(Iter str)
: b_(str), e_(str + strlen(str)) {}
#endif
: b_(str), e_(str + constexpr_strlen(str)) {}
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
/* implicit */ Range(const std::string& str)
: b_(str.data()), e_(b_ + str.size()) {}
......
......@@ -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");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment