blob: 7508567ca19942703a368d4ba677aa00de8dafa3 [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>
Eric Fiselierdd3ba792017-02-17 01:17:10 +000015#include <cassert>
Marshall Clow57e06df2014-06-06 22:33:40 +000016
Eric Fiselierb530a252016-04-22 10:33:56 +000017#include "test_macros.h"
Marshall Clow57e06df2014-06-06 22:33:40 +000018
19template <class _CharT>
20struct 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
Eric Fiselierb530a252016-04-22 10:33:56 +000028 static TEST_CONSTEXPR_CXX14 void assign(char_type& __c1, const char_type& __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000029 {__c1 = __c2;}
30
Eric Fiselierb530a252016-04-22 10:33:56 +000031 static TEST_CONSTEXPR bool eq(char_type __c1, char_type __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000032 {return __c1 == __c2;}
33
Eric Fiselierb530a252016-04-22 10:33:56 +000034 static TEST_CONSTEXPR bool lt(char_type __c1, char_type __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000035 {return __c1 < __c2;}
36
Eric Fiselierb530a252016-04-22 10:33:56 +000037 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);
Marshall Clow57e06df2014-06-06 22:33:40 +000043
Eric Fiselierb530a252016-04-22 10:33:56 +000044 static TEST_CONSTEXPR int_type not_eof(int_type __c) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000045 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
46
Eric Fiselierb530a252016-04-22 10:33:56 +000047 static TEST_CONSTEXPR char_type to_char_type(int_type __c) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000048 {return char_type(__c);}
49
Eric Fiselierb530a252016-04-22 10:33:56 +000050 static TEST_CONSTEXPR int_type to_int_type(char_type __c) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000051 {return int_type(__c);}
52
Eric Fiselierb530a252016-04-22 10:33:56 +000053 static TEST_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000054 {return __c1 == __c2;}
55
Eric Fiselierb530a252016-04-22 10:33:56 +000056 static TEST_CONSTEXPR int_type eof() TEST_NOEXCEPT
Marshall Clow57e06df2014-06-06 22:33:40 +000057 {return int_type(EOF);}
58};
59
60
61template <class _CharT>
Eric Fiselierb530a252016-04-22 10:33:56 +000062TEST_CONSTEXPR_CXX14 int
Marshall Clow57e06df2014-06-06 22:33:40 +000063constexpr_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
75template <class _CharT>
Eric Fiselierb530a252016-04-22 10:33:56 +000076TEST_CONSTEXPR_CXX14 size_t
Marshall Clow57e06df2014-06-06 22:33:40 +000077constexpr_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
85template <class _CharT>
Eric Fiselierb530a252016-04-22 10:33:56 +000086TEST_CONSTEXPR_CXX14 const _CharT*
Marshall Clow57e06df2014-06-06 22:33:40 +000087constexpr_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
98template <class _CharT>
Eric Fiselierb530a252016-04-22 10:33:56 +000099TEST_CONSTEXPR_CXX14 _CharT*
Marshall Clow57e06df2014-06-06 22:33:40 +0000100constexpr_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
118template <class _CharT>
Eric Fiselierb530a252016-04-22 10:33:56 +0000119TEST_CONSTEXPR_CXX14 _CharT*
Marshall Clow57e06df2014-06-06 22:33:40 +0000120constexpr_char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
121{
Eric Fiselierdd3ba792017-02-17 01:17:10 +0000122 assert(__s2 < __s1 || __s2 >= __s1+__n);
Marshall Clow57e06df2014-06-06 22:33:40 +0000123 char_type* __r = __s1;
124 for (; __n; --__n, ++__s1, ++__s2)
125 assign(*__s1, *__s2);
126 return __r;
127}
128
129template <class _CharT>
Eric Fiselierb530a252016-04-22 10:33:56 +0000130TEST_CONSTEXPR_CXX14 _CharT*
Marshall Clow57e06df2014-06-06 22:33:40 +0000131constexpr_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
Eric Fiseliercc2e1ab2015-02-10 17:32:49 +0000139#endif // _CONSTEXPR_CHAR_TRAITS