Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Stephan T. Lavavej | a730ed3 | 2017-01-18 20:10:25 +0000 | [diff] [blame] | 2 | //===-------------------- constexpr_char_traits ---------------------------===// |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 14 | #include <string> |
Eric Fiselier | dd3ba79 | 2017-02-17 01:17:10 +0000 | [diff] [blame] | 15 | #include <cassert> |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 16 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 17 | #include "test_macros.h" |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 28 | static TEST_CONSTEXPR_CXX14 void assign(char_type& __c1, const char_type& __c2) TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 29 | {__c1 = __c2;} |
| 30 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 31 | static TEST_CONSTEXPR bool eq(char_type __c1, char_type __c2) TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 32 | {return __c1 == __c2;} |
| 33 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 34 | static TEST_CONSTEXPR bool lt(char_type __c1, char_type __c2) TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 35 | {return __c1 < __c2;} |
| 36 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 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); |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 43 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 44 | static TEST_CONSTEXPR int_type not_eof(int_type __c) TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 45 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 46 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 47 | static TEST_CONSTEXPR char_type to_char_type(int_type __c) TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 48 | {return char_type(__c);} |
| 49 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 50 | static TEST_CONSTEXPR int_type to_int_type(char_type __c) TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 51 | {return int_type(__c);} |
| 52 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 53 | static TEST_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 54 | {return __c1 == __c2;} |
| 55 | |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 56 | static TEST_CONSTEXPR int_type eof() TEST_NOEXCEPT |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 57 | {return int_type(EOF);} |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | template <class _CharT> |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 62 | TEST_CONSTEXPR_CXX14 int |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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> |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 76 | TEST_CONSTEXPR_CXX14 size_t |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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> |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 86 | TEST_CONSTEXPR_CXX14 const _CharT* |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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> |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 99 | TEST_CONSTEXPR_CXX14 _CharT* |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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> |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 119 | TEST_CONSTEXPR_CXX14 _CharT* |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 120 | constexpr_char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n) |
| 121 | { |
Eric Fiselier | dd3ba79 | 2017-02-17 01:17:10 +0000 | [diff] [blame] | 122 | assert(__s2 < __s1 || __s2 >= __s1+__n); |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 123 | char_type* __r = __s1; |
| 124 | for (; __n; --__n, ++__s1, ++__s2) |
| 125 | assign(*__s1, *__s2); |
| 126 | return __r; |
| 127 | } |
| 128 | |
| 129 | template <class _CharT> |
Eric Fiselier | b530a25 | 2016-04-22 10:33:56 +0000 | [diff] [blame] | 130 | TEST_CONSTEXPR_CXX14 _CharT* |
Marshall Clow | 57e06df | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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 | |
Eric Fiselier | cc2e1ab | 2015-02-10 17:32:49 +0000 | [diff] [blame] | 139 | #endif // _CONSTEXPR_CHAR_TRAITS |