blob: 2136b26a0a8c535650b10c3e5e5d3c295c57d976 [file] [log] [blame]
Marshall Clow57e06df2014-06-06 22:33:40 +00001// -*- C++ -*-
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +00002//===-------------------- constexpr_char_traits ---------------------------===//
Marshall Clow57e06df2014-06-06 22:33:40 +00003//
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
Marshall Clow57e06df2014-06-06 22:33:40 +000014#include <string>
15
Eric Fiselierb530a252016-04-22 10:33:56 +000016#include "test_macros.h"
Marshall Clow57e06df2014-06-06 22:33:40 +000017
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
Eric Fiselierb530a252016-04-22 10:33:56 +000027 static TEST_CONSTEXPR_CXX14 void assign(char_type& __c1, const char_type& __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000028 {__c1 = __c2;}
29
Eric Fiselierb530a252016-04-22 10:33:56 +000030 static TEST_CONSTEXPR bool eq(char_type __c1, char_type __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000031 {return __c1 == __c2;}
32
Eric Fiselierb530a252016-04-22 10:33:56 +000033 static TEST_CONSTEXPR bool lt(char_type __c1, char_type __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000034 {return __c1 < __c2;}
35
Eric Fiselierb530a252016-04-22 10:33:56 +000036 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);
Marshall Clow57e06df2014-06-06 22:33:40 +000042
Eric Fiselierb530a252016-04-22 10:33:56 +000043 static TEST_CONSTEXPR int_type not_eof(int_type __c) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000044 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
45
Eric Fiselierb530a252016-04-22 10:33:56 +000046 static TEST_CONSTEXPR char_type to_char_type(int_type __c) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000047 {return char_type(__c);}
48
Eric Fiselierb530a252016-04-22 10:33:56 +000049 static TEST_CONSTEXPR int_type to_int_type(char_type __c) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000050 {return int_type(__c);}
51
Eric Fiselierb530a252016-04-22 10:33:56 +000052 static TEST_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000053 {return __c1 == __c2;}
54
Eric Fiselierb530a252016-04-22 10:33:56 +000055 static TEST_CONSTEXPR int_type eof() TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000056 {return int_type(EOF);}
57};
58
59
60template <class _CharT>
Eric Fiselierb530a252016-04-22 10:33:56 +000061TEST_CONSTEXPR_CXX14 int
Marshall Clow57e06df2014-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>
Eric Fiselierb530a252016-04-22 10:33:56 +000075TEST_CONSTEXPR_CXX14 size_t
Marshall Clow57e06df2014-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>
Eric Fiselierb530a252016-04-22 10:33:56 +000085TEST_CONSTEXPR_CXX14 const _CharT*
Marshall Clow57e06df2014-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>
Eric Fiselierb530a252016-04-22 10:33:56 +000098TEST_CONSTEXPR_CXX14 _CharT*
Marshall Clow57e06df2014-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>
Eric Fiselierb530a252016-04-22 10:33:56 +0000118TEST_CONSTEXPR_CXX14 _CharT*
Marshall Clow57e06df2014-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>
Eric Fiselierb530a252016-04-22 10:33:56 +0000129TEST_CONSTEXPR_CXX14 _CharT*
Marshall Clow57e06df2014-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 Fiseliercc2e1ab2015-02-10 17:32:49 +0000138#endif // _CONSTEXPR_CHAR_TRAITS