blob: 615d600ca4bfe69bbeb3d57156f7d53400f535b7 [file] [log] [blame]
Brian Silverman60e3e2a2018-08-04 23:57:12 -07001/*
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
16#include <boost/config.hpp>
17#include <boost/utility/string_view.hpp>
18
19#if __cplusplus >= 201402L
20struct constexpr_char_traits
21{
22 typedef char 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
28 static void assign(char_type& c1, const char_type& c2) noexcept { c1 = c2; }
29 static constexpr bool eq(char_type c1, char_type c2) noexcept { return c1 == c2; }
30 static constexpr bool lt(char_type c1, char_type c2) noexcept { return c1 < c2; }
31
32 static constexpr int compare(const char_type* s1, const char_type* s2, size_t n) noexcept;
33 static constexpr size_t length(const char_type* s) noexcept;
34 static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a) noexcept;
35 static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n) noexcept;
36 static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n) noexcept;
37 static constexpr char_type* assign(char_type* s, size_t n, char_type a) noexcept;
38
39 static constexpr int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? ~eof() : c; }
40 static constexpr char_type to_char_type(int_type c) noexcept { return char_type(c); }
41 static constexpr int_type to_int_type(char_type c) noexcept { return int_type(c); }
42 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; }
43 static constexpr int_type eof() noexcept { return EOF; }
44};
45
46// yields:
47// 0 if for each i in [0,n), X::eq(s1[i],s2[i]) is true;
48// else, a negative value if, for some j in [0,n), X::lt(s1[j],s2[j]) is true and
49// for each i in [0,j) X::eq(s2[i],s2[i]) is true;
50// else a positive value.
51constexpr int constexpr_char_traits::compare(const char_type* s1, const char_type* s2, size_t n) noexcept
52{
53 for (; n != 0; --n, ++s1, ++s2)
54 {
55 if (lt(*s1, *s2))
56 return -1;
57 if (lt(*s2, *s1))
58 return 1;
59 }
60 return 0;
61}
62
63// yields: the smallest i such that X::eq(s[i],charT()) is true.
64constexpr size_t constexpr_char_traits::length(const char_type* s) noexcept
65{
66 size_t len = 0;
67 for (; !eq(*s, char_type(0)); ++s)
68 ++len;
69 return len;
70}
71
72typedef boost::basic_string_view<char, constexpr_char_traits> string_view;
73
74int main()
75{
76 constexpr string_view sv1;
77 constexpr string_view sv2{"abc", 3}; // ptr, len
78 constexpr string_view sv3{"def"}; // ptr
79
80 constexpr const char *s1 = "";
81 constexpr const char *s2 = "abc";
82
83 static_assert( (sv1 == sv1), "" );
84
85 static_assert(!(sv1 == sv2), "" );
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
92 static_assert(!(s1 == sv2), "" );
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
99 static_assert(!(sv1 == s2), "" );
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
106 static_assert( sv1.compare(sv2) < 0, "" );
107 static_assert( sv1.compare(sv1) == 0, "" );
108 static_assert( sv3.compare(sv1) > 0, "" );
109
110 static_assert( sv1.compare(s2) < 0, "" );
111 static_assert( sv1.compare(s1) == 0, "" );
112 static_assert( sv3.compare(s1) > 0, "" );
113}
114#endif