You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

string_view.h 31 kB

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