[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / support / constexpr_char_traits.h
blobdf43d8f7c19ae233fa4f7e7e7c1cad97046086b7
1 // -*- C++ -*-
2 //===-------------------- constexpr_char_traits ---------------------------===//
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>
16 #include "test_macros.h"
18 template <class _CharT>
19 struct constexpr_char_traits
21 typedef _CharT char_type;
22 typedef int int_type;
23 typedef std::streamoff off_type;
24 typedef std::streampos pos_type;
25 typedef std::mbstate_t state_type;
27 static TEST_CONSTEXPR_CXX14 void assign(char_type& __c1, const char_type& __c2) TEST_NOEXCEPT
28 {__c1 = __c2;}
30 static TEST_CONSTEXPR bool eq(char_type __c1, char_type __c2) TEST_NOEXCEPT
31 {return __c1 == __c2;}
33 static TEST_CONSTEXPR bool lt(char_type __c1, char_type __c2) TEST_NOEXCEPT
34 {return __c1 < __c2;}
36 static TEST_CONSTEXPR_CXX14 int compare(const char_type* __s1, const char_type* __s2, size_t __n);
37 static TEST_CONSTEXPR_CXX14 size_t length(const char_type* __s);
38 static TEST_CONSTEXPR_CXX14 const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
39 static TEST_CONSTEXPR_CXX14 char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
40 static TEST_CONSTEXPR_CXX14 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
41 static TEST_CONSTEXPR_CXX14 char_type* assign(char_type* __s, size_t __n, char_type __a);
43 static TEST_CONSTEXPR int_type not_eof(int_type __c) TEST_NOEXCEPT
44 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
46 static TEST_CONSTEXPR char_type to_char_type(int_type __c) TEST_NOEXCEPT
47 {return char_type(__c);}
49 static TEST_CONSTEXPR int_type to_int_type(char_type __c) TEST_NOEXCEPT
50 {return int_type(__c);}
52 static TEST_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) TEST_NOEXCEPT
53 {return __c1 == __c2;}
55 static TEST_CONSTEXPR int_type eof() TEST_NOEXCEPT
56 {return int_type(EOF);}
60 template <class _CharT>
61 TEST_CONSTEXPR_CXX14 int
62 constexpr_char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
64 for (; __n; --__n, ++__s1, ++__s2)
66 if (lt(*__s1, *__s2))
67 return -1;
68 if (lt(*__s2, *__s1))
69 return 1;
71 return 0;
74 template <class _CharT>
75 TEST_CONSTEXPR_CXX14 size_t
76 constexpr_char_traits<_CharT>::length(const char_type* __s)
78 size_t __len = 0;
79 for (; !eq(*__s, char_type(0)); ++__s)
80 ++__len;
81 return __len;
84 template <class _CharT>
85 TEST_CONSTEXPR_CXX14 const _CharT*
86 constexpr_char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
88 for (; __n; --__n)
90 if (eq(*__s, __a))
91 return __s;
92 ++__s;
94 return 0;
97 template <class _CharT>
98 TEST_CONSTEXPR_CXX14 _CharT*
99 constexpr_char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
101 char_type* __r = __s1;
102 if (__s1 < __s2)
104 for (; __n; --__n, ++__s1, ++__s2)
105 assign(*__s1, *__s2);
107 else if (__s2 < __s1)
109 __s1 += __n;
110 __s2 += __n;
111 for (; __n; --__n)
112 assign(*--__s1, *--__s2);
114 return __r;
117 template <class _CharT>
118 TEST_CONSTEXPR_CXX14 _CharT*
119 constexpr_char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
121 assert(__s2 < __s1 || __s2 >= __s1+__n);
122 char_type* __r = __s1;
123 for (; __n; --__n, ++__s1, ++__s2)
124 assign(*__s1, *__s2);
125 return __r;
128 template <class _CharT>
129 TEST_CONSTEXPR_CXX14 _CharT*
130 constexpr_char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
132 char_type* __r = __s;
133 for (; __n; --__n, ++__s)
134 assign(*__s, __a);
135 return __r;
138 #endif // _CONSTEXPR_CHAR_TRAITS