2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _CONSTEXPR_CHAR_TRAITS
11 #define _CONSTEXPR_CHAR_TRAITS
17 #include "test_macros.h"
19 template <class CharT
>
20 struct constexpr_char_traits
22 typedef CharT char_type
;
24 typedef std::streamoff off_type
;
25 typedef std::streampos pos_type
;
26 typedef std::mbstate_t state_type
;
27 // The comparison_category is omitted so the class will have weak_ordering
28 // in C++20. This is intentional.
30 static TEST_CONSTEXPR_CXX14
void assign(char_type
& c1
, const char_type
& c2
) TEST_NOEXCEPT
33 static TEST_CONSTEXPR
bool eq(char_type c1
, char_type c2
) TEST_NOEXCEPT
36 static TEST_CONSTEXPR
bool lt(char_type c1
, char_type c2
) TEST_NOEXCEPT
39 static TEST_CONSTEXPR_CXX14
int compare(const char_type
* s1
, const char_type
* s2
, std::size_t n
);
40 static TEST_CONSTEXPR_CXX14
std::size_t length(const char_type
* s
);
41 static TEST_CONSTEXPR_CXX14
const char_type
* find(const char_type
* s
, std::size_t n
, const char_type
& a
);
42 static TEST_CONSTEXPR_CXX14 char_type
* move(char_type
* s1
, const char_type
* s2
, std::size_t n
);
43 static TEST_CONSTEXPR_CXX14 char_type
* copy(char_type
* s1
, const char_type
* s2
, std::size_t n
);
44 static TEST_CONSTEXPR_CXX14 char_type
* assign(char_type
* s
, std::size_t n
, char_type a
);
46 static TEST_CONSTEXPR int_type
not_eof(int_type c
) TEST_NOEXCEPT
47 {return eq_int_type(c
, eof()) ? ~eof() : c
;}
49 static TEST_CONSTEXPR char_type
to_char_type(int_type c
) TEST_NOEXCEPT
50 {return char_type(c
);}
52 static TEST_CONSTEXPR int_type
to_int_type(char_type c
) TEST_NOEXCEPT
55 static TEST_CONSTEXPR
bool eq_int_type(int_type c1
, int_type c2
) TEST_NOEXCEPT
58 static TEST_CONSTEXPR int_type
eof() TEST_NOEXCEPT
59 {return int_type(EOF
);}
63 template <class CharT
>
64 TEST_CONSTEXPR_CXX14
int
65 constexpr_char_traits
<CharT
>::compare(const char_type
* s1
, const char_type
* s2
, std::size_t n
)
67 for (; n
; --n
, ++s1
, ++s2
)
77 template <class CharT
>
78 TEST_CONSTEXPR_CXX14
std::size_t
79 constexpr_char_traits
<CharT
>::length(const char_type
* s
)
82 for (; !eq(*s
, char_type(0)); ++s
)
87 template <class CharT
>
88 TEST_CONSTEXPR_CXX14
const CharT
*
89 constexpr_char_traits
<CharT
>::find(const char_type
* s
, std::size_t n
, const char_type
& a
)
100 template <class CharT
>
101 TEST_CONSTEXPR_CXX14 CharT
*
102 constexpr_char_traits
<CharT
>::move(char_type
* s1
, const char_type
* s2
, std::size_t n
)
107 for (; n
; --n
, ++s1
, ++s2
)
115 assign(*--s1
, *--s2
);
120 template <class CharT
>
121 TEST_CONSTEXPR_CXX14 CharT
*
122 constexpr_char_traits
<CharT
>::copy(char_type
* s1
, const char_type
* s2
, std::size_t n
)
124 if (!TEST_IS_CONSTANT_EVALUATED
) // fails in constexpr because we might be comparing unrelated pointers
125 assert(s2
< s1
|| s2
>= s1
+n
);
127 for (; n
; --n
, ++s1
, ++s2
)
132 template <class CharT
>
133 TEST_CONSTEXPR_CXX14 CharT
*
134 constexpr_char_traits
<CharT
>::assign(char_type
* s
, std::size_t n
, char_type a
)
142 #endif // _CONSTEXPR_CHAR_TRAITS