Implement P0426: Constexpr for std::char_traits

llvm-svn: 291741
This commit is contained in:
Marshall Clow 2017-01-12 04:37:14 +00:00
parent b4d9a310d2
commit 05d57faa2e
17 changed files with 386 additions and 52 deletions

View File

@ -26,13 +26,14 @@ struct char_traits
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits
};
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 @@ char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_
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 @@ char_traits<_CharT>::length(const char_type* __s)
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char>
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char>
{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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>
{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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
{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 @@ char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, siz
return 0;
}
inline
inline _LIBCPP_CONSTEXPR_AFTER_CXX14
size_t
char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
{
@ -347,7 +431,7 @@ char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
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 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
{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 @@ char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, siz
return 0;
}
inline
inline _LIBCPP_CONSTEXPR_AFTER_CXX14
size_t
char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
{
@ -466,7 +550,7 @@ char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
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
{

View File

@ -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
}

View File

@ -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 @@ int main()
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
}

View File

@ -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 @@ int main()
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
}

View File

@ -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 @@ int main()
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
}

View File

@ -12,12 +12,22 @@
// template<> struct char_traits<char16_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()
{
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 @@ int main()
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
}

View File

@ -12,12 +12,23 @@
// 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>
#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 @@ int main()
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
}

View File

@ -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 @@ int main()
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
}

View File

@ -12,12 +12,21 @@
// template<> struct char_traits<char16_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<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 @@ int main()
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
}

View File

@ -12,12 +12,22 @@
// template<> struct char_traits<char32_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()
{
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 @@ int main()
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
}

View File

@ -12,12 +12,22 @@
// 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>
#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 @@ int main()
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
}

View File

@ -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 @@ int main()
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
}

View File

@ -12,12 +12,21 @@
// template<> struct char_traits<char32_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<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 @@ int main()
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
}

View File

@ -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
}

View File

@ -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 @@ int main()
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
}

View File

@ -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 @@ int main()
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
}

View File

@ -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 @@ int main()
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
}