Home | History | Annotate | Download | only in support
      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 <string>
     15 
     16 #include "test_macros.h"
     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 
     27     static TEST_CONSTEXPR_CXX14 void assign(char_type& __c1, const char_type& __c2) TEST_NOEXCEPT
     28         {__c1 = __c2;}
     29 
     30     static TEST_CONSTEXPR bool eq(char_type __c1, char_type __c2) TEST_NOEXCEPT
     31         {return __c1 == __c2;}
     32 
     33     static TEST_CONSTEXPR  bool lt(char_type __c1, char_type __c2) TEST_NOEXCEPT
     34         {return __c1 < __c2;}
     35 
     36     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);
     42 
     43     static TEST_CONSTEXPR int_type  not_eof(int_type __c) TEST_NOEXCEPT
     44         {return eq_int_type(__c, eof()) ? ~eof() : __c;}
     45 
     46     static TEST_CONSTEXPR char_type to_char_type(int_type __c) TEST_NOEXCEPT
     47         {return char_type(__c);}
     48 
     49     static TEST_CONSTEXPR int_type  to_int_type(char_type __c) TEST_NOEXCEPT
     50         {return int_type(__c);}
     51 
     52     static TEST_CONSTEXPR bool      eq_int_type(int_type __c1, int_type __c2) TEST_NOEXCEPT
     53         {return __c1 == __c2;}
     54 
     55     static TEST_CONSTEXPR int_type  eof() TEST_NOEXCEPT
     56         {return int_type(EOF);}
     57 };
     58 
     59 
     60 template <class _CharT>
     61 TEST_CONSTEXPR_CXX14 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 TEST_CONSTEXPR_CXX14 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 TEST_CONSTEXPR_CXX14 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 TEST_CONSTEXPR_CXX14 _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 TEST_CONSTEXPR_CXX14 _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 TEST_CONSTEXPR_CXX14 _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
    139