70483.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2017-2022 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. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License along
  13. // with this library; see the file COPYING3. If not see
  14. // <http://www.gnu.org/licenses/>.
  15. // { dg-options "-std=gnu++17" }
  16. // { dg-do compile { target c++17 } }
  17. #include <string_view>
  18. struct constexpr_char_traits : std::char_traits<char>
  19. {
  20. static constexpr size_t
  21. length(const char* val)
  22. {
  23. size_t res = 0;
  24. for (; val[res] != '\0'; ++res)
  25. ;
  26. return res;
  27. }
  28. static constexpr int
  29. compare(const char* lhs, const char* rhs, std::size_t count)
  30. {
  31. for (size_t pos = 0; pos < count; ++pos)
  32. {
  33. if (lhs[pos] != rhs[pos])
  34. return lhs[pos] - rhs[pos];
  35. }
  36. return 0;
  37. }
  38. };
  39. using string_view = std::basic_string_view<char, constexpr_char_traits>;
  40. constexpr
  41. string_view get()
  42. {
  43. string_view res = "x::";
  44. string_view start_pattern = "x";
  45. res = res.substr(res.find(start_pattern) + start_pattern.size());
  46. res = res.substr(0, res.find_first_of(";]"));
  47. res = res.substr(res.rfind("::"));
  48. return res;
  49. }
  50. static_assert( get() == get() );
  51. using std::u16string_view;
  52. constexpr
  53. u16string_view get16()
  54. {
  55. u16string_view res = u"x::";
  56. u16string_view start_pattern = u"x";
  57. res = res.substr(res.find(start_pattern) + start_pattern.size());
  58. res = res.substr(0, res.find_first_of(u";]"));
  59. res = res.substr(res.rfind(u"::"));
  60. return res;
  61. }
  62. static_assert( get16() == get16() );
  63. using std::u32string_view;
  64. constexpr
  65. u32string_view get32()
  66. {
  67. u32string_view res = U"x::";
  68. u32string_view start_pattern = U"x";
  69. res = res.substr(res.find(start_pattern) + start_pattern.size());
  70. res = res.substr(0, res.find_first_of(U";]"));
  71. res = res.substr(res.rfind(U"::"));
  72. return res;
  73. }
  74. static_assert( get32() == get32() );