123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- //
- // Copyright 2017 The Abseil Authors.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // https://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- // -----------------------------------------------------------------------------
- // File: string_view.h
- // -----------------------------------------------------------------------------
- //
- // This file contains the definition of the `y_absl::string_view` class. A
- // `string_view` points to a contiguous span of characters, often part or all of
- // another `TString`, double-quoted string literal, character array, or even
- // another `string_view`.
- //
- // This `y_absl::string_view` abstraction is designed to be a drop-in
- // replacement for the C++17 `std::string_view` abstraction.
- #ifndef Y_ABSL_STRINGS_STRING_VIEW_H_
- #define Y_ABSL_STRINGS_STRING_VIEW_H_
- #include <algorithm>
- #include <cassert>
- #include <cstddef>
- #include <cstring>
- #include <iosfwd>
- #include <iterator>
- #include <limits>
- #include <util/generic/string.h>
- #include "y_absl/base/attributes.h"
- #include "y_absl/base/config.h"
- #include "y_absl/base/internal/throw_delegate.h"
- #include "y_absl/base/macros.h"
- #include "y_absl/base/optimization.h"
- #include "y_absl/base/port.h"
- #ifdef Y_ABSL_USES_STD_STRING_VIEW
- #include <string_view> // IWYU pragma: export
- namespace y_absl {
- Y_ABSL_NAMESPACE_BEGIN
- using string_view = std::string_view;
- Y_ABSL_NAMESPACE_END
- } // namespace y_absl
- #else // Y_ABSL_USES_STD_STRING_VIEW
- #if Y_ABSL_HAVE_BUILTIN(__builtin_memcmp) || \
- (defined(__GNUC__) && !defined(__clang__)) || \
- (defined(_MSC_VER) && _MSC_VER >= 1928)
- #define Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP __builtin_memcmp
- #else // Y_ABSL_HAVE_BUILTIN(__builtin_memcmp)
- #define Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP memcmp
- #endif // Y_ABSL_HAVE_BUILTIN(__builtin_memcmp)
- namespace y_absl {
- Y_ABSL_NAMESPACE_BEGIN
- // y_absl::string_view
- //
- // A `string_view` provides a lightweight view into the string data provided by
- // a `TString`, double-quoted string literal, character array, or even
- // another `string_view`. A `string_view` does *not* own the string to which it
- // points, and that data cannot be modified through the view.
- //
- // You can use `string_view` as a function or method parameter anywhere a
- // parameter can receive a double-quoted string literal, `const char*`,
- // `TString`, or another `y_absl::string_view` argument with no need to copy
- // the string data. Systematic use of `string_view` within function arguments
- // reduces data copies and `strlen()` calls.
- //
- // Because of its small size, prefer passing `string_view` by value:
- //
- // void MyFunction(y_absl::string_view arg);
- //
- // If circumstances require, you may also pass one by const reference:
- //
- // void MyFunction(const y_absl::string_view& arg); // not preferred
- //
- // Passing by value generates slightly smaller code for many architectures.
- //
- // In either case, the source data of the `string_view` must outlive the
- // `string_view` itself.
- //
- // A `string_view` is also suitable for local variables if you know that the
- // lifetime of the underlying object is longer than the lifetime of your
- // `string_view` variable. However, beware of binding a `string_view` to a
- // temporary value:
- //
- // // BAD use of string_view: lifetime problem
- // y_absl::string_view sv = obj.ReturnAString();
- //
- // // GOOD use of string_view: str outlives sv
- // TString str = obj.ReturnAString();
- // y_absl::string_view sv = str;
- //
- // Due to lifetime issues, a `string_view` is sometimes a poor choice for a
- // return value and usually a poor choice for a data member. If you do use a
- // `string_view` this way, it is your responsibility to ensure that the object
- // pointed to by the `string_view` outlives the `string_view`.
- //
- // A `string_view` may represent a whole string or just part of a string. For
- // example, when splitting a string, `std::vector<y_absl::string_view>` is a
- // natural data type for the output.
- //
- // For another example, a Cord is a non-contiguous, potentially very
- // long string-like object. The Cord class has an interface that iteratively
- // provides string_view objects that point to the successive pieces of a Cord
- // object.
- //
- // When constructed from a source which is NUL-terminated, the `string_view`
- // itself will not include the NUL-terminator unless a specific size (including
- // the NUL) is passed to the constructor. As a result, common idioms that work
- // on NUL-terminated strings do not work on `string_view` objects. If you write
- // code that scans a `string_view`, you must check its length rather than test
- // for nul, for example. Note, however, that nuls may still be embedded within
- // a `string_view` explicitly.
- //
- // You may create a null `string_view` in two ways:
- //
- // y_absl::string_view sv;
- // y_absl::string_view sv(nullptr, 0);
- //
- // For the above, `sv.data() == nullptr`, `sv.length() == 0`, and
- // `sv.empty() == true`. Also, if you create a `string_view` with a non-null
- // pointer then `sv.data() != nullptr`. Thus, you can use `string_view()` to
- // signal an undefined value that is different from other `string_view` values
- // in a similar fashion to how `const char* p1 = nullptr;` is different from
- // `const char* p2 = "";`. However, in practice, it is not recommended to rely
- // on this behavior.
- //
- // Be careful not to confuse a null `string_view` with an empty one. A null
- // `string_view` is an empty `string_view`, but some empty `string_view`s are
- // not null. Prefer checking for emptiness over checking for null.
- //
- // There are many ways to create an empty string_view:
- //
- // const char* nullcp = nullptr;
- // // string_view.size() will return 0 in all cases.
- // y_absl::string_view();
- // y_absl::string_view(nullcp, 0);
- // y_absl::string_view("");
- // y_absl::string_view("", 0);
- // y_absl::string_view("abcdef", 0);
- // y_absl::string_view("abcdef" + 6, 0);
- //
- // All empty `string_view` objects whether null or not, are equal:
- //
- // y_absl::string_view() == y_absl::string_view("", 0)
- // y_absl::string_view(nullptr, 0) == y_absl::string_view("abcdef"+6, 0)
- class string_view {
- public:
- using traits_type = std::char_traits<char>;
- using value_type = char;
- using pointer = char*;
- using const_pointer = const char*;
- using reference = char&;
- using const_reference = const char&;
- using const_iterator = const char*;
- using iterator = const_iterator;
- using const_reverse_iterator = std::reverse_iterator<const_iterator>;
- using reverse_iterator = const_reverse_iterator;
- using size_type = size_t;
- using difference_type = std::ptrdiff_t;
- static constexpr size_type npos = static_cast<size_type>(-1);
- // Null `string_view` constructor
- constexpr string_view() noexcept : ptr_(nullptr), length_(0) {}
- // Implicit constructors
- template <typename Allocator>
- string_view( // NOLINT(runtime/explicit)
- const std::basic_string<char, std::char_traits<char>, Allocator>& str
- Y_ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept
- // This is implemented in terms of `string_view(p, n)` so `str.size()`
- // doesn't need to be reevaluated after `ptr_` is set.
- // The length check is also skipped since it is unnecessary and causes
- // code bloat.
- : string_view(str.data(), str.size(), SkipCheckLengthTag{}) {}
- // Implicit constructor of a `string_view` from NUL-terminated `str`. When
- // accepting possibly null strings, use `y_absl::NullSafeStringView(str)`
- // instead (see below).
- // The length check is skipped since it is unnecessary and causes code bloat.
- constexpr string_view(const char* str) // NOLINT(runtime/explicit)
- : ptr_(str), length_(str ? StrlenInternal(str) : 0) {}
- // Implicit constructor of a `string_view` from a `const char*` and length.
- constexpr string_view(const char* data, size_type len)
- : ptr_(data), length_(CheckLengthInternal(len)) {}
- // NOTE: Harmlessly omitted to work around gdb bug.
- // constexpr string_view(const string_view&) noexcept = default;
- // string_view& operator=(const string_view&) noexcept = default;
- // Iterators
- // string_view::begin()
- //
- // Returns an iterator pointing to the first character at the beginning of the
- // `string_view`, or `end()` if the `string_view` is empty.
- constexpr const_iterator begin() const noexcept { return ptr_; }
- // string_view::end()
- //
- // Returns an iterator pointing just beyond the last character at the end of
- // the `string_view`. This iterator acts as a placeholder; attempting to
- // access it results in undefined behavior.
- constexpr const_iterator end() const noexcept { return ptr_ + length_; }
- // string_view::cbegin()
- //
- // Returns a const iterator pointing to the first character at the beginning
- // of the `string_view`, or `end()` if the `string_view` is empty.
- constexpr const_iterator cbegin() const noexcept { return begin(); }
- // string_view::cend()
- //
- // Returns a const iterator pointing just beyond the last character at the end
- // of the `string_view`. This pointer acts as a placeholder; attempting to
- // access its element results in undefined behavior.
- constexpr const_iterator cend() const noexcept { return end(); }
- // string_view::rbegin()
- //
- // Returns a reverse iterator pointing to the last character at the end of the
- // `string_view`, or `rend()` if the `string_view` is empty.
- const_reverse_iterator rbegin() const noexcept {
- return const_reverse_iterator(end());
- }
- // string_view::rend()
- //
- // Returns a reverse iterator pointing just before the first character at the
- // beginning of the `string_view`. This pointer acts as a placeholder;
- // attempting to access its element results in undefined behavior.
- const_reverse_iterator rend() const noexcept {
- return const_reverse_iterator(begin());
- }
- // string_view::crbegin()
- //
- // Returns a const reverse iterator pointing to the last character at the end
- // of the `string_view`, or `crend()` if the `string_view` is empty.
- const_reverse_iterator crbegin() const noexcept { return rbegin(); }
- // string_view::crend()
- //
- // Returns a const reverse iterator pointing just before the first character
- // at the beginning of the `string_view`. This pointer acts as a placeholder;
- // attempting to access its element results in undefined behavior.
- const_reverse_iterator crend() const noexcept { return rend(); }
- // Capacity Utilities
- // string_view::size()
- //
- // Returns the number of characters in the `string_view`.
- constexpr size_type size() const noexcept { return length_; }
- // string_view::length()
- //
- // Returns the number of characters in the `string_view`. Alias for `size()`.
- constexpr size_type length() const noexcept { return size(); }
- // string_view::max_size()
- //
- // Returns the maximum number of characters the `string_view` can hold.
- constexpr size_type max_size() const noexcept { return kMaxSize; }
- // string_view::empty()
- //
- // Checks if the `string_view` is empty (refers to no characters).
- constexpr bool empty() const noexcept { return length_ == 0; }
- // string_view::operator[]
- //
- // Returns the ith element of the `string_view` using the array operator.
- // Note that this operator does not perform any bounds checking.
- constexpr const_reference operator[](size_type i) const {
- return Y_ABSL_HARDENING_ASSERT(i < size()), ptr_[i];
- }
- // string_view::at()
- //
- // Returns the ith element of the `string_view`. Bounds checking is performed,
- // and an exception of type `std::out_of_range` will be thrown on invalid
- // access.
- constexpr const_reference at(size_type i) const {
- return Y_ABSL_PREDICT_TRUE(i < size())
- ? ptr_[i]
- : ((void)base_internal::ThrowStdOutOfRange(
- "y_absl::string_view::at"),
- ptr_[i]);
- }
- // string_view::front()
- //
- // Returns the first element of a `string_view`.
- constexpr const_reference front() const {
- return Y_ABSL_HARDENING_ASSERT(!empty()), ptr_[0];
- }
- // string_view::back()
- //
- // Returns the last element of a `string_view`.
- constexpr const_reference back() const {
- return Y_ABSL_HARDENING_ASSERT(!empty()), ptr_[size() - 1];
- }
- // string_view::data()
- //
- // Returns a pointer to the underlying character array (which is of course
- // stored elsewhere). Note that `string_view::data()` may contain embedded nul
- // characters, but the returned buffer may or may not be NUL-terminated;
- // therefore, do not pass `data()` to a routine that expects a NUL-terminated
- // string.
- constexpr const_pointer data() const noexcept { return ptr_; }
- // Modifiers
- // string_view::remove_prefix()
- //
- // Removes the first `n` characters from the `string_view`. Note that the
- // underlying string is not changed, only the view.
- constexpr void remove_prefix(size_type n) {
- Y_ABSL_HARDENING_ASSERT(n <= length_);
- ptr_ += n;
- length_ -= n;
- }
- // string_view::remove_suffix()
- //
- // Removes the last `n` characters from the `string_view`. Note that the
- // underlying string is not changed, only the view.
- constexpr void remove_suffix(size_type n) {
- Y_ABSL_HARDENING_ASSERT(n <= length_);
- length_ -= n;
- }
- // string_view::swap()
- //
- // Swaps this `string_view` with another `string_view`.
- constexpr void swap(string_view& s) noexcept {
- auto t = *this;
- *this = s;
- s = t;
- }
- // Explicit conversion operators
- // Converts to `std::basic_string`.
- template <typename A>
- explicit operator std::basic_string<char, traits_type, A>() const {
- if (!data()) return {};
- return std::basic_string<char, traits_type, A>(data(), size());
- }
- // string_view::copy()
- //
- // Copies the contents of the `string_view` at offset `pos` and length `n`
- // into `buf`.
- size_type copy(char* buf, size_type n, size_type pos = 0) const {
- if (Y_ABSL_PREDICT_FALSE(pos > length_)) {
- base_internal::ThrowStdOutOfRange("y_absl::string_view::copy");
- }
- size_type rlen = (std::min)(length_ - pos, n);
- if (rlen > 0) {
- const char* start = ptr_ + pos;
- traits_type::copy(buf, start, rlen);
- }
- return rlen;
- }
- // string_view::substr()
- //
- // Returns a "substring" of the `string_view` (at offset `pos` and length
- // `n`) as another string_view. This function throws `std::out_of_bounds` if
- // `pos > size`.
- // Use y_absl::ClippedSubstr if you need a truncating substr operation.
- constexpr string_view substr(size_type pos = 0, size_type n = npos) const {
- return Y_ABSL_PREDICT_FALSE(pos > length_)
- ? (base_internal::ThrowStdOutOfRange(
- "y_absl::string_view::substr"),
- string_view())
- : string_view(ptr_ + pos, Min(n, length_ - pos));
- }
- // string_view::compare()
- //
- // Performs a lexicographical comparison between this `string_view` and
- // another `string_view` `x`, returning a negative value if `*this` is less
- // than `x`, 0 if `*this` is equal to `x`, and a positive value if `*this`
- // is greater than `x`.
- constexpr int compare(string_view x) const noexcept {
- return CompareImpl(length_, x.length_,
- Min(length_, x.length_) == 0
- ? 0
- : Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP(
- ptr_, x.ptr_, Min(length_, x.length_)));
- }
- // Overload of `string_view::compare()` for comparing a substring of the
- // 'string_view` and another `y_absl::string_view`.
- constexpr int compare(size_type pos1, size_type count1, string_view v) const {
- return substr(pos1, count1).compare(v);
- }
- // Overload of `string_view::compare()` for comparing a substring of the
- // `string_view` and a substring of another `y_absl::string_view`.
- constexpr int compare(size_type pos1, size_type count1, string_view v,
- size_type pos2, size_type count2) const {
- return substr(pos1, count1).compare(v.substr(pos2, count2));
- }
- // Overload of `string_view::compare()` for comparing a `string_view` and a
- // a different C-style string `s`.
- constexpr int compare(const char* s) const { return compare(string_view(s)); }
- // Overload of `string_view::compare()` for comparing a substring of the
- // `string_view` and a different string C-style string `s`.
- constexpr int compare(size_type pos1, size_type count1, const char* s) const {
- return substr(pos1, count1).compare(string_view(s));
- }
- // Overload of `string_view::compare()` for comparing a substring of the
- // `string_view` and a substring of a different C-style string `s`.
- constexpr int compare(size_type pos1, size_type count1, const char* s,
- size_type count2) const {
- return substr(pos1, count1).compare(string_view(s, count2));
- }
- // Find Utilities
- // string_view::find()
- //
- // Finds the first occurrence of the substring `s` within the `string_view`,
- // returning the position of the first character's match, or `npos` if no
- // match was found.
- size_type find(string_view s, size_type pos = 0) const noexcept;
- // Overload of `string_view::find()` for finding the given character `c`
- // within the `string_view`.
- size_type find(char c, size_type pos = 0) const noexcept;
- // Overload of `string_view::find()` for finding a substring of a different
- // C-style string `s` within the `string_view`.
- size_type find(const char* s, size_type pos, size_type count) const {
- return find(string_view(s, count), pos);
- }
- // Overload of `string_view::find()` for finding a different C-style string
- // `s` within the `string_view`.
- size_type find(const char* s, size_type pos = 0) const {
- return find(string_view(s), pos);
- }
- // string_view::rfind()
- //
- // Finds the last occurrence of a substring `s` within the `string_view`,
- // returning the position of the first character's match, or `npos` if no
- // match was found.
- size_type rfind(string_view s, size_type pos = npos) const noexcept;
- // Overload of `string_view::rfind()` for finding the last given character `c`
- // within the `string_view`.
- size_type rfind(char c, size_type pos = npos) const noexcept;
- // Overload of `string_view::rfind()` for finding a substring of a different
- // C-style string `s` within the `string_view`.
- size_type rfind(const char* s, size_type pos, size_type count) const {
- return rfind(string_view(s, count), pos);
- }
- // Overload of `string_view::rfind()` for finding a different C-style string
- // `s` within the `string_view`.
- size_type rfind(const char* s, size_type pos = npos) const {
- return rfind(string_view(s), pos);
- }
- // string_view::find_first_of()
- //
- // Finds the first occurrence of any of the characters in `s` within the
- // `string_view`, returning the start position of the match, or `npos` if no
- // match was found.
- size_type find_first_of(string_view s, size_type pos = 0) const noexcept;
- // Overload of `string_view::find_first_of()` for finding a character `c`
- // within the `string_view`.
- size_type find_first_of(char c, size_type pos = 0) const noexcept {
- return find(c, pos);
- }
- // Overload of `string_view::find_first_of()` for finding a substring of a
- // different C-style string `s` within the `string_view`.
- size_type find_first_of(const char* s, size_type pos,
- size_type count) const {
- return find_first_of(string_view(s, count), pos);
- }
- // Overload of `string_view::find_first_of()` for finding a different C-style
- // string `s` within the `string_view`.
- size_type find_first_of(const char* s, size_type pos = 0) const {
- return find_first_of(string_view(s), pos);
- }
- // string_view::find_last_of()
- //
- // Finds the last occurrence of any of the characters in `s` within the
- // `string_view`, returning the start position of the match, or `npos` if no
- // match was found.
- size_type find_last_of(string_view s, size_type pos = npos) const noexcept;
- // Overload of `string_view::find_last_of()` for finding a character `c`
- // within the `string_view`.
- size_type find_last_of(char c, size_type pos = npos) const noexcept {
- return rfind(c, pos);
- }
- // Overload of `string_view::find_last_of()` for finding a substring of a
- // different C-style string `s` within the `string_view`.
- size_type find_last_of(const char* s, size_type pos, size_type count) const {
- return find_last_of(string_view(s, count), pos);
- }
- // Overload of `string_view::find_last_of()` for finding a different C-style
- // string `s` within the `string_view`.
- size_type find_last_of(const char* s, size_type pos = npos) const {
- return find_last_of(string_view(s), pos);
- }
- // string_view::find_first_not_of()
- //
- // Finds the first occurrence of any of the characters not in `s` within the
- // `string_view`, returning the start position of the first non-match, or
- // `npos` if no non-match was found.
- size_type find_first_not_of(string_view s, size_type pos = 0) const noexcept;
- // Overload of `string_view::find_first_not_of()` for finding a character
- // that is not `c` within the `string_view`.
- size_type find_first_not_of(char c, size_type pos = 0) const noexcept;
- // Overload of `string_view::find_first_not_of()` for finding a substring of a
- // different C-style string `s` within the `string_view`.
- size_type find_first_not_of(const char* s, size_type pos,
- size_type count) const {
- return find_first_not_of(string_view(s, count), pos);
- }
- // Overload of `string_view::find_first_not_of()` for finding a different
- // C-style string `s` within the `string_view`.
- size_type find_first_not_of(const char* s, size_type pos = 0) const {
- return find_first_not_of(string_view(s), pos);
- }
- // string_view::find_last_not_of()
- //
- // Finds the last occurrence of any of the characters not in `s` within the
- // `string_view`, returning the start position of the last non-match, or
- // `npos` if no non-match was found.
- size_type find_last_not_of(string_view s,
- size_type pos = npos) const noexcept;
- // Overload of `string_view::find_last_not_of()` for finding a character
- // that is not `c` within the `string_view`.
- size_type find_last_not_of(char c, size_type pos = npos) const noexcept;
- // Overload of `string_view::find_last_not_of()` for finding a substring of a
- // different C-style string `s` within the `string_view`.
- size_type find_last_not_of(const char* s, size_type pos,
- size_type count) const {
- return find_last_not_of(string_view(s, count), pos);
- }
- // Overload of `string_view::find_last_not_of()` for finding a different
- // C-style string `s` within the `string_view`.
- size_type find_last_not_of(const char* s, size_type pos = npos) const {
- return find_last_not_of(string_view(s), pos);
- }
- private:
- // The constructor from TString delegates to this constructor.
- // See the comment on that constructor for the rationale.
- struct SkipCheckLengthTag {};
- string_view(const char* data, size_type len, SkipCheckLengthTag) noexcept
- : ptr_(data), length_(len) {}
- static constexpr size_type kMaxSize =
- (std::numeric_limits<difference_type>::max)();
- static constexpr size_type CheckLengthInternal(size_type len) {
- return Y_ABSL_HARDENING_ASSERT(len <= kMaxSize), len;
- }
- static constexpr size_type StrlenInternal(const char* str) {
- #if defined(_MSC_VER) && _MSC_VER >= 1910 && !defined(__clang__)
- // MSVC 2017+ can evaluate this at compile-time.
- const char* begin = str;
- while (*str != '\0') ++str;
- return str - begin;
- #elif Y_ABSL_HAVE_BUILTIN(__builtin_strlen) || \
- (defined(__GNUC__) && !defined(__clang__))
- // GCC has __builtin_strlen according to
- // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but
- // Y_ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above.
- // __builtin_strlen is constexpr.
- return __builtin_strlen(str);
- #else
- return str ? strlen(str) : 0;
- #endif
- }
- static constexpr size_t Min(size_type length_a, size_type length_b) {
- return length_a < length_b ? length_a : length_b;
- }
- static constexpr int CompareImpl(size_type length_a, size_type length_b,
- int compare_result) {
- return compare_result == 0 ? static_cast<int>(length_a > length_b) -
- static_cast<int>(length_a < length_b)
- : (compare_result < 0 ? -1 : 1);
- }
- const char* ptr_;
- size_type length_;
- };
- // This large function is defined inline so that in a fairly common case where
- // one of the arguments is a literal, the compiler can elide a lot of the
- // following comparisons.
- constexpr bool operator==(string_view x, string_view y) noexcept {
- return x.size() == y.size() &&
- (x.empty() ||
- Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP(x.data(), y.data(), x.size()) == 0);
- }
- constexpr bool operator!=(string_view x, string_view y) noexcept {
- return !(x == y);
- }
- constexpr bool operator<(string_view x, string_view y) noexcept {
- return x.compare(y) < 0;
- }
- constexpr bool operator>(string_view x, string_view y) noexcept {
- return y < x;
- }
- constexpr bool operator<=(string_view x, string_view y) noexcept {
- return !(y < x);
- }
- constexpr bool operator>=(string_view x, string_view y) noexcept {
- return !(x < y);
- }
- // IO Insertion Operator
- std::ostream& operator<<(std::ostream& o, string_view piece);
- Y_ABSL_NAMESPACE_END
- } // namespace y_absl
- #undef Y_ABSL_INTERNAL_STRING_VIEW_MEMCMP
- #endif // Y_ABSL_USES_STD_STRING_VIEW
- namespace y_absl {
- Y_ABSL_NAMESPACE_BEGIN
- // ClippedSubstr()
- //
- // Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`.
- // Provided because std::string_view::substr throws if `pos > size()`
- inline string_view ClippedSubstr(string_view s, size_t pos,
- size_t n = string_view::npos) {
- pos = (std::min)(pos, static_cast<size_t>(s.size()));
- return s.substr(pos, n);
- }
- // NullSafeStringView()
- //
- // Creates an `y_absl::string_view` from a pointer `p` even if it's null-valued.
- // This function should be used where an `y_absl::string_view` can be created from
- // a possibly-null pointer.
- constexpr string_view NullSafeStringView(const char* p) {
- return p ? string_view(p) : string_view();
- }
- Y_ABSL_NAMESPACE_END
- } // namespace y_absl
- #endif // Y_ABSL_STRINGS_STRING_VIEW_H_
|