blob: b069c90076a2cb6c31200e94d9c07992ac209928 [file] [log] [blame]
Marshall Clow281918b2014-06-06 22:33:40 +00001// -*- 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
Dan Albert1d4a1ed2016-05-25 22:36:09 -070014#include <__config>
Marshall Clow281918b2014-06-06 22:33:40 +000015#include <string>
16
17
18template <class _CharT>
19struct constexpr_char_traits
20{
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;
26
Dan Albert1d4a1ed2016-05-25 22:36:09 -070027 static _LIBCPP_CONSTEXPR_AFTER_CXX11 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000028 {__c1 = __c2;}
29
Dan Albert1d4a1ed2016-05-25 22:36:09 -070030 static _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000031 {return __c1 == __c2;}
32
Dan Albert1d4a1ed2016-05-25 22:36:09 -070033 static _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000034 {return __c1 < __c2;}
35
Dan Albert1d4a1ed2016-05-25 22:36:09 -070036 static _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(const char_type* __s1, const char_type* __s2, size_t __n);
37 static _LIBCPP_CONSTEXPR_AFTER_CXX11 size_t length(const char_type* __s);
38 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
39 static _LIBCPP_CONSTEXPR_AFTER_CXX11 char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
40 static _LIBCPP_CONSTEXPR_AFTER_CXX11 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
41 static _LIBCPP_CONSTEXPR_AFTER_CXX11 char_type* assign(char_type* __s, size_t __n, char_type __a);
Marshall Clow281918b2014-06-06 22:33:40 +000042
Dan Albert1d4a1ed2016-05-25 22:36:09 -070043 static _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000044 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
45
Dan Albert1d4a1ed2016-05-25 22:36:09 -070046 static _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000047 {return char_type(__c);}
48
Dan Albert1d4a1ed2016-05-25 22:36:09 -070049 static _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000050 {return int_type(__c);}
51
Dan Albert1d4a1ed2016-05-25 22:36:09 -070052 static _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000053 {return __c1 == __c2;}
54
Dan Albert1d4a1ed2016-05-25 22:36:09 -070055 static _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
Marshall Clow281918b2014-06-06 22:33:40 +000056 {return int_type(EOF);}
57};
58
59
60template <class _CharT>
Dan Albert1d4a1ed2016-05-25 22:36:09 -070061_LIBCPP_CONSTEXPR_AFTER_CXX11 int
Marshall Clow281918b2014-06-06 22:33:40 +000062constexpr_char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
63{
64 for (; __n; --__n, ++__s1, ++__s2)
65 {
66 if (lt(*__s1, *__s2))
67 return -1;
68 if (lt(*__s2, *__s1))
69 return 1;
70 }
71 return 0;
72}
73
74template <class _CharT>
Dan Albert1d4a1ed2016-05-25 22:36:09 -070075_LIBCPP_CONSTEXPR_AFTER_CXX11 size_t
Marshall Clow281918b2014-06-06 22:33:40 +000076constexpr_char_traits<_CharT>::length(const char_type* __s)
77{
78 size_t __len = 0;
79 for (; !eq(*__s, char_type(0)); ++__s)
80 ++__len;
81 return __len;
82}
83
84template <class _CharT>
Dan Albert1d4a1ed2016-05-25 22:36:09 -070085_LIBCPP_CONSTEXPR_AFTER_CXX11 const _CharT*
Marshall Clow281918b2014-06-06 22:33:40 +000086constexpr_char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
87{
88 for (; __n; --__n)
89 {
90 if (eq(*__s, __a))
91 return __s;
92 ++__s;
93 }
94 return 0;
95}
96
97template <class _CharT>
Dan Albert1d4a1ed2016-05-25 22:36:09 -070098_LIBCPP_CONSTEXPR_AFTER_CXX11 _CharT*
Marshall Clow281918b2014-06-06 22:33:40 +000099constexpr_char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
100{
101 char_type* __r = __s1;
102 if (__s1 < __s2)
103 {
104 for (; __n; --__n, ++__s1, ++__s2)
105 assign(*__s1, *__s2);
106 }
107 else if (__s2 < __s1)
108 {
109 __s1 += __n;
110 __s2 += __n;
111 for (; __n; --__n)
112 assign(*--__s1, *--__s2);
113 }
114 return __r;
115}
116
117template <class _CharT>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700118_LIBCPP_CONSTEXPR_AFTER_CXX11 _CharT*
Marshall Clow281918b2014-06-06 22:33:40 +0000119constexpr_char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
120{
121 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
122 char_type* __r = __s1;
123 for (; __n; --__n, ++__s1, ++__s2)
124 assign(*__s1, *__s2);
125 return __r;
126}
127
128template <class _CharT>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700129_LIBCPP_CONSTEXPR_AFTER_CXX11 _CharT*
Marshall Clow281918b2014-06-06 22:33:40 +0000130constexpr_char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
131{
132 char_type* __r = __s;
133 for (; __n; --__n, ++__s)
134 assign(*__s, __a);
135 return __r;
136}
137
Eric Fiselier47b9a9a2015-02-10 17:32:49 +0000138#endif // _CONSTEXPR_CHAR_TRAITS