Commit 754ee1bc authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

add constexpr_strcmp()

Summary: Add a constexpr implementation of `strcmp()`

Reviewed By: yfeldblum

Differential Revision: D6881816

fbshipit-source-id: 08a9e5ab9ef4a422ff6d004ec13b8ec8c71b0d6f
parent 4464999f
......@@ -33,6 +33,13 @@ constexpr size_t constexpr_strlen_internal(const Char* s, size_t len) {
static_assert(
constexpr_strlen_internal("123456789", 0) == 9,
"Someone appears to have broken constexpr_strlen...");
template <typename Char>
constexpr int constexpr_strcmp_internal(const Char* s1, const Char* s2) {
return (*s1 == '\0' || *s1 != *s2)
? (static_cast<int>(*s1 - *s2))
: constexpr_strcmp_internal(s1 + 1, s2 + 1);
}
} // namespace detail
template <typename Char>
......@@ -52,4 +59,22 @@ constexpr size_t constexpr_strlen(const char* s) {
return detail::constexpr_strlen_internal(s, 0);
#endif
}
template <typename Char>
constexpr int constexpr_strcmp(const Char* s1, const Char* s2) {
return detail::constexpr_strcmp_internal(s1, s2);
}
template <>
constexpr int constexpr_strcmp(const char* s1, const char* s2) {
#if FOLLY_HAS_FEATURE(cxx_constexpr_string_builtins)
// clang provides a constexpr builtin
return __builtin_strcmp(s1, s2);
#elif defined(__GNUC__) && !defined(__clang__)
// strcmp() happens to already be constexpr under gcc
return std::strcmp(s1, s2);
#else
return detail::constexpr_strcmp_internal(s1, s2);
#endif
}
} // namespace folly
......@@ -18,12 +18,9 @@
#include <folly/portability/GTest.h>
namespace {
using folly::constexpr_strcmp;
class ConstexprTest : public testing::Test {};
} // namespace
TEST_F(ConstexprTest, constexpr_strlen_cstr) {
TEST(ConstexprTest, constexpr_strlen_cstr) {
constexpr auto v = "hello";
constexpr auto a = folly::constexpr_strlen(v);
EXPECT_EQ(5, a);
......@@ -32,10 +29,26 @@ TEST_F(ConstexprTest, constexpr_strlen_cstr) {
// gcc-4.9 cannot compile the following constexpr code correctly
#if !(defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5)
TEST_F(ConstexprTest, constexpr_strlen_ints) {
TEST(ConstexprTest, constexpr_strlen_ints) {
constexpr int v[] = {5, 3, 4, 0, 7};
constexpr auto a = folly::constexpr_strlen(v);
EXPECT_EQ(3, a);
EXPECT_TRUE((std::is_same<const size_t, decltype(a)>::value));
}
TEST(ConstexprTest, constexpr_strcmp_ints) {
constexpr int v[] = {5, 3, 4, 0, 7};
constexpr int v1[] = {6, 4};
static_assert(constexpr_strcmp(v1, v) > 0, "constexpr_strcmp is broken");
static_assert(constexpr_strcmp(v, v) == 0, "constexpr_strcmp is broken");
}
#endif
static_assert(
constexpr_strcmp("abc", "abc") == 0,
"constexpr_strcmp is broken");
static_assert(constexpr_strcmp("", "") == 0, "constexpr_strcmp is broken");
static_assert(constexpr_strcmp("abc", "def") < 0, "constexpr_strcmp is broken");
static_assert(constexpr_strcmp("xyz", "abc") > 0, "constexpr_strcmp is broken");
static_assert(constexpr_strcmp("a", "abc") < 0, "constexpr_strcmp is broken");
static_assert(constexpr_strcmp("abc", "a") > 0, "constexpr_strcmp is broken");
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