string_view.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2017 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00016 // -----------------------------------------------------------------------------
00017 // File: string_view.h
00018 // -----------------------------------------------------------------------------
00019 //
00020 // This file contains the definition of the `absl::string_view` class. A
00021 // `string_view` points to a contiguous span of characters, often part or all of
00022 // another `std::string`, double-quoted string literal, character array, or even
00023 // another `string_view`.
00024 //
00025 // This `absl::string_view` abstraction is designed to be a drop-in
00026 // replacement for the C++17 `std::string_view` abstraction.
00027 #ifndef ABSL_STRINGS_STRING_VIEW_H_
00028 #define ABSL_STRINGS_STRING_VIEW_H_
00029 
00030 #include <algorithm>
00031 #include "absl/base/config.h"
00032 
00033 #ifdef ABSL_HAVE_STD_STRING_VIEW
00034 
00035 #include <string_view>  // IWYU pragma: export
00036 
00037 namespace absl {
00038 using std::string_view;
00039 }  // namespace absl
00040 
00041 #else  // ABSL_HAVE_STD_STRING_VIEW
00042 
00043 #include <cassert>
00044 #include <cstddef>
00045 #include <cstring>
00046 #include <iosfwd>
00047 #include <iterator>
00048 #include <limits>
00049 #include <string>
00050 
00051 #include "absl/base/internal/throw_delegate.h"
00052 #include "absl/base/macros.h"
00053 #include "absl/base/port.h"
00054 
00055 namespace absl {
00056 
00057 // absl::string_view
00058 //
00059 // A `string_view` provides a lightweight view into the string data provided by
00060 // a `std::string`, double-quoted string literal, character array, or even
00061 // another `string_view`. A `string_view` does *not* own the string to which it
00062 // points, and that data cannot be modified through the view.
00063 //
00064 // You can use `string_view` as a function or method parameter anywhere a
00065 // parameter can receive a double-quoted string literal, `const char*`,
00066 // `std::string`, or another `absl::string_view` argument with no need to copy
00067 // the string data. Systematic use of `string_view` within function arguments
00068 // reduces data copies and `strlen()` calls.
00069 //
00070 // Because of its small size, prefer passing `string_view` by value:
00071 //
00072 //   void MyFunction(absl::string_view arg);
00073 //
00074 // If circumstances require, you may also pass one by const reference:
00075 //
00076 //   void MyFunction(const absl::string_view& arg);  // not preferred
00077 //
00078 // Passing by value generates slightly smaller code for many architectures.
00079 //
00080 // In either case, the source data of the `string_view` must outlive the
00081 // `string_view` itself.
00082 //
00083 // A `string_view` is also suitable for local variables if you know that the
00084 // lifetime of the underlying object is longer than the lifetime of your
00085 // `string_view` variable. However, beware of binding a `string_view` to a
00086 // temporary value:
00087 //
00088 //   // BAD use of string_view: lifetime problem
00089 //   absl::string_view sv = obj.ReturnAString();
00090 //
00091 //   // GOOD use of string_view: str outlives sv
00092 //   std::string str = obj.ReturnAString();
00093 //   absl::string_view sv = str;
00094 //
00095 // Due to lifetime issues, a `string_view` is sometimes a poor choice for a
00096 // return value and usually a poor choice for a data member. If you do use a
00097 // `string_view` this way, it is your responsibility to ensure that the object
00098 // pointed to by the `string_view` outlives the `string_view`.
00099 //
00100 // A `string_view` may represent a whole string or just part of a string. For
00101 // example, when splitting a string, `std::vector<absl::string_view>` is a
00102 // natural data type for the output.
00103 //
00104 // When constructed from a source which is nul-terminated, the `string_view`
00105 // itself will not include the nul-terminator unless a specific size (including
00106 // the nul) is passed to the constructor. As a result, common idioms that work
00107 // on nul-terminated strings do not work on `string_view` objects. If you write
00108 // code that scans a `string_view`, you must check its length rather than test
00109 // for nul, for example. Note, however, that nuls may still be embedded within
00110 // a `string_view` explicitly.
00111 //
00112 // You may create a null `string_view` in two ways:
00113 //
00114 //   absl::string_view sv();
00115 //   absl::string_view sv(nullptr, 0);
00116 //
00117 // For the above, `sv.data() == nullptr`, `sv.length() == 0`, and
00118 // `sv.empty() == true`. Also, if you create a `string_view` with a non-null
00119 // pointer then `sv.data() != nullptr`. Thus, you can use `string_view()` to
00120 // signal an undefined value that is different from other `string_view` values
00121 // in a similar fashion to how `const char* p1 = nullptr;` is different from
00122 // `const char* p2 = "";`. However, in practice, it is not recommended to rely
00123 // on this behavior.
00124 //
00125 // Be careful not to confuse a null `string_view` with an empty one. A null
00126 // `string_view` is an empty `string_view`, but some empty `string_view`s are
00127 // not null. Prefer checking for emptiness over checking for null.
00128 //
00129 // There are many ways to create an empty string_view:
00130 //
00131 //   const char* nullcp = nullptr;
00132 //   // string_view.size() will return 0 in all cases.
00133 //   absl::string_view();
00134 //   absl::string_view(nullcp, 0);
00135 //   absl::string_view("");
00136 //   absl::string_view("", 0);
00137 //   absl::string_view("abcdef", 0);
00138 //   absl::string_view("abcdef" + 6, 0);
00139 //
00140 // All empty `string_view` objects whether null or not, are equal:
00141 //
00142 //   absl::string_view() == absl::string_view("", 0)
00143 //   absl::string_view(nullptr, 0) == absl::string_view("abcdef"+6, 0)
00144 class string_view {
00145  public:
00146   using traits_type = std::char_traits<char>;
00147   using value_type = char;
00148   using pointer = char*;
00149   using const_pointer = const char*;
00150   using reference = char&;
00151   using const_reference = const char&;
00152   using const_iterator = const char*;
00153   using iterator = const_iterator;
00154   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
00155   using reverse_iterator = const_reverse_iterator;
00156   using size_type = size_t;
00157   using difference_type = std::ptrdiff_t;
00158 
00159   static constexpr size_type npos = static_cast<size_type>(-1);
00160 
00161   // Null `string_view` constructor
00162   constexpr string_view() noexcept : ptr_(nullptr), length_(0) {}
00163 
00164   // Implicit constructors
00165 
00166   template <typename Allocator>
00167   string_view(  // NOLINT(runtime/explicit)
00168       const std::basic_string<char, std::char_traits<char>, Allocator>&
00169           str) noexcept
00170       : ptr_(str.data()), length_(CheckLengthInternal(str.size())) {}
00171 
00172   // Implicit constructor of a `string_view` from nul-terminated `str`. When
00173   // accepting possibly null strings, use `absl::NullSafeStringView(str)`
00174   // instead (see below).
00175 #if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
00176     (defined(__GNUC__) && !defined(__clang__))
00177   // GCC has __builtin_strlen according to
00178   // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but
00179   // ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above.
00180   // __builtin_strlen is constexpr.
00181   constexpr string_view(const char* str)  // NOLINT(runtime/explicit)
00182       : ptr_(str),
00183         length_(CheckLengthInternal(str ? __builtin_strlen(str) : 0)) {}
00184 #else
00185   constexpr string_view(const char* str)  // NOLINT(runtime/explicit)
00186       : ptr_(str), length_(CheckLengthInternal(str ? strlen(str) : 0)) {}
00187 #endif
00188 
00189   // Implicit constructor of a `string_view` from a `const char*` and length.
00190   constexpr string_view(const char* data, size_type len)
00191       : ptr_(data), length_(CheckLengthInternal(len)) {}
00192 
00193   // NOTE: Harmlessly omitted to work around gdb bug.
00194   //   constexpr string_view(const string_view&) noexcept = default;
00195   //   string_view& operator=(const string_view&) noexcept = default;
00196 
00197   // Iterators
00198 
00199   // string_view::begin()
00200   //
00201   // Returns an iterator pointing to the first character at the beginning of the
00202   // `string_view`, or `end()` if the `string_view` is empty.
00203   constexpr const_iterator begin() const noexcept { return ptr_; }
00204 
00205   // string_view::end()
00206   //
00207   // Returns an iterator pointing just beyond the last character at the end of
00208   // the `string_view`. This iterator acts as a placeholder; attempting to
00209   // access it results in undefined behavior.
00210   constexpr const_iterator end() const noexcept { return ptr_ + length_; }
00211 
00212   // string_view::cbegin()
00213   //
00214   // Returns a const iterator pointing to the first character at the beginning
00215   // of the `string_view`, or `end()` if the `string_view` is empty.
00216   constexpr const_iterator cbegin() const noexcept { return begin(); }
00217 
00218   // string_view::cend()
00219   //
00220   // Returns a const iterator pointing just beyond the last character at the end
00221   // of the `string_view`. This pointer acts as a placeholder; attempting to
00222   // access its element results in undefined behavior.
00223   constexpr const_iterator cend() const noexcept { return end(); }
00224 
00225   // string_view::rbegin()
00226   //
00227   // Returns a reverse iterator pointing to the last character at the end of the
00228   // `string_view`, or `rend()` if the `string_view` is empty.
00229   const_reverse_iterator rbegin() const noexcept {
00230     return const_reverse_iterator(end());
00231   }
00232 
00233   // string_view::rend()
00234   //
00235   // Returns a reverse iterator pointing just before the first character at the
00236   // beginning of the `string_view`. This pointer acts as a placeholder;
00237   // attempting to access its element results in undefined behavior.
00238   const_reverse_iterator rend() const noexcept {
00239     return const_reverse_iterator(begin());
00240   }
00241 
00242   // string_view::crbegin()
00243   //
00244   // Returns a const reverse iterator pointing to the last character at the end
00245   // of the `string_view`, or `crend()` if the `string_view` is empty.
00246   const_reverse_iterator crbegin() const noexcept { return rbegin(); }
00247 
00248   // string_view::crend()
00249   //
00250   // Returns a const reverse iterator pointing just before the first character
00251   // at the beginning of the `string_view`. This pointer acts as a placeholder;
00252   // attempting to access its element results in undefined behavior.
00253   const_reverse_iterator crend() const noexcept { return rend(); }
00254 
00255   // Capacity Utilities
00256 
00257   // string_view::size()
00258   //
00259   // Returns the number of characters in the `string_view`.
00260   constexpr size_type size() const noexcept {
00261     return length_;
00262   }
00263 
00264   // string_view::length()
00265   //
00266   // Returns the number of characters in the `string_view`. Alias for `size()`.
00267   constexpr size_type length() const noexcept { return size(); }
00268 
00269   // string_view::max_size()
00270   //
00271   // Returns the maximum number of characters the `string_view` can hold.
00272   constexpr size_type max_size() const noexcept { return kMaxSize; }
00273 
00274   // string_view::empty()
00275   //
00276   // Checks if the `string_view` is empty (refers to no characters).
00277   constexpr bool empty() const noexcept { return length_ == 0; }
00278 
00279   // string_view::operator[]
00280   //
00281   // Returns the ith element of an `string_view` using the array operator.
00282   // Note that this operator does not perform any bounds checking.
00283   constexpr const_reference operator[](size_type i) const { return ptr_[i]; }
00284 
00285   // string_view::front()
00286   //
00287   // Returns the first element of a `string_view`.
00288   constexpr const_reference front() const { return ptr_[0]; }
00289 
00290   // string_view::back()
00291   //
00292   // Returns the last element of a `string_view`.
00293   constexpr const_reference back() const { return ptr_[size() - 1]; }
00294 
00295   // string_view::data()
00296   //
00297   // Returns a pointer to the underlying character array (which is of course
00298   // stored elsewhere). Note that `string_view::data()` may contain embedded nul
00299   // characters, but the returned buffer may or may not be nul-terminated;
00300   // therefore, do not pass `data()` to a routine that expects a nul-terminated
00301   // std::string.
00302   constexpr const_pointer data() const noexcept { return ptr_; }
00303 
00304   // Modifiers
00305 
00306   // string_view::remove_prefix()
00307   //
00308   // Removes the first `n` characters from the `string_view`. Note that the
00309   // underlying std::string is not changed, only the view.
00310   void remove_prefix(size_type n) {
00311     assert(n <= length_);
00312     ptr_ += n;
00313     length_ -= n;
00314   }
00315 
00316   // string_view::remove_suffix()
00317   //
00318   // Removes the last `n` characters from the `string_view`. Note that the
00319   // underlying std::string is not changed, only the view.
00320   void remove_suffix(size_type n) {
00321     assert(n <= length_);
00322     length_ -= n;
00323   }
00324 
00325   // string_view::swap()
00326   //
00327   // Swaps this `string_view` with another `string_view`.
00328   void swap(string_view& s) noexcept {
00329     auto t = *this;
00330     *this = s;
00331     s = t;
00332   }
00333 
00334   // Explicit conversion operators
00335 
00336   // Converts to `std::basic_string`.
00337   template <typename A>
00338   explicit operator std::basic_string<char, traits_type, A>() const {
00339     if (!data()) return {};
00340     return std::basic_string<char, traits_type, A>(data(), size());
00341   }
00342 
00343   // string_view::copy()
00344   //
00345   // Copies the contents of the `string_view` at offset `pos` and length `n`
00346   // into `buf`.
00347   size_type copy(char* buf, size_type n, size_type pos = 0) const;
00348 
00349   // string_view::substr()
00350   //
00351   // Returns a "substring" of the `string_view` (at offset `pos` and length
00352   // `n`) as another string_view. This function throws `std::out_of_bounds` if
00353   // `pos > size`.
00354   string_view substr(size_type pos, size_type n = npos) const {
00355     if (ABSL_PREDICT_FALSE(pos > length_))
00356       base_internal::ThrowStdOutOfRange("absl::string_view::substr");
00357     n = (std::min)(n, length_ - pos);
00358     return string_view(ptr_ + pos, n);
00359   }
00360 
00361   // string_view::compare()
00362   //
00363   // Performs a lexicographical comparison between the `string_view` and
00364   // another `absl::string_view`, returning -1 if `this` is less than, 0 if
00365   // `this` is equal to, and 1 if `this` is greater than the passed std::string
00366   // view. Note that in the case of data equality, a further comparison is made
00367   // on the respective sizes of the two `string_view`s to determine which is
00368   // smaller, equal, or greater.
00369   int compare(string_view x) const noexcept {
00370     auto min_length = (std::min)(length_, x.length_);
00371     if (min_length > 0) {
00372       int r = memcmp(ptr_, x.ptr_, min_length);
00373       if (r < 0) return -1;
00374       if (r > 0) return 1;
00375     }
00376     if (length_ < x.length_) return -1;
00377     if (length_ > x.length_) return 1;
00378     return 0;
00379   }
00380 
00381   // Overload of `string_view::compare()` for comparing a substring of the
00382   // 'string_view` and another `absl::string_view`.
00383   int compare(size_type pos1, size_type count1, string_view v) const {
00384     return substr(pos1, count1).compare(v);
00385   }
00386 
00387   // Overload of `string_view::compare()` for comparing a substring of the
00388   // `string_view` and a substring of another `absl::string_view`.
00389   int compare(size_type pos1, size_type count1, string_view v, size_type pos2,
00390               size_type count2) const {
00391     return substr(pos1, count1).compare(v.substr(pos2, count2));
00392   }
00393 
00394   // Overload of `string_view::compare()` for comparing a `string_view` and a
00395   // a different  C-style std::string `s`.
00396   int compare(const char* s) const { return compare(string_view(s)); }
00397 
00398   // Overload of `string_view::compare()` for comparing a substring of the
00399   // `string_view` and a different std::string C-style std::string `s`.
00400   int compare(size_type pos1, size_type count1, const char* s) const {
00401     return substr(pos1, count1).compare(string_view(s));
00402   }
00403 
00404   // Overload of `string_view::compare()` for comparing a substring of the
00405   // `string_view` and a substring of a different C-style std::string `s`.
00406   int compare(size_type pos1, size_type count1, const char* s,
00407               size_type count2) const {
00408     return substr(pos1, count1).compare(string_view(s, count2));
00409   }
00410 
00411   // Find Utilities
00412 
00413   // string_view::find()
00414   //
00415   // Finds the first occurrence of the substring `s` within the `string_view`,
00416   // returning the position of the first character's match, or `npos` if no
00417   // match was found.
00418   size_type find(string_view s, size_type pos = 0) const noexcept;
00419 
00420   // Overload of `string_view::find()` for finding the given character `c`
00421   // within the `string_view`.
00422   size_type find(char c, size_type pos = 0) const noexcept;
00423 
00424   // string_view::rfind()
00425   //
00426   // Finds the last occurrence of a substring `s` within the `string_view`,
00427   // returning the position of the first character's match, or `npos` if no
00428   // match was found.
00429   size_type rfind(string_view s, size_type pos = npos) const
00430       noexcept;
00431 
00432   // Overload of `string_view::rfind()` for finding the last given character `c`
00433   // within the `string_view`.
00434   size_type rfind(char c, size_type pos = npos) const noexcept;
00435 
00436   // string_view::find_first_of()
00437   //
00438   // Finds the first occurrence of any of the characters in `s` within the
00439   // `string_view`, returning the start position of the match, or `npos` if no
00440   // match was found.
00441   size_type find_first_of(string_view s, size_type pos = 0) const
00442       noexcept;
00443 
00444   // Overload of `string_view::find_first_of()` for finding a character `c`
00445   // within the `string_view`.
00446   size_type find_first_of(char c, size_type pos = 0) const
00447       noexcept {
00448     return find(c, pos);
00449   }
00450 
00451   // string_view::find_last_of()
00452   //
00453   // Finds the last occurrence of any of the characters in `s` within the
00454   // `string_view`, returning the start position of the match, or `npos` if no
00455   // match was found.
00456   size_type find_last_of(string_view s, size_type pos = npos) const
00457       noexcept;
00458 
00459   // Overload of `string_view::find_last_of()` for finding a character `c`
00460   // within the `string_view`.
00461   size_type find_last_of(char c, size_type pos = npos) const
00462       noexcept {
00463     return rfind(c, pos);
00464   }
00465 
00466   // string_view::find_first_not_of()
00467   //
00468   // Finds the first occurrence of any of the characters not in `s` within the
00469   // `string_view`, returning the start position of the first non-match, or
00470   // `npos` if no non-match was found.
00471   size_type find_first_not_of(string_view s, size_type pos = 0) const noexcept;
00472 
00473   // Overload of `string_view::find_first_not_of()` for finding a character
00474   // that is not `c` within the `string_view`.
00475   size_type find_first_not_of(char c, size_type pos = 0) const noexcept;
00476 
00477   // string_view::find_last_not_of()
00478   //
00479   // Finds the last occurrence of any of the characters not in `s` within the
00480   // `string_view`, returning the start position of the last non-match, or
00481   // `npos` if no non-match was found.
00482   size_type find_last_not_of(string_view s,
00483                                           size_type pos = npos) const noexcept;
00484 
00485   // Overload of `string_view::find_last_not_of()` for finding a character
00486   // that is not `c` within the `string_view`.
00487   size_type find_last_not_of(char c, size_type pos = npos) const
00488       noexcept;
00489 
00490  private:
00491   static constexpr size_type kMaxSize =
00492       (std::numeric_limits<difference_type>::max)();
00493 
00494   static constexpr size_type CheckLengthInternal(size_type len) {
00495     return ABSL_ASSERT(len <= kMaxSize), len;
00496   }
00497 
00498   const char* ptr_;
00499   size_type length_;
00500 };
00501 
00502 // This large function is defined inline so that in a fairly common case where
00503 // one of the arguments is a literal, the compiler can elide a lot of the
00504 // following comparisons.
00505 inline bool operator==(string_view x, string_view y) noexcept {
00506   auto len = x.size();
00507   if (len != y.size()) {
00508     return false;
00509   }
00510 
00511   return x.data() == y.data() || len <= 0 ||
00512          memcmp(x.data(), y.data(), len) == 0;
00513 }
00514 
00515 inline bool operator!=(string_view x, string_view y) noexcept {
00516   return !(x == y);
00517 }
00518 
00519 inline bool operator<(string_view x, string_view y) noexcept {
00520   auto min_size = (std::min)(x.size(), y.size());
00521   const int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
00522   return (r < 0) || (r == 0 && x.size() < y.size());
00523 }
00524 
00525 inline bool operator>(string_view x, string_view y) noexcept { return y < x; }
00526 
00527 inline bool operator<=(string_view x, string_view y) noexcept {
00528   return !(y < x);
00529 }
00530 
00531 inline bool operator>=(string_view x, string_view y) noexcept {
00532   return !(x < y);
00533 }
00534 
00535 // IO Insertion Operator
00536 std::ostream& operator<<(std::ostream& o, string_view piece);
00537 
00538 }  // namespace absl
00539 
00540 #endif  // ABSL_HAVE_STD_STRING_VIEW
00541 
00542 namespace absl {
00543 
00544 // ClippedSubstr()
00545 //
00546 // Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`.
00547 // Provided because std::string_view::substr throws if `pos > size()`
00548 inline string_view ClippedSubstr(string_view s, size_t pos,
00549                                  size_t n = string_view::npos) {
00550   pos = (std::min)(pos, static_cast<size_t>(s.size()));
00551   return s.substr(pos, n);
00552 }
00553 
00554 // NullSafeStringView()
00555 //
00556 // Creates an `absl::string_view` from a pointer `p` even if it's null-valued.
00557 // This function should be used where an `absl::string_view` can be created from
00558 // a possibly-null pointer.
00559 inline string_view NullSafeStringView(const char* p) {
00560   return p ? string_view(p) : string_view();
00561 }
00562 
00563 }  // namespace absl
00564 
00565 #endif  // ABSL_STRINGS_STRING_VIEW_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15