string_view.h 23 KB

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