• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===-------------------- constexpr_char_traits ---------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef _CONSTEXPR_CHAR_TRAITS
12 #define _CONSTEXPR_CHAR_TRAITS
13 
14 #include <string>
15 #include <cassert>
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 
assignconstexpr_char_traits28     static TEST_CONSTEXPR_CXX14 void assign(char_type& __c1, const char_type& __c2) TEST_NOEXCEPT
29         {__c1 = __c2;}
30 
eqconstexpr_char_traits31     static TEST_CONSTEXPR bool eq(char_type __c1, char_type __c2) TEST_NOEXCEPT
32         {return __c1 == __c2;}
33 
ltconstexpr_char_traits34     static TEST_CONSTEXPR  bool lt(char_type __c1, char_type __c2) TEST_NOEXCEPT
35         {return __c1 < __c2;}
36 
37     static TEST_CONSTEXPR_CXX14 int              compare(const char_type* __s1, const char_type* __s2, size_t __n);
38     static TEST_CONSTEXPR_CXX14 size_t           length(const char_type* __s);
39     static TEST_CONSTEXPR_CXX14 const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
40     static TEST_CONSTEXPR_CXX14 char_type*       move(char_type* __s1, const char_type* __s2, size_t __n);
41     static TEST_CONSTEXPR_CXX14 char_type*       copy(char_type* __s1, const char_type* __s2, size_t __n);
42     static TEST_CONSTEXPR_CXX14 char_type*       assign(char_type* __s, size_t __n, char_type __a);
43 
not_eofconstexpr_char_traits44     static TEST_CONSTEXPR int_type  not_eof(int_type __c) TEST_NOEXCEPT
45         {return eq_int_type(__c, eof()) ? ~eof() : __c;}
46 
to_char_typeconstexpr_char_traits47     static TEST_CONSTEXPR char_type to_char_type(int_type __c) TEST_NOEXCEPT
48         {return char_type(__c);}
49 
to_int_typeconstexpr_char_traits50     static TEST_CONSTEXPR int_type  to_int_type(char_type __c) TEST_NOEXCEPT
51         {return int_type(__c);}
52 
eq_int_typeconstexpr_char_traits53     static TEST_CONSTEXPR bool      eq_int_type(int_type __c1, int_type __c2) TEST_NOEXCEPT
54         {return __c1 == __c2;}
55 
eofconstexpr_char_traits56     static TEST_CONSTEXPR int_type  eof() TEST_NOEXCEPT
57         {return int_type(EOF);}
58 };
59 
60 
61 template <class _CharT>
62 TEST_CONSTEXPR_CXX14 int
compare(const char_type * __s1,const char_type * __s2,size_t __n)63 constexpr_char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
64 {
65     for (; __n; --__n, ++__s1, ++__s2)
66     {
67         if (lt(*__s1, *__s2))
68             return -1;
69         if (lt(*__s2, *__s1))
70             return 1;
71     }
72     return 0;
73 }
74 
75 template <class _CharT>
76 TEST_CONSTEXPR_CXX14 size_t
length(const char_type * __s)77 constexpr_char_traits<_CharT>::length(const char_type* __s)
78 {
79     size_t __len = 0;
80     for (; !eq(*__s, char_type(0)); ++__s)
81         ++__len;
82     return __len;
83 }
84 
85 template <class _CharT>
86 TEST_CONSTEXPR_CXX14 const _CharT*
find(const char_type * __s,size_t __n,const char_type & __a)87 constexpr_char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
88 {
89     for (; __n; --__n)
90     {
91         if (eq(*__s, __a))
92             return __s;
93         ++__s;
94     }
95     return 0;
96 }
97 
98 template <class _CharT>
99 TEST_CONSTEXPR_CXX14 _CharT*
move(char_type * __s1,const char_type * __s2,size_t __n)100 constexpr_char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
101 {
102     char_type* __r = __s1;
103     if (__s1 < __s2)
104     {
105         for (; __n; --__n, ++__s1, ++__s2)
106             assign(*__s1, *__s2);
107     }
108     else if (__s2 < __s1)
109     {
110         __s1 += __n;
111         __s2 += __n;
112         for (; __n; --__n)
113             assign(*--__s1, *--__s2);
114     }
115     return __r;
116 }
117 
118 template <class _CharT>
119 TEST_CONSTEXPR_CXX14 _CharT*
copy(char_type * __s1,const char_type * __s2,size_t __n)120 constexpr_char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
121 {
122     assert(__s2 < __s1 || __s2 >= __s1+__n);
123     char_type* __r = __s1;
124     for (; __n; --__n, ++__s1, ++__s2)
125         assign(*__s1, *__s2);
126     return __r;
127 }
128 
129 template <class _CharT>
130 TEST_CONSTEXPR_CXX14 _CharT*
assign(char_type * __s,size_t __n,char_type __a)131 constexpr_char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
132 {
133     char_type* __r = __s;
134     for (; __n; --__n, ++__s)
135         assign(*__s, __a);
136     return __r;
137 }
138 
139 #endif // _CONSTEXPR_CHAR_TRAITS
140