string 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Components for manipulating sequences of characters -*- C++ -*-
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  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. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/string
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 21 Strings library
  25. //
  26. #ifndef _GLIBCXX_STRING
  27. #define _GLIBCXX_STRING 1
  28. #pragma GCC system_header
  29. #include <bits/c++config.h>
  30. #include <bits/stringfwd.h>
  31. #include <bits/char_traits.h> // NB: In turn includes stl_algobase.h
  32. #include <bits/allocator.h>
  33. #include <bits/cpp_type_traits.h>
  34. #include <bits/localefwd.h> // For operators >>, <<, and getline.
  35. #include <bits/ostream_insert.h>
  36. #include <bits/stl_iterator_base_types.h>
  37. #include <bits/stl_iterator_base_funcs.h>
  38. #include <bits/stl_iterator.h>
  39. #include <bits/stl_function.h> // For less
  40. #include <ext/numeric_traits.h>
  41. #include <bits/stl_algobase.h>
  42. #include <bits/refwrap.h>
  43. #include <bits/range_access.h>
  44. #include <bits/basic_string.h>
  45. #include <bits/basic_string.tcc>
  46. #if __cplusplus >= 201703L && _GLIBCXX_USE_CXX11_ABI
  47. namespace std _GLIBCXX_VISIBILITY(default)
  48. {
  49. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  50. namespace pmr {
  51. template<typename _Tp> class polymorphic_allocator;
  52. template<typename _CharT, typename _Traits = char_traits<_CharT>>
  53. using basic_string = std::basic_string<_CharT, _Traits,
  54. polymorphic_allocator<_CharT>>;
  55. using string = basic_string<char>;
  56. #ifdef _GLIBCXX_USE_CHAR8_T
  57. using u8string = basic_string<char8_t>;
  58. #endif
  59. using u16string = basic_string<char16_t>;
  60. using u32string = basic_string<char32_t>;
  61. using wstring = basic_string<wchar_t>;
  62. } // namespace pmr
  63. template<typename _Str>
  64. struct __hash_string_base
  65. : public __hash_base<size_t, _Str>
  66. {
  67. size_t
  68. operator()(const _Str& __s) const noexcept
  69. { return hash<basic_string_view<typename _Str::value_type>>{}(__s); }
  70. };
  71. template<>
  72. struct hash<pmr::string>
  73. : public __hash_string_base<pmr::string>
  74. { };
  75. #ifdef _GLIBCXX_USE_CHAR8_T
  76. template<>
  77. struct hash<pmr::u8string>
  78. : public __hash_string_base<pmr::u8string>
  79. { };
  80. #endif
  81. template<>
  82. struct hash<pmr::u16string>
  83. : public __hash_string_base<pmr::u16string>
  84. { };
  85. template<>
  86. struct hash<pmr::u32string>
  87. : public __hash_string_base<pmr::u32string>
  88. { };
  89. template<>
  90. struct hash<pmr::wstring>
  91. : public __hash_string_base<pmr::wstring>
  92. { };
  93. _GLIBCXX_END_NAMESPACE_VERSION
  94. } // namespace std
  95. #endif // C++17
  96. #if __cplusplus > 201703L
  97. namespace std _GLIBCXX_VISIBILITY(default)
  98. {
  99. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  100. #define __cpp_lib_erase_if 202002L
  101. template<typename _CharT, typename _Traits, typename _Alloc,
  102. typename _Predicate>
  103. _GLIBCXX20_CONSTEXPR
  104. inline typename basic_string<_CharT, _Traits, _Alloc>::size_type
  105. erase_if(basic_string<_CharT, _Traits, _Alloc>& __cont, _Predicate __pred)
  106. {
  107. using namespace __gnu_cxx;
  108. const auto __osz = __cont.size();
  109. const auto __end = __cont.end();
  110. auto __removed = std::__remove_if(__cont.begin(), __end,
  111. __ops::__pred_iter(std::ref(__pred)));
  112. __cont.erase(__removed, __end);
  113. return __osz - __cont.size();
  114. }
  115. template<typename _CharT, typename _Traits, typename _Alloc, typename _Up>
  116. _GLIBCXX20_CONSTEXPR
  117. inline typename basic_string<_CharT, _Traits, _Alloc>::size_type
  118. erase(basic_string<_CharT, _Traits, _Alloc>& __cont, const _Up& __value)
  119. {
  120. using namespace __gnu_cxx;
  121. const auto __osz = __cont.size();
  122. const auto __end = __cont.end();
  123. auto __removed = std::__remove_if(__cont.begin(), __end,
  124. __ops::__iter_equals_val(__value));
  125. __cont.erase(__removed, __end);
  126. return __osz - __cont.size();
  127. }
  128. _GLIBCXX_END_NAMESPACE_VERSION
  129. } // namespace std
  130. #endif // C++20
  131. #endif /* _GLIBCXX_STRING */