Marshall Clow | 281918b | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 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 <__config> |
| 15 | #include <string> |
| 16 | |
| 17 | |
| 18 | template <class _CharT> |
| 19 | struct 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 | |
Marshall Clow | 675dd5c | 2014-06-10 18:52:57 +0000 | [diff] [blame^] | 27 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT |
Marshall Clow | 281918b | 2014-06-06 22:33:40 +0000 | [diff] [blame] | 28 | {__c1 = __c2;} |
| 29 | |
| 30 | static _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT |
| 31 | {return __c1 == __c2;} |
| 32 | |
| 33 | static _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT |
| 34 | {return __c1 < __c2;} |
| 35 | |
| 36 | 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); |
| 42 | |
| 43 | static _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT |
| 44 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 45 | |
| 46 | static _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT |
| 47 | {return char_type(__c);} |
| 48 | |
| 49 | static _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT |
| 50 | {return int_type(__c);} |
| 51 | |
| 52 | static _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT |
| 53 | {return __c1 == __c2;} |
| 54 | |
| 55 | static _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT |
| 56 | {return int_type(EOF);} |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | template <class _CharT> |
| 61 | _LIBCPP_CONSTEXPR_AFTER_CXX11 int |
| 62 | constexpr_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 | |
| 74 | template <class _CharT> |
| 75 | _LIBCPP_CONSTEXPR_AFTER_CXX11 size_t |
| 76 | constexpr_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 | |
| 84 | template <class _CharT> |
| 85 | _LIBCPP_CONSTEXPR_AFTER_CXX11 const _CharT* |
| 86 | constexpr_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 | |
| 97 | template <class _CharT> |
| 98 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _CharT* |
| 99 | constexpr_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 | |
| 117 | template <class _CharT> |
| 118 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _CharT* |
| 119 | constexpr_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 | |
| 128 | template <class _CharT> |
| 129 | _LIBCPP_CONSTEXPR_AFTER_CXX11 _CharT* |
| 130 | constexpr_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 | |
| 138 | #endif // _CONSTEXPR_CHAR_TRAITS |