mclow.lists created this revision.
mclow.lists added reviewers: EricWF, rsmith.
mclow.lists added a subscriber: cfe-commits.

Make `assign`/`length`/`find`/`compare` for `std::char_traits` constexpr.

This makes using `string_view`s at compile time easier.

Use the compiler intrinsics when available.
Note that `__builtin_memchr` is not really appropriate, since it returns a 
`void *`.
Sadly, this turns a bunch of code into "a twisty little maze of ifdefs, all 
different", but oh well.


https://reviews.llvm.org/D28473

Files:
  include/__string
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/assign2.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/compare.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/find.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/length.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/assign2.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/compare.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/find.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/length.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/assign2.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/compare.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/find.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/length.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/assign2.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/compare.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/find.pass.cpp
  
test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/length.pass.cpp

Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/length.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/length.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/length.pass.cpp
@@ -12,10 +12,21 @@
 // template<> struct char_traits<wchar_t>
 
 // static size_t length(const char_type* s);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<wchar_t>::length(L"") == 0
+        && std::char_traits<wchar_t>::length(L"abcd") == 4;
+}
+#endif
+
 int main()
 {
     assert(std::char_traits<wchar_t>::length(L"") == 0);
@@ -23,4 +34,8 @@
     assert(std::char_traits<wchar_t>::length(L"aa") == 2);
     assert(std::char_traits<wchar_t>::length(L"aaa") == 3);
     assert(std::char_traits<wchar_t>::length(L"aaaa") == 4);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/find.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/find.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/find.pass.cpp
@@ -12,10 +12,24 @@
 // template<> struct char_traits<wchar_t>
 
 // static const char_type* find(const char_type* s, size_t n, const char_type& a);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    constexpr const wchar_t *p = L"123";
+    return std::char_traits<wchar_t>::find(p, 3, L'1') == p
+        && std::char_traits<wchar_t>::find(p, 3, L'2') == p + 1
+        && std::char_traits<wchar_t>::find(p, 3, L'3') == p + 2
+        && std::char_traits<wchar_t>::find(p, 3, L'4') == nullptr;
+}
+#endif
+
 int main()
 {
     wchar_t s1[] = {1, 2, 3};
@@ -25,4 +39,8 @@
     assert(std::char_traits<wchar_t>::find(s1, 3, wchar_t(4)) == 0);
     assert(std::char_traits<wchar_t>::find(s1, 3, wchar_t(0)) == 0);
     assert(std::char_traits<wchar_t>::find(NULL, 0, wchar_t(0)) == 0);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/compare.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/compare.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/compare.pass.cpp
@@ -12,10 +12,22 @@
 // template<> struct char_traits<wchar_t>
 
 // static int compare(const char_type* s1, const char_type* s2, size_t n);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<wchar_t>::compare(L"123", L"223", 3) < 0
+        && std::char_traits<wchar_t>::compare(L"223", L"123", 3) > 0
+        && std::char_traits<wchar_t>::compare(L"123", L"123", 3) == 0;
+}
+#endif
+
 int main()
 {
     assert(std::char_traits<wchar_t>::compare(L"", L"", 0) == 0);
@@ -38,4 +50,8 @@
     assert(std::char_traits<wchar_t>::compare(L"223", L"123", 3) > 0);
     assert(std::char_traits<wchar_t>::compare(L"133", L"123", 3) > 0);
     assert(std::char_traits<wchar_t>::compare(L"124", L"123", 3) > 0);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/assign2.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/assign2.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.wchar.t/assign2.pass.cpp
@@ -12,13 +12,29 @@
 // template<> struct char_traits<wchar_t>
 
 // static void assign(char_type& c1, const char_type& c2);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    wchar_t c = L'1';
+    std::char_traits<wchar_t>::assign(c, L'a');
+    return c == L'a';
+}
+#endif
+
 int main()
 {
     wchar_t c = L'\0';
     std::char_traits<wchar_t>::assign(c, L'a');
     assert(c == L'a');
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/length.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/length.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/length.pass.cpp
@@ -12,6 +12,7 @@
 // template<> struct char_traits<char32_t>
 
 // static size_t length(const char_type* s);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
@@ -18,6 +19,14 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<char32_t>::length(U"") == 0
+        && std::char_traits<char32_t>::length(U"abcd") == 4;
+}
+#endif
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -28,5 +37,9 @@
     assert(std::char_traits<char32_t>::length(U"aaa") == 3);
     assert(std::char_traits<char32_t>::length(U"aaaa") == 4);
 #endif
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/find.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/find.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/find.pass.cpp
@@ -12,10 +12,24 @@
 // template<> struct char_traits<char32_t>
 
 // static const char_type* find(const char_type* s, size_t n, const char_type& a);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    constexpr const char32_t *p = U"123";
+    return std::char_traits<char32_t>::find(p, 3, U'1') == p
+        && std::char_traits<char32_t>::find(p, 3, U'2') == p + 1
+        && std::char_traits<char32_t>::find(p, 3, U'3') == p + 2
+        && std::char_traits<char32_t>::find(p, 3, U'4') == nullptr;
+}
+#endif
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -26,5 +40,9 @@
     assert(std::char_traits<char32_t>::find(s1, 3, char32_t(4)) == 0);
     assert(std::char_traits<char32_t>::find(s1, 3, char32_t(0)) == 0);
     assert(std::char_traits<char32_t>::find(NULL, 0, char32_t(0)) == 0);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/compare.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/compare.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/compare.pass.cpp
@@ -12,6 +12,7 @@
 // template<> struct char_traits<char32_t>
 
 // static int compare(const char_type* s1, const char_type* s2, size_t n);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
@@ -18,6 +19,15 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<char32_t>::compare(U"123", U"223", 3) < 0
+        && std::char_traits<char32_t>::compare(U"223", U"123", 3) > 0
+        && std::char_traits<char32_t>::compare(U"123", U"123", 3) == 0;
+}
+#endif
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -43,5 +53,9 @@
     assert(std::char_traits<char32_t>::compare(U"133", U"123", 3) > 0);
     assert(std::char_traits<char32_t>::compare(U"124", U"123", 3) > 0);
 #endif
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/assign2.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/assign2.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char32_t/assign2.pass.cpp
@@ -12,6 +12,7 @@
 // template<> struct char_traits<char32_t>
 
 // static void assign(char_type& c1, const char_type& c2);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
@@ -18,6 +19,15 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    char32_t c = U'1';
+    std::char_traits<char32_t>::assign(c, U'a');
+    return c == U'a';
+}
+#endif
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -26,5 +36,9 @@
     std::char_traits<char32_t>::assign(c, U'a');
     assert(c == U'a');
 #endif
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/length.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/length.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/length.pass.cpp
@@ -12,6 +12,7 @@
 // template<> struct char_traits<char16_t>
 
 // static size_t length(const char_type* s);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
@@ -18,6 +19,14 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<char16_t>::length(u"") == 0
+        && std::char_traits<char16_t>::length(u"abcd") == 4;
+}
+#endif
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -28,5 +37,9 @@
     assert(std::char_traits<char16_t>::length(u"aaa") == 3);
     assert(std::char_traits<char16_t>::length(u"aaaa") == 4);
 #endif
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/find.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/find.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/find.pass.cpp
@@ -12,10 +12,24 @@
 // template<> struct char_traits<char16_t>
 
 // static const char_type* find(const char_type* s, size_t n, const char_type& a);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    constexpr const char16_t *p = u"123";
+    return std::char_traits<char16_t>::find(p, 3, u'1') == p
+        && std::char_traits<char16_t>::find(p, 3, u'2') == p + 1
+        && std::char_traits<char16_t>::find(p, 3, u'3') == p + 2
+        && std::char_traits<char16_t>::find(p, 3, u'4') == nullptr;
+}
+#endif
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -26,5 +40,9 @@
     assert(std::char_traits<char16_t>::find(s1, 3, char16_t(4)) == 0);
     assert(std::char_traits<char16_t>::find(s1, 3, char16_t(0)) == 0);
     assert(std::char_traits<char16_t>::find(NULL, 0, char16_t(0)) == 0);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/compare.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/compare.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/compare.pass.cpp
@@ -12,6 +12,7 @@
 // template<> struct char_traits<char16_t>
 
 // static int compare(const char_type* s1, const char_type* s2, size_t n);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
@@ -18,6 +19,16 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<char16_t>::compare(u"123", u"223", 3) < 0
+        && std::char_traits<char16_t>::compare(u"223", u"123", 3) > 0
+        && std::char_traits<char16_t>::compare(u"123", u"123", 3) == 0;
+}
+#endif
+
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -43,5 +54,9 @@
     assert(std::char_traits<char16_t>::compare(u"133", u"123", 3) > 0);
     assert(std::char_traits<char16_t>::compare(u"124", u"123", 3) > 0);
 #endif
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/assign2.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/assign2.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char16_t/assign2.pass.cpp
@@ -12,6 +12,7 @@
 // template<> struct char_traits<char16_t>
 
 // static void assign(char_type& c1, const char_type& c2);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
@@ -18,6 +19,15 @@
 
 #include "test_macros.h"
 
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    char16_t c = u'1';
+    std::char_traits<char16_t>::assign(c, u'a');
+    return c == u'a';
+}
+#endif
+
 int main()
 {
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
@@ -26,5 +36,9 @@
     std::char_traits<char16_t>::assign(c, u'a');
     assert(c == u'a');
 #endif
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/length.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/length.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/length.pass.cpp
@@ -12,10 +12,22 @@
 // template<> struct char_traits<char>
 
 // static size_t length(const char_type* s);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<char>::length("") == 0
+        && std::char_traits<char>::length("abcd") == 4;
+}
+#endif
+
+
 int main()
 {
     assert(std::char_traits<char>::length("") == 0);
@@ -23,4 +35,8 @@
     assert(std::char_traits<char>::length("aa") == 2);
     assert(std::char_traits<char>::length("aaa") == 3);
     assert(std::char_traits<char>::length("aaaa") == 4);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/find.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/find.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/find.pass.cpp
@@ -12,10 +12,24 @@
 // template<> struct char_traits<char>
 
 // static const char_type* find(const char_type* s, size_t n, const char_type& a);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    constexpr const char *p = "123";
+    return std::char_traits<char>::find(p, 3, '1') == p
+        && std::char_traits<char>::find(p, 3, '2') == p + 1
+        && std::char_traits<char>::find(p, 3, '3') == p + 2
+        && std::char_traits<char>::find(p, 3, '4') == nullptr;
+}
+#endif
+
 int main()
 {
     char s1[] = {1, 2, 3};
@@ -25,4 +39,8 @@
     assert(std::char_traits<char>::find(s1, 3, char(4)) == 0);
     assert(std::char_traits<char>::find(s1, 3, char(0)) == 0);
     assert(std::char_traits<char>::find(NULL, 0, char(0)) == 0);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/compare.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/compare.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/compare.pass.cpp
@@ -12,10 +12,22 @@
 // template<> struct char_traits<char>
 
 // static int compare(const char_type* s1, const char_type* s2, size_t n);
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    return std::char_traits<char>::compare("123", "223", 3) < 0
+        && std::char_traits<char>::compare("223", "123", 3) > 0
+        && std::char_traits<char>::compare("123", "123", 3) == 0;
+}
+#endif
+
 int main()
 {
     assert(std::char_traits<char>::compare("", "", 0) == 0);
@@ -38,4 +50,8 @@
     assert(std::char_traits<char>::compare("223", "123", 3) > 0);
     assert(std::char_traits<char>::compare("133", "123", 3) > 0);
     assert(std::char_traits<char>::compare("124", "123", 3) > 0);
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/assign2.pass.cpp
===================================================================
--- test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/assign2.pass.cpp
+++ test/std/strings/char.traits/char.traits.specializations/char.traits.specializations.char/assign2.pass.cpp
@@ -11,14 +11,30 @@
 
 // template<> struct char_traits<char>
 
-// static void assign(char_type& c1, const char_type& c2);
+// static constexpr void assign(char_type& c1, const char_type& c2); // constexpr in C++17
+// constexpr in C++17
 
 #include <string>
 #include <cassert>
 
+#include "test_macros.h"
+
+#if TEST_STD_VER > 14
+constexpr bool test_constexpr()
+{
+    char c = '1';
+    std::char_traits<char>::assign(c, 'a');
+    return c == 'a';
+}
+#endif
+
 int main()
 {
     char c = '\0';
     std::char_traits<char>::assign(c, 'a');
     assert(c == 'a');
+
+#if TEST_STD_VER > 14
+    static_assert(test_constexpr(), "" );
+#endif
 }
Index: include/__string
===================================================================
--- include/__string
+++ include/__string
@@ -26,13 +26,14 @@
     typedef streampos pos_type;
     typedef mbstate_t state_type;
 
-    static void assign(char_type& c1, const char_type& c2) noexcept;
+    static constexpr void assign(char_type& c1, const char_type& c2) noexcept;
     static constexpr bool eq(char_type c1, char_type c2) noexcept;
     static constexpr bool lt(char_type c1, char_type c2) noexcept;
 
-    static int              compare(const char_type* s1, const char_type* s2, size_t n);
-    static size_t           length(const char_type* s);
-    static const char_type* find(const char_type* s, size_t n, const char_type& a);
+    static constexpr int    compare(const char_type* s1, const char_type* s2, size_t n);
+    static constexpr size_t length(const char_type* s);
+    static constexpr const char_type* 
+                            find(const char_type* s, size_t n, const char_type& a);
     static char_type*       move(char_type* s1, const char_type* s2, size_t n);
     static char_type*       copy(char_type* s1, const char_type* s2, size_t n);
     static char_type*       assign(char_type* s, size_t n, char_type a);
@@ -77,18 +78,19 @@
     typedef streampos pos_type;
     typedef mbstate_t state_type;
 
-    static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
-        {__c1 = __c2;}
+    static inline void _LIBCPP_CONSTEXPR_AFTER_CXX14
+        assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
     static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 == __c2;}
     static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 < __c2;}
 
-    static int              compare(const char_type* __s1, const char_type* __s2, size_t __n);
-    _LIBCPP_INLINE_VISIBILITY
-    static size_t           length(const char_type* __s);
-    _LIBCPP_INLINE_VISIBILITY
-    static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
+    static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    int compare(const char_type* __s1, const char_type* __s2, size_t __n);
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    size_t length(const char_type* __s);
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
     static char_type*       move(char_type* __s1, const char_type* __s2, size_t __n);
     _LIBCPP_INLINE_VISIBILITY
     static char_type*       copy(char_type* __s1, const char_type* __s2, size_t __n);
@@ -108,7 +110,7 @@
 };
 
 template <class _CharT>
-int
+_LIBCPP_CONSTEXPR_AFTER_CXX14 int
 char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
 {
     for (; __n; --__n, ++__s1, ++__s2)
@@ -123,7 +125,7 @@
 
 template <class _CharT>
 inline
-size_t
+_LIBCPP_CONSTEXPR_AFTER_CXX14 size_t
 char_traits<_CharT>::length(const char_type* __s)
 {
     size_t __len = 0;
@@ -134,7 +136,7 @@
 
 template <class _CharT>
 inline
-const _CharT*
+_LIBCPP_CONSTEXPR_AFTER_CXX14 const _CharT*
 char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
 {
     for (; __n; --__n)
@@ -200,18 +202,20 @@
     typedef streampos pos_type;
     typedef mbstate_t state_type;
 
-    static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
-        {__c1 = __c2;}
+    static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+    void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
     static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
             {return __c1 == __c2;}
     static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
         {return (unsigned char)__c1 < (unsigned char)__c2;}
 
-    static inline int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
-        {return __n == 0 ? 0 : memcmp(__s1, __s2, __n);}
-    static inline size_t length(const char_type* __s)  _NOEXCEPT {return strlen(__s);}
-    static inline const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
-        {return __n == 0 ? NULL : (const char_type*) memchr(__s, to_int_type(__a), __n);}
+    static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+    int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
+        {return __n == 0 ? 0 : __builtin_memcmp(__s1, __s2, __n);}
+    static inline size_t _LIBCPP_CONSTEXPR_AFTER_CXX14
+    length(const char_type* __s)  _NOEXCEPT {return __builtin_strlen(__s);}
+    static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
     static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
         {return __n == 0 ? __s1 : (char_type*) memmove(__s1, __s2, __n);}
     static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
@@ -234,6 +238,47 @@
         {return int_type(EOF);}
 };
 
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+const char*
+char_traits<char>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
+{
+    if (__n == 0)
+        return NULL;
+#if _LIBCPP_STD_VER <= 14
+    return (const char_type*) memchr(__s, to_int_type(__a), __n);
+#else
+    for (; __n; --__n)
+    {
+        if (eq(*__s, __a))
+            return __s;
+        ++__s;
+    }
+    return NULL;
+#endif
+}
+
+
+// inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+// int
+// char_traits<char>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
+// {
+//     if (__n == 0)
+//         return 0;
+// #if _LIBCPP_STD_VER <= 14
+//     return memcmp(__s1, __s2, __n);
+// #else
+//     for (; __n; --__n, ++__s1, ++__s2)
+//     {
+//         if (lt(*__s1, *__s2))
+//             return -1;
+//         if (lt(*__s2, *__s1))
+//             return 1;
+//     }
+//     return 0;
+// #endif
+// }
+
+
 // char_traits<wchar_t>
 
 template <>
@@ -245,19 +290,19 @@
     typedef streampos pos_type;
     typedef mbstate_t state_type;
 
-    static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
-        {__c1 = __c2;}
+    static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+    void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
     static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 == __c2;}
     static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 < __c2;}
 
-    static inline int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
-        {return __n == 0 ? 0 : wmemcmp(__s1, __s2, __n);}
-    static inline size_t length(const char_type* __s) _NOEXCEPT
-        {return wcslen(__s);}
-    static inline const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
-        {return __n == 0 ? NULL : (const char_type*)wmemchr(__s, __a, __n);}
+    static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
+    static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    size_t length(const char_type* __s) _NOEXCEPT;
+    static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
     static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
         {return __n == 0 ? __s1 : (char_type*)wmemmove(__s1, __s2, __n);}
     static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
@@ -280,6 +325,66 @@
         {return int_type(WEOF);}
 };
 
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+int
+char_traits<wchar_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
+{
+    if (__n == 0)
+        return 0;
+#if __has_builtin(__builtin_wmemcmp)
+    return __builtin_wmemcmp(__s1, __s2, __n);
+#elif _LIBCPP_STD_VER <= 14
+    return wmemcmp(__s1, __s2, __n);
+#else
+    for (; __n; --__n, ++__s1, ++__s2)
+    {
+        if (lt(*__s1, *__s2))
+            return -1;
+        if (lt(*__s2, *__s1))
+            return 1;
+    }
+    return 0;
+#endif
+}
+
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+size_t
+char_traits<wchar_t>::length(const char_type* __s) _NOEXCEPT
+{
+#if __has_builtin(__builtin_wcslen)
+    return __builtin_wcslen(__s);
+#elif _LIBCPP_STD_VER <= 14
+    return wcslen(__s);
+#else
+    size_t __len = 0;
+    for (; !eq(*__s, char_type(0)); ++__s)
+        ++__len;
+    return __len;
+#endif
+}
+
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+const wchar_t*
+char_traits<wchar_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
+{
+    if (__n == 0)
+        return NULL;
+#if __has_builtin(__builtin_wmemchr)
+        return __builtin_wmemchr(__s, __a, __n);
+#elif _LIBCPP_STD_VER <= 14
+    return wmemchr(__s, __a, __n);
+#else
+    for (; __n; --__n)
+    {
+        if (eq(*__s, __a))
+            return __s;
+        ++__s;
+    }
+    return NULL;
+#endif
+}
+
+
 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
 
 template <>
@@ -291,20 +396,20 @@
     typedef u16streampos   pos_type;
     typedef mbstate_t      state_type;
 
-    static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
-        {__c1 = __c2;}
+    static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+    void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
     static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 == __c2;}
     static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 < __c2;}
 
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    int              compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    size_t           length(const char_type* __s) _NOEXCEPT;
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
     _LIBCPP_INLINE_VISIBILITY
-    static int              compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
-    _LIBCPP_INLINE_VISIBILITY
-    static size_t           length(const char_type* __s) _NOEXCEPT;
-    _LIBCPP_INLINE_VISIBILITY
-    static const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
-    _LIBCPP_INLINE_VISIBILITY
     static char_type*       move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
     _LIBCPP_INLINE_VISIBILITY
     static char_type*       copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
@@ -323,7 +428,7 @@
         {return int_type(0xFFFF);}
 };
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 int
 char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
 {
@@ -337,7 +442,7 @@
     return 0;
 }
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 size_t
 char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
 {
@@ -347,7 +452,7 @@
     return __len;
 }
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 const char16_t*
 char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
 {
@@ -410,20 +515,20 @@
     typedef u32streampos   pos_type;
     typedef mbstate_t      state_type;
 
-    static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
-        {__c1 = __c2;}
+    static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
+    void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
     static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 == __c2;}
     static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
         {return __c1 < __c2;}
 
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    int              compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    size_t           length(const char_type* __s) _NOEXCEPT;
+    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
+    const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
     _LIBCPP_INLINE_VISIBILITY
-    static int              compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
-    _LIBCPP_INLINE_VISIBILITY
-    static size_t           length(const char_type* __s) _NOEXCEPT;
-    _LIBCPP_INLINE_VISIBILITY
-    static const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
-    _LIBCPP_INLINE_VISIBILITY
     static char_type*       move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
     _LIBCPP_INLINE_VISIBILITY
     static char_type*       copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
@@ -442,7 +547,7 @@
         {return int_type(0xFFFFFFFF);}
 };
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 int
 char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
 {
@@ -456,7 +561,7 @@
     return 0;
 }
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 size_t
 char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
 {
@@ -466,7 +571,7 @@
     return __len;
 }
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 const char32_t*
 char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
 {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to