1 /*
2 Copyright (c) Marshall Clow 2017-2017.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8 */
9
10 #include <new> // for placement new
11 #include <iostream>
12 #include <cstddef> // for NULL, std::size_t, std::ptrdiff_t
13 #include <cstring> // for std::strchr and std::strcmp
14 #include <cstdlib> // for std::malloc and std::free
15 #include <cstdio> // for EOF
16
17 #include <boost/config.hpp>
18 #include <boost/utility/string_view.hpp>
19
20 #if __cplusplus >= 201402L
21 struct constexpr_char_traits
22 {
23 typedef char char_type;
24 typedef int int_type;
25 typedef std::streamoff off_type;
26 typedef std::streampos pos_type;
27 typedef std::mbstate_t state_type;
28
assignconstexpr_char_traits29 static void assign(char_type& c1, const char_type& c2) noexcept { c1 = c2; }
eqconstexpr_char_traits30 static constexpr bool eq(char_type c1, char_type c2) noexcept { return c1 == c2; }
ltconstexpr_char_traits31 static constexpr bool lt(char_type c1, char_type c2) noexcept { return c1 < c2; }
32
33 static constexpr int compare(const char_type* s1, const char_type* s2, size_t n) noexcept;
34 static constexpr size_t length(const char_type* s) noexcept;
35 static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a) noexcept;
36 static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n) noexcept;
37 static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n) noexcept;
38 static constexpr char_type* assign(char_type* s, size_t n, char_type a) noexcept;
39
not_eofconstexpr_char_traits40 static constexpr int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? ~eof() : c; }
to_char_typeconstexpr_char_traits41 static constexpr char_type to_char_type(int_type c) noexcept { return char_type(c); }
to_int_typeconstexpr_char_traits42 static constexpr int_type to_int_type(char_type c) noexcept { return int_type(c); }
eq_int_typeconstexpr_char_traits43 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; }
eofconstexpr_char_traits44 static constexpr int_type eof() noexcept { return EOF; }
45 };
46
47 // yields:
48 // 0 if for each i in [0,n), X::eq(s1[i],s2[i]) is true;
49 // else, a negative value if, for some j in [0,n), X::lt(s1[j],s2[j]) is true and
50 // for each i in [0,j) X::eq(s2[i],s2[i]) is true;
51 // else a positive value.
compare(const char_type * s1,const char_type * s2,size_t n)52 constexpr int constexpr_char_traits::compare(const char_type* s1, const char_type* s2, size_t n) noexcept
53 {
54 for (; n != 0; --n, ++s1, ++s2)
55 {
56 if (lt(*s1, *s2))
57 return -1;
58 if (lt(*s2, *s1))
59 return 1;
60 }
61 return 0;
62 }
63
64 // yields: the smallest i such that X::eq(s[i],charT()) is true.
length(const char_type * s)65 constexpr size_t constexpr_char_traits::length(const char_type* s) noexcept
66 {
67 size_t len = 0;
68 for (; !eq(*s, char_type(0)); ++s)
69 ++len;
70 return len;
71 }
72
73 typedef boost::basic_string_view<char, constexpr_char_traits> string_view;
74
main()75 int main()
76 {
77 constexpr string_view sv1;
78 constexpr string_view sv2{"abc", 3}; // ptr, len
79 constexpr string_view sv3{"def"}; // ptr
80
81 constexpr const char *s1 = "";
82 constexpr const char *s2 = "abc";
83
84 static_assert( (sv1 == sv1), "" );
85
86 static_assert(!(sv1 == sv2), "" );
87 static_assert( (sv1 != sv2), "" );
88 static_assert( (sv1 < sv2), "" );
89 static_assert( (sv1 <= sv2), "" );
90 static_assert(!(sv1 > sv2), "" );
91 static_assert(!(sv1 >= sv2), "" );
92
93 static_assert(!(s1 == sv2), "" );
94 static_assert( (s1 != sv2), "" );
95 static_assert( (s1 < sv2), "" );
96 static_assert( (s1 <= sv2), "" );
97 static_assert(!(s1 > sv2), "" );
98 static_assert(!(s1 >= sv2), "" );
99
100 static_assert(!(sv1 == s2), "" );
101 static_assert( (sv1 != s2), "" );
102 static_assert( (sv1 < s2), "" );
103 static_assert( (sv1 <= s2), "" );
104 static_assert(!(sv1 > s2), "" );
105 static_assert(!(sv1 >= s2), "" );
106
107 static_assert( sv1.compare(sv2) < 0, "" );
108 static_assert( sv1.compare(sv1) == 0, "" );
109 static_assert( sv3.compare(sv1) > 0, "" );
110
111 static_assert( sv1.compare(s2) < 0, "" );
112 static_assert( sv1.compare(s1) == 0, "" );
113 static_assert( sv3.compare(s1) > 0, "" );
114 }
115 #endif
116