| 1 |
// Copyright (C) 2017-2025 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-do compile { target c++17 } } |
| 19 |
|
| 20 |
#include <string_view> |
| 21 |
|
| 22 |
struct constexpr_char_traits : std::char_traits<char> |
| 23 |
{ |
| 24 |
static constexpr size_t |
| 25 |
length(const char* val) |
| 26 |
{ |
| 27 |
size_t res = 0; |
| 28 |
for (; val[res] != '\0'; ++res) |
| 29 |
; |
| 30 |
return res; |
| 31 |
} |
| 32 |
|
| 33 |
static constexpr int |
| 34 |
compare(const char* lhs, const char* rhs, std::size_t count) |
| 35 |
{ |
| 36 |
for (size_t pos = 0; pos < count; ++pos) |
| 37 |
{ |
| 38 |
if (lhs[pos] != rhs[pos]) |
| 39 |
return lhs[pos] - rhs[pos]; |
| 40 |
} |
| 41 |
return 0; |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
using string_view = std::basic_string_view<char, constexpr_char_traits>; |
| 46 |
|
| 47 |
constexpr |
| 48 |
string_view get() |
| 49 |
{ |
| 50 |
string_view res = "x::"; |
| 51 |
string_view start_pattern = "x"; |
| 52 |
res = res.substr(res.find(start_pattern) + start_pattern.size()); |
| 53 |
res = res.substr(0, res.find_first_of(";]")); |
| 54 |
res = res.substr(res.rfind("::")); |
| 55 |
return res; |
| 56 |
} |
| 57 |
|
| 58 |
static_assert( get() == get() ); |
| 59 |
|
| 60 |
#ifdef _GLIBCXX_USE_CHAR8_T |
| 61 |
using std::u8string_view; |
| 62 |
#else |
| 63 |
using u8string_view = std::basic_string_view<char>; |
| 64 |
#endif |
| 65 |
|
| 66 |
constexpr |
| 67 |
u8string_view get8() |
| 68 |
{ |
| 69 |
u8string_view res = u8"x::"; |
| 70 |
u8string_view start_pattern = u8"x"; |
| 71 |
res = res.substr(res.find(start_pattern) + start_pattern.size()); |
| 72 |
res = res.substr(0, res.find_first_of(u8";]")); |
| 73 |
res = res.substr(res.rfind(u8"::")); |
| 74 |
return res; |
| 75 |
} |
| 76 |
|
| 77 |
static_assert( get8() == get8() ); |
| 78 |
|
| 79 |
using std::u16string_view; |
| 80 |
|
| 81 |
constexpr |
| 82 |
u16string_view get16() |
| 83 |
{ |
| 84 |
u16string_view res = u"x::"; |
| 85 |
u16string_view start_pattern = u"x"; |
| 86 |
res = res.substr(res.find(start_pattern) + start_pattern.size()); |
| 87 |
res = res.substr(0, res.find_first_of(u";]")); |
| 88 |
res = res.substr(res.rfind(u"::")); |
| 89 |
return res; |
| 90 |
} |
| 91 |
|
| 92 |
static_assert( get16() == get16() ); |
| 93 |
|
| 94 |
using std::u32string_view; |
| 95 |
|
| 96 |
constexpr |
| 97 |
u32string_view get32() |
| 98 |
{ |
| 99 |
u32string_view res = U"x::"; |
| 100 |
u32string_view start_pattern = U"x"; |
| 101 |
res = res.substr(res.find(start_pattern) + start_pattern.size()); |
| 102 |
res = res.substr(0, res.find_first_of(U";]")); |
| 103 |
res = res.substr(res.rfind(U"::")); |
| 104 |
return res; |
| 105 |
} |
| 106 |
|
| 107 |
static_assert( get32() == get32() ); |