]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string_view/operations/compare/char/70483.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string_view / operations / compare / char / 70483.cc
1 // Copyright (C) 2017-2020 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++17" }
19 // { dg-do compile { target c++17 } }
20
21 #include <string_view>
22
23 struct constexpr_char_traits : std::char_traits<char>
24 {
25 static constexpr size_t
26 length(const char* val)
27 {
28 size_t res = 0;
29 for (; val[res] != '\0'; ++res)
30 ;
31 return res;
32 }
33
34 static constexpr int
35 compare(const char* lhs, const char* rhs, std::size_t count)
36 {
37 for (size_t pos = 0; pos < count; ++pos)
38 {
39 if (lhs[pos] != rhs[pos])
40 return lhs[pos] - rhs[pos];
41 }
42 return 0;
43 }
44 };
45
46 using string_view = std::basic_string_view<char, constexpr_char_traits>;
47
48 constexpr
49 string_view get()
50 {
51 string_view res = "x::";
52 string_view start_pattern = "x";
53 res = res.substr(res.find(start_pattern) + start_pattern.size());
54 res = res.substr(0, res.find_first_of(";]"));
55 res = res.substr(res.rfind("::"));
56 return res;
57 }
58
59 static_assert( get() == get() );
60
61 #ifdef _GLIBCXX_USE_CHAR8_T
62 using std::u8string_view;
63 #else
64 using u8string_view = std::basic_string_view<char>;
65 #endif
66
67 constexpr
68 u8string_view get8()
69 {
70 u8string_view res = u8"x::";
71 u8string_view start_pattern = u8"x";
72 res = res.substr(res.find(start_pattern) + start_pattern.size());
73 res = res.substr(0, res.find_first_of(u8";]"));
74 res = res.substr(res.rfind(u8"::"));
75 return res;
76 }
77
78 static_assert( get8() == get8() );
79
80 using std::u16string_view;
81
82 constexpr
83 u16string_view get16()
84 {
85 u16string_view res = u"x::";
86 u16string_view start_pattern = u"x";
87 res = res.substr(res.find(start_pattern) + start_pattern.size());
88 res = res.substr(0, res.find_first_of(u";]"));
89 res = res.substr(res.rfind(u"::"));
90 return res;
91 }
92
93 static_assert( get16() == get16() );
94
95 using std::u32string_view;
96
97 constexpr
98 u32string_view get32()
99 {
100 u32string_view res = U"x::";
101 u32string_view start_pattern = U"x";
102 res = res.substr(res.find(start_pattern) + start_pattern.size());
103 res = res.substr(0, res.find_first_of(U";]"));
104 res = res.substr(res.rfind(U"::"));
105 return res;
106 }
107
108 static_assert( get32() == get32() );