string_view.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: string_view.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This file contains the definition of the `y_absl::string_view` class. A
  21. // `string_view` points to a contiguous span of characters, often part or all of
  22. // another `TString`, double-quoted string literal, character array, or even
  23. // another `string_view`.
  24. //
  25. // This `y_absl::string_view` abstraction is designed to be a drop-in
  26. // replacement for the C++17 `std::string_view` abstraction.
  27. #ifndef Y_ABSL_STRINGS_STRING_VIEW_H_
  28. #define Y_ABSL_STRINGS_STRING_VIEW_H_
  29. #include <algorithm>
  30. #include <cassert>
  31. #include <cstddef>
  32. #include <cstring>
  33. #include <iosfwd>
  34. #include <iterator>
  35. #include <limits>
  36. #include <util/generic/string.h>
  37. #include "y_absl/base/attributes.h"
  38. #include "y_absl/base/config.h"
  39. #include "y_absl/base/internal/throw_delegate.h"
  40. #include "y_absl/base/macros.h"
  41. #include "y_absl/base/optimization.h"
  42. #include "y_absl/base/port.h"
  43. #ifdef Y_ABSL_USES_STD_STRING_VIEW
  44. #include <string_view> // IWYU pragma: export
  45. namespace y_absl {
  46. Y_ABSL_NAMESPACE_BEGIN
  47. using string_view = std::string_view;
  48. Y_ABSL_NAMESPACE_END
  49. } // namespace y_absl
  50. #else // Y_ABSL_USES_STD_STRING_VIEW
  51. #if Y_ABSL_HAVE_BUILTIN(__builtin_memcmp) || \
  52. (defined(__GNUC__) && !defined(__clang__)) || \
  53. (defined(_MSC_VER) && _MSC_VER >= 1928)
  54. #define Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP __builtin_memcmp
  55. #else // Y_ABSL_HAVE_BUILTIN(__builtin_memcmp)
  56. #define Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP memcmp
  57. #endif // Y_ABSL_HAVE_BUILTIN(__builtin_memcmp)
  58. namespace y_absl {
  59. Y_ABSL_NAMESPACE_BEGIN
  60. // y_absl::string_view
  61. //
  62. // A `string_view` provides a lightweight view into the string data provided by
  63. // a `TString`, double-quoted string literal, character array, or even
  64. // another `string_view`. A `string_view` does *not* own the string to which it
  65. // points, and that data cannot be modified through the view.
  66. //
  67. // You can use `string_view` as a function or method parameter anywhere a
  68. // parameter can receive a double-quoted string literal, `const char*`,
  69. // `TString`, or another `y_absl::string_view` argument with no need to copy
  70. // the string data. Systematic use of `string_view` within function arguments
  71. // reduces data copies and `strlen()` calls.
  72. //
  73. // Because of its small size, prefer passing `string_view` by value:
  74. //
  75. // void MyFunction(y_absl::string_view arg);
  76. //
  77. // If circumstances require, you may also pass one by const reference:
  78. //
  79. // void MyFunction(const y_absl::string_view& arg); // not preferred
  80. //
  81. // Passing by value generates slightly smaller code for many architectures.
  82. //
  83. // In either case, the source data of the `string_view` must outlive the
  84. // `string_view` itself.
  85. //
  86. // A `string_view` is also suitable for local variables if you know that the
  87. // lifetime of the underlying object is longer than the lifetime of your
  88. // `string_view` variable. However, beware of binding a `string_view` to a
  89. // temporary value:
  90. //
  91. // // BAD use of string_view: lifetime problem
  92. // y_absl::string_view sv = obj.ReturnAString();
  93. //
  94. // // GOOD use of string_view: str outlives sv
  95. // TString str = obj.ReturnAString();
  96. // y_absl::string_view sv = str;
  97. //
  98. // Due to lifetime issues, a `string_view` is sometimes a poor choice for a
  99. // return value and usually a poor choice for a data member. If you do use a
  100. // `string_view` this way, it is your responsibility to ensure that the object
  101. // pointed to by the `string_view` outlives the `string_view`.
  102. //
  103. // A `string_view` may represent a whole string or just part of a string. For
  104. // example, when splitting a string, `std::vector<y_absl::string_view>` is a
  105. // natural data type for the output.
  106. //
  107. // For another example, a Cord is a non-contiguous, potentially very
  108. // long string-like object. The Cord class has an interface that iteratively
  109. // provides string_view objects that point to the successive pieces of a Cord
  110. // object.
  111. //
  112. // When constructed from a source which is NUL-terminated, the `string_view`
  113. // itself will not include the NUL-terminator unless a specific size (including
  114. // the NUL) is passed to the constructor. As a result, common idioms that work
  115. // on NUL-terminated strings do not work on `string_view` objects. If you write
  116. // code that scans a `string_view`, you must check its length rather than test
  117. // for nul, for example. Note, however, that nuls may still be embedded within
  118. // a `string_view` explicitly.
  119. //
  120. // You may create a null `string_view` in two ways:
  121. //
  122. // y_absl::string_view sv;
  123. // y_absl::string_view sv(nullptr, 0);
  124. //
  125. // For the above, `sv.data() == nullptr`, `sv.length() == 0`, and
  126. // `sv.empty() == true`. Also, if you create a `string_view` with a non-null
  127. // pointer then `sv.data() != nullptr`. Thus, you can use `string_view()` to
  128. // signal an undefined value that is different from other `string_view` values
  129. // in a similar fashion to how `const char* p1 = nullptr;` is different from
  130. // `const char* p2 = "";`. However, in practice, it is not recommended to rely
  131. // on this behavior.
  132. //
  133. // Be careful not to confuse a null `string_view` with an empty one. A null
  134. // `string_view` is an empty `string_view`, but some empty `string_view`s are
  135. // not null. Prefer checking for emptiness over checking for null.
  136. //
  137. // There are many ways to create an empty string_view:
  138. //
  139. // const char* nullcp = nullptr;
  140. // // string_view.size() will return 0 in all cases.
  141. // y_absl::string_view();
  142. // y_absl::string_view(nullcp, 0);
  143. // y_absl::string_view("");
  144. // y_absl::string_view("", 0);
  145. // y_absl::string_view("abcdef", 0);
  146. // y_absl::string_view("abcdef" + 6, 0);
  147. //
  148. // All empty `string_view` objects whether null or not, are equal:
  149. //
  150. // y_absl::string_view() == y_absl::string_view("", 0)
  151. // y_absl::string_view(nullptr, 0) == y_absl::string_view("abcdef"+6, 0)
  152. class string_view {
  153. public:
  154. using traits_type = std::char_traits<char>;
  155. using value_type = char;
  156. using pointer = char*;
  157. using const_pointer = const char*;
  158. using reference = char&;
  159. using const_reference = const char&;
  160. using const_iterator = const char*;
  161. using iterator = const_iterator;
  162. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  163. using reverse_iterator = const_reverse_iterator;
  164. using size_type = size_t;
  165. using difference_type = std::ptrdiff_t;
  166. static constexpr size_type npos = static_cast<size_type>(-1);
  167. // Null `string_view` constructor
  168. constexpr string_view() noexcept : ptr_(nullptr), length_(0) {}
  169. // Implicit constructors
  170. template <typename Allocator>
  171. string_view( // NOLINT(runtime/explicit)
  172. const std::basic_string<char, std::char_traits<char>, Allocator>& str
  173. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept
  174. // This is implemented in terms of `string_view(p, n)` so `str.size()`
  175. // doesn't need to be reevaluated after `ptr_` is set.
  176. // The length check is also skipped since it is unnecessary and causes
  177. // code bloat.
  178. : string_view(str.data(), str.size(), SkipCheckLengthTag{}) {}
  179. // Implicit constructor of a `string_view` from NUL-terminated `str`. When
  180. // accepting possibly null strings, use `y_absl::NullSafeStringView(str)`
  181. // instead (see below).
  182. // The length check is skipped since it is unnecessary and causes code bloat.
  183. constexpr string_view(const char* str) // NOLINT(runtime/explicit)
  184. : ptr_(str), length_(str ? StrlenInternal(str) : 0) {}
  185. // Implicit constructor of a `string_view` from a `const char*` and length.
  186. constexpr string_view(const char* data, size_type len)
  187. : ptr_(data), length_(CheckLengthInternal(len)) {}
  188. // NOTE: Harmlessly omitted to work around gdb bug.
  189. // constexpr string_view(const string_view&) noexcept = default;
  190. // string_view& operator=(const string_view&) noexcept = default;
  191. // Iterators
  192. // string_view::begin()
  193. //
  194. // Returns an iterator pointing to the first character at the beginning of the
  195. // `string_view`, or `end()` if the `string_view` is empty.
  196. constexpr const_iterator begin() const noexcept { return ptr_; }
  197. // string_view::end()
  198. //
  199. // Returns an iterator pointing just beyond the last character at the end of
  200. // the `string_view`. This iterator acts as a placeholder; attempting to
  201. // access it results in undefined behavior.
  202. constexpr const_iterator end() const noexcept { return ptr_ + length_; }
  203. // string_view::cbegin()
  204. //
  205. // Returns a const iterator pointing to the first character at the beginning
  206. // of the `string_view`, or `end()` if the `string_view` is empty.
  207. constexpr const_iterator cbegin() const noexcept { return begin(); }
  208. // string_view::cend()
  209. //
  210. // Returns a const iterator pointing just beyond the last character at the end
  211. // of the `string_view`. This pointer acts as a placeholder; attempting to
  212. // access its element results in undefined behavior.
  213. constexpr const_iterator cend() const noexcept { return end(); }
  214. // string_view::rbegin()
  215. //
  216. // Returns a reverse iterator pointing to the last character at the end of the
  217. // `string_view`, or `rend()` if the `string_view` is empty.
  218. const_reverse_iterator rbegin() const noexcept {
  219. return const_reverse_iterator(end());
  220. }
  221. // string_view::rend()
  222. //
  223. // Returns a reverse iterator pointing just before the first character at the
  224. // beginning of the `string_view`. This pointer acts as a placeholder;
  225. // attempting to access its element results in undefined behavior.
  226. const_reverse_iterator rend() const noexcept {
  227. return const_reverse_iterator(begin());
  228. }
  229. // string_view::crbegin()
  230. //
  231. // Returns a const reverse iterator pointing to the last character at the end
  232. // of the `string_view`, or `crend()` if the `string_view` is empty.
  233. const_reverse_iterator crbegin() const noexcept { return rbegin(); }
  234. // string_view::crend()
  235. //
  236. // Returns a const reverse iterator pointing just before the first character
  237. // at the beginning of the `string_view`. This pointer acts as a placeholder;
  238. // attempting to access its element results in undefined behavior.
  239. const_reverse_iterator crend() const noexcept { return rend(); }
  240. // Capacity Utilities
  241. // string_view::size()
  242. //
  243. // Returns the number of characters in the `string_view`.
  244. constexpr size_type size() const noexcept { return length_; }
  245. // string_view::length()
  246. //
  247. // Returns the number of characters in the `string_view`. Alias for `size()`.
  248. constexpr size_type length() const noexcept { return size(); }
  249. // string_view::max_size()
  250. //
  251. // Returns the maximum number of characters the `string_view` can hold.
  252. constexpr size_type max_size() const noexcept { return kMaxSize; }
  253. // string_view::empty()
  254. //
  255. // Checks if the `string_view` is empty (refers to no characters).
  256. constexpr bool empty() const noexcept { return length_ == 0; }
  257. // string_view::operator[]
  258. //
  259. // Returns the ith element of the `string_view` using the array operator.
  260. // Note that this operator does not perform any bounds checking.
  261. constexpr const_reference operator[](size_type i) const {
  262. return Y_ABSL_HARDENING_ASSERT(i < size()), ptr_[i];
  263. }
  264. // string_view::at()
  265. //
  266. // Returns the ith element of the `string_view`. Bounds checking is performed,
  267. // and an exception of type `std::out_of_range` will be thrown on invalid
  268. // access.
  269. constexpr const_reference at(size_type i) const {
  270. return Y_ABSL_PREDICT_TRUE(i < size())
  271. ? ptr_[i]
  272. : ((void)base_internal::ThrowStdOutOfRange(
  273. "y_absl::string_view::at"),
  274. ptr_[i]);
  275. }
  276. // string_view::front()
  277. //
  278. // Returns the first element of a `string_view`.
  279. constexpr const_reference front() const {
  280. return Y_ABSL_HARDENING_ASSERT(!empty()), ptr_[0];
  281. }
  282. // string_view::back()
  283. //
  284. // Returns the last element of a `string_view`.
  285. constexpr const_reference back() const {
  286. return Y_ABSL_HARDENING_ASSERT(!empty()), ptr_[size() - 1];
  287. }
  288. // string_view::data()
  289. //
  290. // Returns a pointer to the underlying character array (which is of course
  291. // stored elsewhere). Note that `string_view::data()` may contain embedded nul
  292. // characters, but the returned buffer may or may not be NUL-terminated;
  293. // therefore, do not pass `data()` to a routine that expects a NUL-terminated
  294. // string.
  295. constexpr const_pointer data() const noexcept { return ptr_; }
  296. // Modifiers
  297. // string_view::remove_prefix()
  298. //
  299. // Removes the first `n` characters from the `string_view`. Note that the
  300. // underlying string is not changed, only the view.
  301. constexpr void remove_prefix(size_type n) {
  302. Y_ABSL_HARDENING_ASSERT(n <= length_);
  303. ptr_ += n;
  304. length_ -= n;
  305. }
  306. // string_view::remove_suffix()
  307. //
  308. // Removes the last `n` characters from the `string_view`. Note that the
  309. // underlying string is not changed, only the view.
  310. constexpr void remove_suffix(size_type n) {
  311. Y_ABSL_HARDENING_ASSERT(n <= length_);
  312. length_ -= n;
  313. }
  314. // string_view::swap()
  315. //
  316. // Swaps this `string_view` with another `string_view`.
  317. constexpr void swap(string_view& s) noexcept {
  318. auto t = *this;
  319. *this = s;
  320. s = t;
  321. }
  322. // Explicit conversion operators
  323. // Converts to `std::basic_string`.
  324. template <typename A>
  325. explicit operator std::basic_string<char, traits_type, A>() const {
  326. if (!data()) return {};
  327. return std::basic_string<char, traits_type, A>(data(), size());
  328. }
  329. // string_view::copy()
  330. //
  331. // Copies the contents of the `string_view` at offset `pos` and length `n`
  332. // into `buf`.
  333. size_type copy(char* buf, size_type n, size_type pos = 0) const {
  334. if (Y_ABSL_PREDICT_FALSE(pos > length_)) {
  335. base_internal::ThrowStdOutOfRange("y_absl::string_view::copy");
  336. }
  337. size_type rlen = (std::min)(length_ - pos, n);
  338. if (rlen > 0) {
  339. const char* start = ptr_ + pos;
  340. traits_type::copy(buf, start, rlen);
  341. }
  342. return rlen;
  343. }
  344. // string_view::substr()
  345. //
  346. // Returns a "substring" of the `string_view` (at offset `pos` and length
  347. // `n`) as another string_view. This function throws `std::out_of_bounds` if
  348. // `pos > size`.
  349. // Use y_absl::ClippedSubstr if you need a truncating substr operation.
  350. constexpr string_view substr(size_type pos = 0, size_type n = npos) const {
  351. return Y_ABSL_PREDICT_FALSE(pos > length_)
  352. ? (base_internal::ThrowStdOutOfRange(
  353. "y_absl::string_view::substr"),
  354. string_view())
  355. : string_view(ptr_ + pos, Min(n, length_ - pos));
  356. }
  357. // string_view::compare()
  358. //
  359. // Performs a lexicographical comparison between this `string_view` and
  360. // another `string_view` `x`, returning a negative value if `*this` is less
  361. // than `x`, 0 if `*this` is equal to `x`, and a positive value if `*this`
  362. // is greater than `x`.
  363. constexpr int compare(string_view x) const noexcept {
  364. return CompareImpl(length_, x.length_,
  365. Min(length_, x.length_) == 0
  366. ? 0
  367. : Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP(
  368. ptr_, x.ptr_, Min(length_, x.length_)));
  369. }
  370. // Overload of `string_view::compare()` for comparing a substring of the
  371. // 'string_view` and another `y_absl::string_view`.
  372. constexpr int compare(size_type pos1, size_type count1, string_view v) const {
  373. return substr(pos1, count1).compare(v);
  374. }
  375. // Overload of `string_view::compare()` for comparing a substring of the
  376. // `string_view` and a substring of another `y_absl::string_view`.
  377. constexpr int compare(size_type pos1, size_type count1, string_view v,
  378. size_type pos2, size_type count2) const {
  379. return substr(pos1, count1).compare(v.substr(pos2, count2));
  380. }
  381. // Overload of `string_view::compare()` for comparing a `string_view` and a
  382. // a different C-style string `s`.
  383. constexpr int compare(const char* s) const { return compare(string_view(s)); }
  384. // Overload of `string_view::compare()` for comparing a substring of the
  385. // `string_view` and a different string C-style string `s`.
  386. constexpr int compare(size_type pos1, size_type count1, const char* s) const {
  387. return substr(pos1, count1).compare(string_view(s));
  388. }
  389. // Overload of `string_view::compare()` for comparing a substring of the
  390. // `string_view` and a substring of a different C-style string `s`.
  391. constexpr int compare(size_type pos1, size_type count1, const char* s,
  392. size_type count2) const {
  393. return substr(pos1, count1).compare(string_view(s, count2));
  394. }
  395. // Find Utilities
  396. // string_view::find()
  397. //
  398. // Finds the first occurrence of the substring `s` within the `string_view`,
  399. // returning the position of the first character's match, or `npos` if no
  400. // match was found.
  401. size_type find(string_view s, size_type pos = 0) const noexcept;
  402. // Overload of `string_view::find()` for finding the given character `c`
  403. // within the `string_view`.
  404. size_type find(char c, size_type pos = 0) const noexcept;
  405. // Overload of `string_view::find()` for finding a substring of a different
  406. // C-style string `s` within the `string_view`.
  407. size_type find(const char* s, size_type pos, size_type count) const {
  408. return find(string_view(s, count), pos);
  409. }
  410. // Overload of `string_view::find()` for finding a different C-style string
  411. // `s` within the `string_view`.
  412. size_type find(const char* s, size_type pos = 0) const {
  413. return find(string_view(s), pos);
  414. }
  415. // string_view::rfind()
  416. //
  417. // Finds the last occurrence of a substring `s` within the `string_view`,
  418. // returning the position of the first character's match, or `npos` if no
  419. // match was found.
  420. size_type rfind(string_view s, size_type pos = npos) const noexcept;
  421. // Overload of `string_view::rfind()` for finding the last given character `c`
  422. // within the `string_view`.
  423. size_type rfind(char c, size_type pos = npos) const noexcept;
  424. // Overload of `string_view::rfind()` for finding a substring of a different
  425. // C-style string `s` within the `string_view`.
  426. size_type rfind(const char* s, size_type pos, size_type count) const {
  427. return rfind(string_view(s, count), pos);
  428. }
  429. // Overload of `string_view::rfind()` for finding a different C-style string
  430. // `s` within the `string_view`.
  431. size_type rfind(const char* s, size_type pos = npos) const {
  432. return rfind(string_view(s), pos);
  433. }
  434. // string_view::find_first_of()
  435. //
  436. // Finds the first occurrence of any of the characters in `s` within the
  437. // `string_view`, returning the start position of the match, or `npos` if no
  438. // match was found.
  439. size_type find_first_of(string_view s, size_type pos = 0) const noexcept;
  440. // Overload of `string_view::find_first_of()` for finding a character `c`
  441. // within the `string_view`.
  442. size_type find_first_of(char c, size_type pos = 0) const noexcept {
  443. return find(c, pos);
  444. }
  445. // Overload of `string_view::find_first_of()` for finding a substring of a
  446. // different C-style string `s` within the `string_view`.
  447. size_type find_first_of(const char* s, size_type pos,
  448. size_type count) const {
  449. return find_first_of(string_view(s, count), pos);
  450. }
  451. // Overload of `string_view::find_first_of()` for finding a different C-style
  452. // string `s` within the `string_view`.
  453. size_type find_first_of(const char* s, size_type pos = 0) const {
  454. return find_first_of(string_view(s), pos);
  455. }
  456. // string_view::find_last_of()
  457. //
  458. // Finds the last occurrence of any of the characters in `s` within the
  459. // `string_view`, returning the start position of the match, or `npos` if no
  460. // match was found.
  461. size_type find_last_of(string_view s, size_type pos = npos) const noexcept;
  462. // Overload of `string_view::find_last_of()` for finding a character `c`
  463. // within the `string_view`.
  464. size_type find_last_of(char c, size_type pos = npos) const noexcept {
  465. return rfind(c, pos);
  466. }
  467. // Overload of `string_view::find_last_of()` for finding a substring of a
  468. // different C-style string `s` within the `string_view`.
  469. size_type find_last_of(const char* s, size_type pos, size_type count) const {
  470. return find_last_of(string_view(s, count), pos);
  471. }
  472. // Overload of `string_view::find_last_of()` for finding a different C-style
  473. // string `s` within the `string_view`.
  474. size_type find_last_of(const char* s, size_type pos = npos) const {
  475. return find_last_of(string_view(s), pos);
  476. }
  477. // string_view::find_first_not_of()
  478. //
  479. // Finds the first occurrence of any of the characters not in `s` within the
  480. // `string_view`, returning the start position of the first non-match, or
  481. // `npos` if no non-match was found.
  482. size_type find_first_not_of(string_view s, size_type pos = 0) const noexcept;
  483. // Overload of `string_view::find_first_not_of()` for finding a character
  484. // that is not `c` within the `string_view`.
  485. size_type find_first_not_of(char c, size_type pos = 0) const noexcept;
  486. // Overload of `string_view::find_first_not_of()` for finding a substring of a
  487. // different C-style string `s` within the `string_view`.
  488. size_type find_first_not_of(const char* s, size_type pos,
  489. size_type count) const {
  490. return find_first_not_of(string_view(s, count), pos);
  491. }
  492. // Overload of `string_view::find_first_not_of()` for finding a different
  493. // C-style string `s` within the `string_view`.
  494. size_type find_first_not_of(const char* s, size_type pos = 0) const {
  495. return find_first_not_of(string_view(s), pos);
  496. }
  497. // string_view::find_last_not_of()
  498. //
  499. // Finds the last occurrence of any of the characters not in `s` within the
  500. // `string_view`, returning the start position of the last non-match, or
  501. // `npos` if no non-match was found.
  502. size_type find_last_not_of(string_view s,
  503. size_type pos = npos) const noexcept;
  504. // Overload of `string_view::find_last_not_of()` for finding a character
  505. // that is not `c` within the `string_view`.
  506. size_type find_last_not_of(char c, size_type pos = npos) const noexcept;
  507. // Overload of `string_view::find_last_not_of()` for finding a substring of a
  508. // different C-style string `s` within the `string_view`.
  509. size_type find_last_not_of(const char* s, size_type pos,
  510. size_type count) const {
  511. return find_last_not_of(string_view(s, count), pos);
  512. }
  513. // Overload of `string_view::find_last_not_of()` for finding a different
  514. // C-style string `s` within the `string_view`.
  515. size_type find_last_not_of(const char* s, size_type pos = npos) const {
  516. return find_last_not_of(string_view(s), pos);
  517. }
  518. private:
  519. // The constructor from TString delegates to this constructor.
  520. // See the comment on that constructor for the rationale.
  521. struct SkipCheckLengthTag {};
  522. string_view(const char* data, size_type len, SkipCheckLengthTag) noexcept
  523. : ptr_(data), length_(len) {}
  524. static constexpr size_type kMaxSize =
  525. (std::numeric_limits<difference_type>::max)();
  526. static constexpr size_type CheckLengthInternal(size_type len) {
  527. return Y_ABSL_HARDENING_ASSERT(len <= kMaxSize), len;
  528. }
  529. static constexpr size_type StrlenInternal(const char* str) {
  530. #if defined(_MSC_VER) && _MSC_VER >= 1910 && !defined(__clang__)
  531. // MSVC 2017+ can evaluate this at compile-time.
  532. const char* begin = str;
  533. while (*str != '\0') ++str;
  534. return str - begin;
  535. #elif Y_ABSL_HAVE_BUILTIN(__builtin_strlen) || \
  536. (defined(__GNUC__) && !defined(__clang__))
  537. // GCC has __builtin_strlen according to
  538. // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but
  539. // Y_ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above.
  540. // __builtin_strlen is constexpr.
  541. return __builtin_strlen(str);
  542. #else
  543. return str ? strlen(str) : 0;
  544. #endif
  545. }
  546. static constexpr size_t Min(size_type length_a, size_type length_b) {
  547. return length_a < length_b ? length_a : length_b;
  548. }
  549. static constexpr int CompareImpl(size_type length_a, size_type length_b,
  550. int compare_result) {
  551. return compare_result == 0 ? static_cast<int>(length_a > length_b) -
  552. static_cast<int>(length_a < length_b)
  553. : (compare_result < 0 ? -1 : 1);
  554. }
  555. const char* ptr_;
  556. size_type length_;
  557. };
  558. // This large function is defined inline so that in a fairly common case where
  559. // one of the arguments is a literal, the compiler can elide a lot of the
  560. // following comparisons.
  561. constexpr bool operator==(string_view x, string_view y) noexcept {
  562. return x.size() == y.size() &&
  563. (x.empty() ||
  564. Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP(x.data(), y.data(), x.size()) == 0);
  565. }
  566. constexpr bool operator!=(string_view x, string_view y) noexcept {
  567. return !(x == y);
  568. }
  569. constexpr bool operator<(string_view x, string_view y) noexcept {
  570. return x.compare(y) < 0;
  571. }
  572. constexpr bool operator>(string_view x, string_view y) noexcept {
  573. return y < x;
  574. }
  575. constexpr bool operator<=(string_view x, string_view y) noexcept {
  576. return !(y < x);
  577. }
  578. constexpr bool operator>=(string_view x, string_view y) noexcept {
  579. return !(x < y);
  580. }
  581. // IO Insertion Operator
  582. std::ostream& operator<<(std::ostream& o, string_view piece);
  583. Y_ABSL_NAMESPACE_END
  584. } // namespace y_absl
  585. #undef Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP
  586. #endif // Y_ABSL_USES_STD_STRING_VIEW
  587. namespace y_absl {
  588. Y_ABSL_NAMESPACE_BEGIN
  589. // ClippedSubstr()
  590. //
  591. // Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`.
  592. // Provided because std::string_view::substr throws if `pos > size()`
  593. inline string_view ClippedSubstr(string_view s, size_t pos,
  594. size_t n = string_view::npos) {
  595. pos = (std::min)(pos, static_cast<size_t>(s.size()));
  596. return s.substr(pos, n);
  597. }
  598. // NullSafeStringView()
  599. //
  600. // Creates an `y_absl::string_view` from a pointer `p` even if it's null-valued.
  601. // This function should be used where an `y_absl::string_view` can be created from
  602. // a possibly-null pointer.
  603. constexpr string_view NullSafeStringView(const char* p) {
  604. return p ? string_view(p) : string_view();
  605. }
  606. Y_ABSL_NAMESPACE_END
  607. } // namespace y_absl
  608. #endif // Y_ABSL_STRINGS_STRING_VIEW_H_