[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libcxx / test / support / constexpr_char_traits.h
blob75380d5a7ffbb3c78607f52b9f9283d5dadb97e9
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 //===----------------------------------------------------------------------===//
10 #ifndef _CONSTEXPR_CHAR_TRAITS
11 #define _CONSTEXPR_CHAR_TRAITS
13 #include <string>
14 #include <cassert>
15 #include <cstddef>
17 #include "test_macros.h"
19 template <class CharT>
20 struct constexpr_char_traits
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.
30 static TEST_CONSTEXPR_CXX14 void assign(char_type& c1, const char_type& c2) TEST_NOEXCEPT
31 {c1 = c2;}
33 static TEST_CONSTEXPR bool eq(char_type c1, char_type c2) TEST_NOEXCEPT
34 {return c1 == c2;}
36 static TEST_CONSTEXPR bool lt(char_type c1, char_type c2) TEST_NOEXCEPT
37 {return c1 < c2;}
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
53 {return int_type(c);}
55 static TEST_CONSTEXPR bool eq_int_type(int_type c1, int_type c2) TEST_NOEXCEPT
56 {return c1 == c2;}
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)
69 if (lt(*s1, *s2))
70 return -1;
71 if (lt(*s2, *s1))
72 return 1;
74 return 0;
77 template <class CharT>
78 TEST_CONSTEXPR_CXX14 std::size_t
79 constexpr_char_traits<CharT>::length(const char_type* s)
81 std::size_t len = 0;
82 for (; !eq(*s, char_type(0)); ++s)
83 ++len;
84 return len;
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)
91 for (; n; --n)
93 if (eq(*s, a))
94 return s;
95 ++s;
97 return 0;
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)
104 char_type* r = s1;
105 if (s1 < s2)
107 for (; n; --n, ++s1, ++s2)
108 assign(*s1, *s2);
110 else if (s2 < s1)
112 s1 += n;
113 s2 += n;
114 for (; n; --n)
115 assign(*--s1, *--s2);
117 return r;
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);
126 char_type* r = s1;
127 for (; n; --n, ++s1, ++s2)
128 assign(*s1, *s2);
129 return r;
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)
136 char_type* r = s;
137 for (; n; --n, ++s)
138 assign(*s, a);
139 return r;
142 #endif // _CONSTEXPR_CHAR_TRAITS