xref: /aosp_15_r20/external/cronet/third_party/libc++/src/test/support/constexpr_char_traits.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _CONSTEXPR_CHAR_TRAITS
11 #define _CONSTEXPR_CHAR_TRAITS
12 
13 #include <string>
14 #include <cassert>
15 #include <cstddef>
16 
17 #include "test_macros.h"
18 
19 template <class CharT>
20 struct constexpr_char_traits
21 {
22     typedef CharT     char_type;
23     typedef int       int_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.
29 
assignconstexpr_char_traits30     static TEST_CONSTEXPR_CXX14 void assign(char_type& c1, const char_type& c2) TEST_NOEXCEPT
31         {c1 = c2;}
32 
eqconstexpr_char_traits33     static TEST_CONSTEXPR bool eq(char_type c1, char_type c2) TEST_NOEXCEPT
34         {return c1 == c2;}
35 
ltconstexpr_char_traits36     static TEST_CONSTEXPR  bool lt(char_type c1, char_type c2) TEST_NOEXCEPT
37         {return c1 < c2;}
38 
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);
45 
not_eofconstexpr_char_traits46     static TEST_CONSTEXPR int_type  not_eof(int_type c) TEST_NOEXCEPT
47         {return eq_int_type(c, eof()) ? ~eof() : c;}
48 
to_char_typeconstexpr_char_traits49     static TEST_CONSTEXPR char_type to_char_type(int_type c) TEST_NOEXCEPT
50         {return char_type(c);}
51 
to_int_typeconstexpr_char_traits52     static TEST_CONSTEXPR int_type  to_int_type(char_type c) TEST_NOEXCEPT
53         {return int_type(c);}
54 
eq_int_typeconstexpr_char_traits55     static TEST_CONSTEXPR bool      eq_int_type(int_type c1, int_type c2) TEST_NOEXCEPT
56         {return c1 == c2;}
57 
eofconstexpr_char_traits58     static TEST_CONSTEXPR int_type  eof() TEST_NOEXCEPT
59         {return int_type(EOF);}
60 };
61 
62 
63 template <class CharT>
64 TEST_CONSTEXPR_CXX14 int
compare(const char_type * s1,const char_type * s2,std::size_t n)65 constexpr_char_traits<CharT>::compare(const char_type* s1, const char_type* s2, std::size_t n)
66 {
67     for (; n; --n, ++s1, ++s2)
68     {
69         if (lt(*s1, *s2))
70             return -1;
71         if (lt(*s2, *s1))
72             return 1;
73     }
74     return 0;
75 }
76 
77 template <class CharT>
78 TEST_CONSTEXPR_CXX14 std::size_t
length(const char_type * s)79 constexpr_char_traits<CharT>::length(const char_type* s)
80 {
81     std::size_t len = 0;
82     for (; !eq(*s, char_type(0)); ++s)
83         ++len;
84     return len;
85 }
86 
87 template <class CharT>
88 TEST_CONSTEXPR_CXX14 const CharT*
find(const char_type * s,std::size_t n,const char_type & a)89 constexpr_char_traits<CharT>::find(const char_type* s, std::size_t n, const char_type& a)
90 {
91     for (; n; --n)
92     {
93         if (eq(*s, a))
94             return s;
95         ++s;
96     }
97     return 0;
98 }
99 
100 template <class CharT>
101 TEST_CONSTEXPR_CXX14 CharT*
move(char_type * s1,const char_type * s2,std::size_t n)102 constexpr_char_traits<CharT>::move(char_type* s1, const char_type* s2, std::size_t n)
103 {
104     char_type* r = s1;
105     if (s1 < s2)
106     {
107         for (; n; --n, ++s1, ++s2)
108             assign(*s1, *s2);
109     }
110     else if (s2 < s1)
111     {
112         s1 += n;
113         s2 += n;
114         for (; n; --n)
115             assign(*--s1, *--s2);
116     }
117     return r;
118 }
119 
120 template <class CharT>
121 TEST_CONSTEXPR_CXX14 CharT*
copy(char_type * s1,const char_type * s2,std::size_t n)122 constexpr_char_traits<CharT>::copy(char_type* s1, const char_type* s2, std::size_t n)
123 {
124     if (!TEST_IS_CONSTANT_EVALUATED) // fails in constexpr because we might be comparing unrelated pointers
125         assert(s2 < s1 || s2 >= s1+n);
126     char_type* r = s1;
127     for (; n; --n, ++s1, ++s2)
128         assign(*s1, *s2);
129     return r;
130 }
131 
132 template <class CharT>
133 TEST_CONSTEXPR_CXX14 CharT*
assign(char_type * s,std::size_t n,char_type a)134 constexpr_char_traits<CharT>::assign(char_type* s, std::size_t n, char_type a)
135 {
136     char_type* r = s;
137     for (; n; --n, ++s)
138         assign(*s, a);
139     return r;
140 }
141 
142 #endif // _CONSTEXPR_CHAR_TRAITS
143