Implement P0426: Constexpr for std::char_traits

llvm-svn: 291741
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 05d57faa2e54a8faf4fd31d0659c8ff24a6fd3da
diff --git a/include/__string b/include/__string
index 0b851b9..8152660 100644
--- a/include/__string
+++ b/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,26 @@
         {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
+}
+
+
 // char_traits<wchar_t>
 
 template <>
@@ -245,19 +269,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 +304,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,19 +375,19 @@
     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 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 _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 char_type*       move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
     _LIBCPP_INLINE_VISIBILITY
@@ -323,7 +407,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 +421,7 @@
     return 0;
 }
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 size_t
 char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
 {
@@ -347,7 +431,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,19 +494,19 @@
     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 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 _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 char_type*       move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
     _LIBCPP_INLINE_VISIBILITY
@@ -442,7 +526,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 +540,7 @@
     return 0;
 }
 
-inline
+inline _LIBCPP_CONSTEXPR_AFTER_CXX14
 size_t
 char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
 {
@@ -466,7 +550,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
 {