string 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Components for manipulating sequences of characters -*- C++ -*-
  2. // Copyright (C) 1997-2019 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. #if __cplusplus > 201703L
  43. # include <bits/stl_algo.h> // For remove and remove_if
  44. #endif // C++20
  45. #include <bits/range_access.h>
  46. #include <bits/basic_string.h>
  47. #include <bits/basic_string.tcc>
  48. #if __cplusplus >= 201703L && _GLIBCXX_USE_CXX11_ABI
  49. namespace std _GLIBCXX_VISIBILITY(default)
  50. {
  51. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  52. namespace pmr {
  53. template<typename _Tp> class polymorphic_allocator;
  54. template<typename _CharT, typename _Traits = char_traits<_CharT>>
  55. using basic_string = std::basic_string<_CharT, _Traits,
  56. polymorphic_allocator<_CharT>>;
  57. using string = basic_string<char>;
  58. #ifdef _GLIBCXX_USE_CHAR8_T
  59. using u8string = basic_string<char8_t>;
  60. #endif
  61. using u16string = basic_string<char16_t>;
  62. using u32string = basic_string<char32_t>;
  63. #ifdef _GLIBCXX_USE_WCHAR_T
  64. using wstring = basic_string<wchar_t>;
  65. #endif
  66. } // namespace pmr
  67. template<typename _Str>
  68. struct __hash_string_base
  69. : public __hash_base<size_t, _Str>
  70. {
  71. size_t
  72. operator()(const _Str& __s) const noexcept
  73. { return hash<basic_string_view<typename _Str::value_type>>{}(__s); }
  74. };
  75. template<>
  76. struct hash<pmr::string>
  77. : public __hash_string_base<pmr::string>
  78. { };
  79. #ifdef _GLIBCXX_USE_CHAR8_T
  80. template<>
  81. struct hash<pmr::u8string>
  82. : public __hash_string_base<pmr::u8string>
  83. { };
  84. #endif
  85. template<>
  86. struct hash<pmr::u16string>
  87. : public __hash_string_base<pmr::u16string>
  88. { };
  89. template<>
  90. struct hash<pmr::u32string>
  91. : public __hash_string_base<pmr::u32string>
  92. { };
  93. #ifdef _GLIBCXX_USE_WCHAR_T
  94. template<>
  95. struct hash<pmr::wstring>
  96. : public __hash_string_base<pmr::wstring>
  97. { };
  98. #endif
  99. _GLIBCXX_END_NAMESPACE_VERSION
  100. } // namespace std
  101. #endif // C++17
  102. #if __cplusplus > 201703L
  103. namespace std _GLIBCXX_VISIBILITY(default)
  104. {
  105. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  106. #define __cpp_lib_erase_if 201900L
  107. template<typename _CharT, typename _Traits, typename _Alloc,
  108. typename _Predicate>
  109. inline typename basic_string<_CharT, _Traits, _Alloc>::size_type
  110. erase_if(basic_string<_CharT, _Traits, _Alloc>& __cont, _Predicate __pred)
  111. {
  112. const auto __osz = __cont.size();
  113. __cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred),
  114. __cont.end());
  115. return __osz - __cont.size();
  116. }
  117. template<typename _CharT, typename _Traits, typename _Alloc, typename _Up>
  118. inline typename basic_string<_CharT, _Traits, _Alloc>::size_type
  119. erase(basic_string<_CharT, _Traits, _Alloc>& __cont, const _Up& __value)
  120. {
  121. const auto __osz = __cont.size();
  122. __cont.erase(std::remove(__cont.begin(), __cont.end(), __value),
  123. __cont.end());
  124. return __osz - __cont.size();
  125. }
  126. _GLIBCXX_END_NAMESPACE_VERSION
  127. } // namespace std
  128. #endif // C++20
  129. #endif /* _GLIBCXX_STRING */