Commit 4a8a285d authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

Simplify the fallback constexpr strlen, strcmp

Summary: [Folly] Simplify the fallback implementations of `constexpr_strlen` and `constexpr_strcmp` by taking advantage of C++14 `constexpr` syntax.

Differential Revision: D23416900

fbshipit-source-id: 93a969282faa61b3c9ff8e75519ad4e6edd71e4e
parent ec9d37a5
...@@ -98,7 +98,7 @@ constexpr const Char (&checkNullTerminated(const Char (&a)[N]) noexcept)[N] { ...@@ -98,7 +98,7 @@ constexpr const Char (&checkNullTerminated(const Char (&a)[N]) noexcept)[N] {
return a[N - 1u] == Char(0) return a[N - 1u] == Char(0)
#ifndef NDEBUG #ifndef NDEBUG
// In Debug mode, guard against embedded nulls: // In Debug mode, guard against embedded nulls:
&& N - 1u == folly::detail::constexpr_strlen_internal(a, 0u) && N - 1u == folly::detail::constexpr_strlen_fallback(a)
#endif #endif
? decltype(a)(a) ? decltype(a)(a)
: (assertNotNullTerminated(), decltype(a)(a)); : (assertNotNullTerminated(), decltype(a)(a));
......
...@@ -26,25 +26,13 @@ namespace folly { ...@@ -26,25 +26,13 @@ namespace folly {
namespace detail { namespace detail {
template <typename Char>
constexpr size_t constexpr_strlen_internal(const Char* s, size_t len) {
// clang-format off
return
*(s + 0) == Char(0) ? len + 0 :
*(s + 1) == Char(0) ? len + 1 :
*(s + 2) == Char(0) ? len + 2 :
*(s + 3) == Char(0) ? len + 3 :
*(s + 4) == Char(0) ? len + 4 :
*(s + 5) == Char(0) ? len + 5 :
*(s + 6) == Char(0) ? len + 6 :
*(s + 7) == Char(0) ? len + 7 :
constexpr_strlen_internal(s + 8, len + 8);
// clang-format on
}
template <typename Char> template <typename Char>
constexpr size_t constexpr_strlen_fallback(const Char* s) { constexpr size_t constexpr_strlen_fallback(const Char* s) {
return constexpr_strlen_internal(s, 0); size_t ret = 0;
while (*s++) {
++ret;
}
return ret;
} }
static_assert( static_assert(
...@@ -53,9 +41,10 @@ static_assert( ...@@ -53,9 +41,10 @@ static_assert(
template <typename Char> template <typename Char>
constexpr int constexpr_strcmp_fallback(const Char* s1, const Char* s2) { constexpr int constexpr_strcmp_fallback(const Char* s1, const Char* s2) {
return (*s1 == '\0' || *s1 != *s2) while (*s1 && *s1 == *s2) {
? (static_cast<int>(*s1 - *s2)) ++s1, ++s2;
: constexpr_strcmp_fallback(s1 + 1, s2 + 1); }
return static_cast<int>(*s1 - *s2);
} }
} // namespace detail } // namespace detail
......
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