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
23struct 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
46using string_view = std::basic_string_view<char, constexpr_char_traits>;
47
48constexpr
49string_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
59static_assert( get() == get() );
60
61using std::u16string_view;
62
63constexpr
64u16string_view get16()
65{
66    u16string_view res = u"x::";
67    u16string_view start_pattern = u"x";
68    res = res.substr(res.find(start_pattern) + start_pattern.size());
69    res = res.substr(0, res.find_first_of(u";]"));
70    res = res.substr(res.rfind(u"::"));
71    return res;
72}
73
74static_assert( get16() == get16() );
75
76using std::u32string_view;
77
78constexpr
79u32string_view get32()
80{
81    u32string_view res = U"x::";
82    u32string_view start_pattern = U"x";
83    res = res.substr(res.find(start_pattern) + start_pattern.size());
84    res = res.substr(0, res.find_first_of(U";]"));
85    res = res.substr(res.rfind(U"::"));
86    return res;
87}
88
89static_assert( get32() == get32() );
90