string_piece.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // A string-like object that points to a sized piece of memory.
  6. //
  7. // You can use StringPiece as a function or method parameter. A StringPiece
  8. // parameter can receive a double-quoted string literal argument, a "const
  9. // char*" argument, a string argument, or a StringPiece argument with no data
  10. // copying. Systematic use of StringPiece for arguments reduces data
  11. // copies and strlen() calls.
  12. //
  13. // Prefer passing StringPieces by value:
  14. // void MyFunction(StringPiece arg);
  15. // If circumstances require, you may also pass by const reference:
  16. // void MyFunction(const StringPiece& arg); // not preferred
  17. // Both of these have the same lifetime semantics. Passing by value
  18. // generates slightly smaller code. For more discussion, Googlers can see
  19. // the thread go/stringpiecebyvalue on c-users.
  20. #ifndef BASE_STRINGS_STRING_PIECE_H_
  21. #define BASE_STRINGS_STRING_PIECE_H_
  22. #include <stddef.h>
  23. #include <algorithm>
  24. #include <iosfwd>
  25. #include <limits>
  26. #include <string>
  27. #include <type_traits>
  28. #include "base/base_export.h"
  29. #include "base/check.h"
  30. #include "base/check_op.h"
  31. #include "base/compiler_specific.h"
  32. #include "base/strings/string_piece_forward.h" // IWYU pragma: export
  33. #include "build/build_config.h"
  34. namespace base {
  35. // internal --------------------------------------------------------------------
  36. // Many of the StringPiece functions use different implementations for the
  37. // 8-bit and 16-bit versions, and we don't want lots of template expansions in
  38. // this (very common) header that will slow down compilation.
  39. //
  40. // So here we define overloaded functions called by the StringPiece template.
  41. // For those that share an implementation, the two versions will expand to a
  42. // template internal to the .cc file.
  43. namespace internal {
  44. BASE_EXPORT size_t find(StringPiece self, StringPiece s, size_t pos);
  45. BASE_EXPORT size_t find(StringPiece16 self, StringPiece16 s, size_t pos);
  46. BASE_EXPORT size_t rfind(StringPiece self, StringPiece s, size_t pos);
  47. BASE_EXPORT size_t rfind(StringPiece16 self, StringPiece16 s, size_t pos);
  48. BASE_EXPORT size_t find_first_of(StringPiece self, StringPiece s, size_t pos);
  49. BASE_EXPORT size_t find_first_of(StringPiece16 self,
  50. StringPiece16 s,
  51. size_t pos);
  52. BASE_EXPORT size_t find_first_not_of(StringPiece self,
  53. StringPiece s,
  54. size_t pos);
  55. BASE_EXPORT size_t find_first_not_of(StringPiece16 self,
  56. StringPiece16 s,
  57. size_t pos);
  58. BASE_EXPORT size_t find_last_of(StringPiece self, StringPiece s, size_t pos);
  59. BASE_EXPORT size_t find_last_of(StringPiece16 self,
  60. StringPiece16 s,
  61. size_t pos);
  62. BASE_EXPORT size_t find_last_not_of(StringPiece self,
  63. StringPiece s,
  64. size_t pos);
  65. BASE_EXPORT size_t find_last_not_of(StringPiece16 self,
  66. StringPiece16 s,
  67. size_t pos);
  68. BASE_EXPORT size_t find(WStringPiece self, WStringPiece s, size_t pos);
  69. BASE_EXPORT size_t rfind(WStringPiece self, WStringPiece s, size_t pos);
  70. BASE_EXPORT size_t find_first_of(WStringPiece self, WStringPiece s, size_t pos);
  71. BASE_EXPORT size_t find_first_not_of(WStringPiece self,
  72. WStringPiece s,
  73. size_t pos);
  74. BASE_EXPORT size_t find_last_of(WStringPiece self, WStringPiece s, size_t pos);
  75. BASE_EXPORT size_t find_last_not_of(WStringPiece self,
  76. WStringPiece s,
  77. size_t pos);
  78. } // namespace internal
  79. // BasicStringPiece ------------------------------------------------------------
  80. // Mirrors the C++17 version of std::basic_string_view<> as closely as possible,
  81. // except where noted below.
  82. template <typename CharT, typename Traits>
  83. class GSL_POINTER BasicStringPiece {
  84. public:
  85. using traits_type = Traits;
  86. using value_type = CharT;
  87. using pointer = CharT*;
  88. using const_pointer = const CharT*;
  89. using reference = CharT&;
  90. using const_reference = const CharT&;
  91. using const_iterator = const CharT*;
  92. using iterator = const_iterator;
  93. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  94. using reverse_iterator = const_reverse_iterator;
  95. using size_type = size_t;
  96. using difference_type = ptrdiff_t;
  97. constexpr BasicStringPiece() noexcept : ptr_(nullptr), length_(0) {}
  98. constexpr BasicStringPiece(const BasicStringPiece& other) noexcept = default;
  99. constexpr BasicStringPiece& operator=(const BasicStringPiece& view) noexcept =
  100. default;
  101. constexpr BasicStringPiece(const CharT* s, size_type count)
  102. : ptr_(s), length_(count) {}
  103. constexpr BasicStringPiece(const CharT* s)
  104. : ptr_(s), length_(s ? traits_type::length(s) : 0) {
  105. // Intentional STL deviation: Null-check instead of UB.
  106. CHECK(s);
  107. }
  108. // Explicitly disallow construction from nullptr. Note that this does not
  109. // catch construction from runtime strings that might be null.
  110. // Note: The following is just a more elaborate way of spelling
  111. // `BasicStringPiece(nullptr_t) = delete`, but unfortunately the terse form is
  112. // not supported by the PNaCl toolchain.
  113. template <class T, class = std::enable_if_t<std::is_null_pointer<T>::value>>
  114. BasicStringPiece(T) {
  115. static_assert(sizeof(T) == 0, // Always false.
  116. "StringPiece does not support construction from nullptr, use "
  117. "the default constructor instead.");
  118. }
  119. // These are necessary because std::basic_string provides construction from
  120. // (an object convertible to) a std::basic_string_view, as well as an explicit
  121. // cast operator to a std::basic_string_view, but (obviously) not from/to a
  122. // BasicStringPiece.
  123. BasicStringPiece(const std::basic_string<CharT>& str)
  124. : ptr_(str.data()), length_(str.size()) {}
  125. explicit operator std::basic_string<CharT>() const {
  126. return std::basic_string<CharT>(data(), size());
  127. }
  128. constexpr const_iterator begin() const noexcept { return ptr_; }
  129. constexpr const_iterator cbegin() const noexcept { return ptr_; }
  130. constexpr const_iterator end() const noexcept { return ptr_ + length_; }
  131. constexpr const_iterator cend() const noexcept { return ptr_ + length_; }
  132. constexpr const_reverse_iterator rbegin() const noexcept {
  133. return const_reverse_iterator(ptr_ + length_);
  134. }
  135. constexpr const_reverse_iterator crbegin() const noexcept {
  136. return const_reverse_iterator(ptr_ + length_);
  137. }
  138. constexpr const_reverse_iterator rend() const noexcept {
  139. return const_reverse_iterator(ptr_);
  140. }
  141. constexpr const_reverse_iterator crend() const noexcept {
  142. return const_reverse_iterator(ptr_);
  143. }
  144. constexpr const_reference operator[](size_type pos) const {
  145. // Intentional STL deviation: Bounds-check instead of UB.
  146. return at(pos);
  147. }
  148. constexpr const_reference at(size_type pos) const {
  149. CHECK_LT(pos, size());
  150. return data()[pos];
  151. }
  152. constexpr const_reference front() const { return operator[](0); }
  153. constexpr const_reference back() const { return operator[](size() - 1); }
  154. constexpr const_pointer data() const noexcept { return ptr_; }
  155. constexpr size_type size() const noexcept { return length_; }
  156. constexpr size_type length() const noexcept { return length_; }
  157. constexpr size_type max_size() const {
  158. return std::numeric_limits<size_type>::max() / sizeof(CharT);
  159. }
  160. [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
  161. constexpr void remove_prefix(size_type n) {
  162. // Intentional STL deviation: Bounds-check instead of UB.
  163. CHECK_LE(n, size());
  164. ptr_ += n;
  165. length_ -= n;
  166. }
  167. constexpr void remove_suffix(size_type n) {
  168. // Intentional STL deviation: Bounds-check instead of UB.
  169. CHECK_LE(n, size());
  170. length_ -= n;
  171. }
  172. constexpr void swap(BasicStringPiece& v) noexcept {
  173. // Note: Cannot use std::swap() since it is not constexpr until C++20.
  174. const const_pointer ptr = ptr_;
  175. ptr_ = v.ptr_;
  176. v.ptr_ = ptr;
  177. const size_type length = length_;
  178. length_ = v.length_;
  179. v.length_ = length;
  180. }
  181. constexpr size_type copy(CharT* dest,
  182. size_type count,
  183. size_type pos = 0) const {
  184. CHECK_LE(pos, size());
  185. const size_type rcount = std::min(count, size() - pos);
  186. traits_type::copy(dest, data() + pos, rcount);
  187. return rcount;
  188. }
  189. constexpr BasicStringPiece substr(size_type pos = 0,
  190. size_type count = npos) const {
  191. CHECK_LE(pos, size());
  192. const size_type rcount = std::min(count, size() - pos);
  193. return {data() + pos, rcount};
  194. }
  195. constexpr int compare(BasicStringPiece v) const noexcept {
  196. const size_type rlen = std::min(size(), v.size());
  197. const int result = traits_type::compare(data(), v.data(), rlen);
  198. if (result != 0)
  199. return result;
  200. if (size() == v.size())
  201. return 0;
  202. return size() < v.size() ? -1 : 1;
  203. }
  204. constexpr int compare(size_type pos1,
  205. size_type count1,
  206. BasicStringPiece v) const {
  207. return substr(pos1, count1).compare(v);
  208. }
  209. constexpr int compare(size_type pos1,
  210. size_type count1,
  211. BasicStringPiece v,
  212. size_type pos2,
  213. size_type count2) const {
  214. return substr(pos1, count1).compare(v.substr(pos2, count2));
  215. }
  216. constexpr int compare(const CharT* s) const {
  217. return compare(BasicStringPiece(s));
  218. }
  219. constexpr int compare(size_type pos1,
  220. size_type count1,
  221. const CharT* s) const {
  222. return substr(pos1, count1).compare(BasicStringPiece(s));
  223. }
  224. constexpr int compare(size_type pos1,
  225. size_type count1,
  226. const CharT* s,
  227. size_type count2) const {
  228. return substr(pos1, count1).compare(BasicStringPiece(s, count2));
  229. }
  230. constexpr size_type find(BasicStringPiece v,
  231. size_type pos = 0) const noexcept {
  232. if (is_constant_evaluated()) {
  233. if (v.size() > size())
  234. return npos;
  235. for (size_type p = pos; p <= size() - v.size(); ++p) {
  236. if (!compare(p, v.size(), v))
  237. return p;
  238. }
  239. return npos;
  240. }
  241. return internal::find(*this, v, pos);
  242. }
  243. constexpr size_type find(CharT ch, size_type pos = 0) const noexcept {
  244. if (pos >= size())
  245. return npos;
  246. const const_pointer result =
  247. traits_type::find(data() + pos, size() - pos, ch);
  248. return result ? static_cast<size_type>(result - data()) : npos;
  249. }
  250. constexpr size_type find(const CharT* s,
  251. size_type pos,
  252. size_type count) const {
  253. return find(BasicStringPiece(s, count), pos);
  254. }
  255. constexpr size_type find(const CharT* s, size_type pos = 0) const {
  256. return find(BasicStringPiece(s), pos);
  257. }
  258. constexpr size_type rfind(BasicStringPiece v,
  259. size_type pos = npos) const noexcept {
  260. if (is_constant_evaluated()) {
  261. if (v.size() > size())
  262. return npos;
  263. for (size_type p = std::min(size() - v.size(), pos);; --p) {
  264. if (!compare(p, v.size(), v))
  265. return p;
  266. if (!p)
  267. break;
  268. }
  269. return npos;
  270. }
  271. return internal::rfind(*this, v, pos);
  272. }
  273. constexpr size_type rfind(CharT c, size_type pos = npos) const noexcept {
  274. if (empty())
  275. return npos;
  276. for (size_t i = std::min(pos, size() - 1);; --i) {
  277. if (data()[i] == c)
  278. return i;
  279. if (i == 0)
  280. break;
  281. }
  282. return npos;
  283. }
  284. constexpr size_type rfind(const CharT* s,
  285. size_type pos,
  286. size_type count) const {
  287. return rfind(BasicStringPiece(s, count), pos);
  288. }
  289. constexpr size_type rfind(const CharT* s, size_type pos = npos) const {
  290. return rfind(BasicStringPiece(s), pos);
  291. }
  292. constexpr size_type find_first_of(BasicStringPiece v,
  293. size_type pos = 0) const noexcept {
  294. if (is_constant_evaluated()) {
  295. if (empty() || v.empty())
  296. return npos;
  297. for (size_type p = pos; p < size(); ++p) {
  298. if (v.find(data()[p]) != npos)
  299. return p;
  300. }
  301. return npos;
  302. }
  303. return internal::find_first_of(*this, v, pos);
  304. }
  305. constexpr size_type find_first_of(CharT c, size_type pos = 0) const noexcept {
  306. return find(c, pos);
  307. }
  308. constexpr size_type find_first_of(const CharT* s,
  309. size_type pos,
  310. size_type count) const {
  311. return find_first_of(BasicStringPiece(s, count), pos);
  312. }
  313. constexpr size_type find_first_of(const CharT* s, size_type pos = 0) const {
  314. return find_first_of(BasicStringPiece(s), pos);
  315. }
  316. constexpr size_type find_last_of(BasicStringPiece v,
  317. size_type pos = npos) const noexcept {
  318. if (is_constant_evaluated()) {
  319. if (empty() || v.empty())
  320. return npos;
  321. for (size_type p = std::min(pos, size() - 1);; --p) {
  322. if (v.find(data()[p]) != npos)
  323. return p;
  324. if (!p)
  325. break;
  326. }
  327. return npos;
  328. }
  329. return internal::find_last_of(*this, v, pos);
  330. }
  331. constexpr size_type find_last_of(CharT c,
  332. size_type pos = npos) const noexcept {
  333. return rfind(c, pos);
  334. }
  335. constexpr size_type find_last_of(const CharT* s,
  336. size_type pos,
  337. size_type count) const {
  338. return find_last_of(BasicStringPiece(s, count), pos);
  339. }
  340. constexpr size_type find_last_of(const CharT* s, size_type pos = npos) const {
  341. return find_last_of(BasicStringPiece(s), pos);
  342. }
  343. constexpr size_type find_first_not_of(BasicStringPiece v,
  344. size_type pos = 0) const noexcept {
  345. if (is_constant_evaluated()) {
  346. if (empty())
  347. return npos;
  348. for (size_type p = pos; p < size(); ++p) {
  349. if (v.find(data()[p]) == npos)
  350. return p;
  351. }
  352. return npos;
  353. }
  354. return internal::find_first_not_of(*this, v, pos);
  355. }
  356. constexpr size_type find_first_not_of(CharT c,
  357. size_type pos = 0) const noexcept {
  358. if (empty())
  359. return npos;
  360. for (; pos < size(); ++pos) {
  361. if (data()[pos] != c)
  362. return pos;
  363. }
  364. return npos;
  365. }
  366. constexpr size_type find_first_not_of(const CharT* s,
  367. size_type pos,
  368. size_type count) const {
  369. return find_first_not_of(BasicStringPiece(s, count), pos);
  370. }
  371. constexpr size_type find_first_not_of(const CharT* s,
  372. size_type pos = 0) const {
  373. return find_first_not_of(BasicStringPiece(s), pos);
  374. }
  375. constexpr size_type find_last_not_of(BasicStringPiece v,
  376. size_type pos = npos) const noexcept {
  377. if (is_constant_evaluated()) {
  378. if (empty())
  379. return npos;
  380. for (size_type p = std::min(pos, size() - 1);; --p) {
  381. if (v.find(data()[p]) == npos)
  382. return p;
  383. if (!p)
  384. break;
  385. }
  386. return npos;
  387. }
  388. return internal::find_last_not_of(*this, v, pos);
  389. }
  390. constexpr size_type find_last_not_of(CharT c,
  391. size_type pos = npos) const noexcept {
  392. if (empty())
  393. return npos;
  394. for (size_t i = std::min(pos, size() - 1);; --i) {
  395. if (data()[i] != c)
  396. return i;
  397. if (i == 0)
  398. break;
  399. }
  400. return npos;
  401. }
  402. constexpr size_type find_last_not_of(const CharT* s,
  403. size_type pos,
  404. size_type count) const {
  405. return find_last_not_of(BasicStringPiece(s, count), pos);
  406. }
  407. constexpr size_type find_last_not_of(const CharT* s,
  408. size_type pos = npos) const {
  409. return find_last_not_of(BasicStringPiece(s), pos);
  410. }
  411. static constexpr size_type npos = size_type(-1);
  412. protected:
  413. const_pointer ptr_;
  414. size_type length_;
  415. };
  416. // static
  417. template <typename CharT, typename Traits>
  418. const typename BasicStringPiece<CharT, Traits>::size_type
  419. BasicStringPiece<CharT, Traits>::npos;
  420. // MSVC doesn't like complex extern templates and DLLs.
  421. #if !defined(COMPILER_MSVC)
  422. extern template class BASE_EXPORT BasicStringPiece<char>;
  423. extern template class BASE_EXPORT BasicStringPiece<char16_t>;
  424. #endif
  425. template <typename CharT, typename Traits>
  426. constexpr bool operator==(BasicStringPiece<CharT, Traits> lhs,
  427. BasicStringPiece<CharT, Traits> rhs) noexcept {
  428. return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
  429. }
  430. // Here and below we make use of std::common_type_t to emulate
  431. // std::type_identity (part of C++20). This creates a non-deduced context, so
  432. // that we can compare StringPieces with types that implicitly convert to
  433. // StringPieces. See https://wg21.link/n3766 for details.
  434. // Furthermore, we require dummy template parameters for these overloads to work
  435. // around a name mangling issue on Windows.
  436. template <typename CharT, typename Traits, int = 1>
  437. constexpr bool operator==(
  438. BasicStringPiece<CharT, Traits> lhs,
  439. std::common_type_t<BasicStringPiece<CharT, Traits>> rhs) noexcept {
  440. return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
  441. }
  442. template <typename CharT, typename Traits, int = 2>
  443. constexpr bool operator==(
  444. std::common_type_t<BasicStringPiece<CharT, Traits>> lhs,
  445. BasicStringPiece<CharT, Traits> rhs) noexcept {
  446. return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
  447. }
  448. template <typename CharT, typename Traits>
  449. constexpr bool operator!=(BasicStringPiece<CharT, Traits> lhs,
  450. BasicStringPiece<CharT, Traits> rhs) noexcept {
  451. return !(lhs == rhs);
  452. }
  453. template <typename CharT, typename Traits, int = 1>
  454. constexpr bool operator!=(
  455. BasicStringPiece<CharT, Traits> lhs,
  456. std::common_type_t<BasicStringPiece<CharT, Traits>> rhs) noexcept {
  457. return !(lhs == rhs);
  458. }
  459. template <typename CharT, typename Traits, int = 2>
  460. constexpr bool operator!=(
  461. std::common_type_t<BasicStringPiece<CharT, Traits>> lhs,
  462. BasicStringPiece<CharT, Traits> rhs) noexcept {
  463. return !(lhs == rhs);
  464. }
  465. template <typename CharT, typename Traits>
  466. constexpr bool operator<(BasicStringPiece<CharT, Traits> lhs,
  467. BasicStringPiece<CharT, Traits> rhs) noexcept {
  468. return lhs.compare(rhs) < 0;
  469. }
  470. template <typename CharT, typename Traits, int = 1>
  471. constexpr bool operator<(
  472. BasicStringPiece<CharT, Traits> lhs,
  473. std::common_type_t<BasicStringPiece<CharT, Traits>> rhs) noexcept {
  474. return lhs.compare(rhs) < 0;
  475. }
  476. template <typename CharT, typename Traits, int = 2>
  477. constexpr bool operator<(
  478. std::common_type_t<BasicStringPiece<CharT, Traits>> lhs,
  479. BasicStringPiece<CharT, Traits> rhs) noexcept {
  480. return lhs.compare(rhs) < 0;
  481. }
  482. template <typename CharT, typename Traits>
  483. constexpr bool operator>(BasicStringPiece<CharT, Traits> lhs,
  484. BasicStringPiece<CharT, Traits> rhs) noexcept {
  485. return rhs < lhs;
  486. }
  487. template <typename CharT, typename Traits, int = 1>
  488. constexpr bool operator>(
  489. BasicStringPiece<CharT, Traits> lhs,
  490. std::common_type_t<BasicStringPiece<CharT, Traits>> rhs) noexcept {
  491. return rhs < lhs;
  492. }
  493. template <typename CharT, typename Traits, int = 2>
  494. constexpr bool operator>(
  495. std::common_type_t<BasicStringPiece<CharT, Traits>> lhs,
  496. BasicStringPiece<CharT, Traits> rhs) noexcept {
  497. return rhs < lhs;
  498. }
  499. template <typename CharT, typename Traits>
  500. constexpr bool operator<=(BasicStringPiece<CharT, Traits> lhs,
  501. BasicStringPiece<CharT, Traits> rhs) noexcept {
  502. return !(rhs < lhs);
  503. }
  504. template <typename CharT, typename Traits, int = 1>
  505. constexpr bool operator<=(
  506. BasicStringPiece<CharT, Traits> lhs,
  507. std::common_type_t<BasicStringPiece<CharT, Traits>> rhs) noexcept {
  508. return !(rhs < lhs);
  509. }
  510. template <typename CharT, typename Traits, int = 2>
  511. constexpr bool operator<=(
  512. std::common_type_t<BasicStringPiece<CharT, Traits>> lhs,
  513. BasicStringPiece<CharT, Traits> rhs) noexcept {
  514. return !(rhs < lhs);
  515. }
  516. template <typename CharT, typename Traits>
  517. constexpr bool operator>=(BasicStringPiece<CharT, Traits> lhs,
  518. BasicStringPiece<CharT, Traits> rhs) noexcept {
  519. return !(lhs < rhs);
  520. }
  521. template <typename CharT, typename Traits, int = 1>
  522. constexpr bool operator>=(
  523. BasicStringPiece<CharT, Traits> lhs,
  524. std::common_type_t<BasicStringPiece<CharT, Traits>> rhs) noexcept {
  525. return !(lhs < rhs);
  526. }
  527. template <typename CharT, typename Traits, int = 2>
  528. constexpr bool operator>=(
  529. std::common_type_t<BasicStringPiece<CharT, Traits>> lhs,
  530. BasicStringPiece<CharT, Traits> rhs) noexcept {
  531. return !(lhs < rhs);
  532. }
  533. BASE_EXPORT std::ostream& operator<<(std::ostream& o, StringPiece piece);
  534. // Not in the STL: convenience functions to output non-UTF-8 strings to an
  535. // 8-bit-width stream.
  536. BASE_EXPORT std::ostream& operator<<(std::ostream& o, StringPiece16 piece);
  537. BASE_EXPORT std::ostream& operator<<(std::ostream& o, WStringPiece piece);
  538. // Intentionally omitted (since Chromium does not use character literals):
  539. // operator""sv.
  540. // Stand-ins for the STL's std::hash<> specializations.
  541. template <typename StringPieceType>
  542. struct StringPieceHashImpl {
  543. // This is a custom hash function. We don't use the ones already defined for
  544. // string and std::u16string directly because it would require the string
  545. // constructors to be called, which we don't want.
  546. size_t operator()(StringPieceType sp) const {
  547. size_t result = 0;
  548. for (auto c : sp)
  549. result = (result * 131) + static_cast<size_t>(c);
  550. return result;
  551. }
  552. };
  553. using StringPieceHash = StringPieceHashImpl<StringPiece>;
  554. using StringPiece16Hash = StringPieceHashImpl<StringPiece16>;
  555. using WStringPieceHash = StringPieceHashImpl<WStringPiece>;
  556. } // namespace base
  557. #endif // BASE_STRINGS_STRING_PIECE_H_