123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- #ifndef BASE_STRINGS_STRING_PIECE_H_
- #define BASE_STRINGS_STRING_PIECE_H_
- #include <stddef.h>
- #include <iosfwd>
- #include <ostream>
- #include <string>
- #include <type_traits>
- #include "base/base_export.h"
- #include "base/check_op.h"
- #include "base/strings/char_traits.h"
- #include "base/strings/string16.h"
- #include "base/strings/string_piece_forward.h"
- namespace base {
- namespace internal {
- BASE_EXPORT size_t copy(const StringPiece& self,
- char* buf,
- size_t n,
- size_t pos);
- BASE_EXPORT size_t copy(const StringPiece16& self,
- char16* buf,
- size_t n,
- size_t pos);
- BASE_EXPORT size_t find(const StringPiece& self,
- const StringPiece& s,
- size_t pos);
- BASE_EXPORT size_t find(const StringPiece16& self,
- const StringPiece16& s,
- size_t pos);
- BASE_EXPORT size_t find(const StringPiece& self,
- char c,
- size_t pos);
- BASE_EXPORT size_t find(const StringPiece16& self,
- char16 c,
- size_t pos);
- BASE_EXPORT size_t rfind(const StringPiece& self,
- const StringPiece& s,
- size_t pos);
- BASE_EXPORT size_t rfind(const StringPiece16& self,
- const StringPiece16& s,
- size_t pos);
- BASE_EXPORT size_t rfind(const StringPiece& self,
- char c,
- size_t pos);
- BASE_EXPORT size_t rfind(const StringPiece16& self,
- char16 c,
- size_t pos);
- BASE_EXPORT size_t find_first_of(const StringPiece& self,
- const StringPiece& s,
- size_t pos);
- BASE_EXPORT size_t find_first_of(const StringPiece16& self,
- const StringPiece16& s,
- size_t pos);
- BASE_EXPORT size_t find_first_not_of(const StringPiece& self,
- const StringPiece& s,
- size_t pos);
- BASE_EXPORT size_t find_first_not_of(const StringPiece16& self,
- const StringPiece16& s,
- size_t pos);
- BASE_EXPORT size_t find_first_not_of(const StringPiece& self,
- char c,
- size_t pos);
- BASE_EXPORT size_t find_first_not_of(const StringPiece16& self,
- char16 c,
- size_t pos);
- BASE_EXPORT size_t find_last_of(const StringPiece& self,
- const StringPiece& s,
- size_t pos);
- BASE_EXPORT size_t find_last_of(const StringPiece16& self,
- const StringPiece16& s,
- size_t pos);
- BASE_EXPORT size_t find_last_of(const StringPiece& self,
- char c,
- size_t pos);
- BASE_EXPORT size_t find_last_of(const StringPiece16& self,
- char16 c,
- size_t pos);
- BASE_EXPORT size_t find_last_not_of(const StringPiece& self,
- const StringPiece& s,
- size_t pos);
- BASE_EXPORT size_t find_last_not_of(const StringPiece16& self,
- const StringPiece16& s,
- size_t pos);
- BASE_EXPORT size_t find_last_not_of(const StringPiece16& self,
- char16 c,
- size_t pos);
- BASE_EXPORT size_t find_last_not_of(const StringPiece& self,
- char c,
- size_t pos);
- }
- template <typename STRING_TYPE> class BasicStringPiece {
- public:
-
- typedef size_t size_type;
- typedef typename STRING_TYPE::traits_type traits_type;
- typedef typename STRING_TYPE::value_type value_type;
- typedef const value_type* pointer;
- typedef const value_type& reference;
- typedef const value_type& const_reference;
- typedef ptrdiff_t difference_type;
- typedef const value_type* const_iterator;
- typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
- static const size_type npos;
- public:
-
-
-
- constexpr BasicStringPiece() : ptr_(nullptr), length_(0) {}
-
-
-
-
- constexpr BasicStringPiece(const value_type* str)
- : ptr_(str), length_(!str ? 0 : CharTraits<value_type>::length(str)) {}
-
-
-
-
-
-
-
- template <class T, class = std::enable_if_t<std::is_null_pointer<T>::value>>
- BasicStringPiece(T) {
- static_assert(sizeof(T) == 0,
- "StringPiece does not support construction from nullptr, use "
- "the default constructor instead.");
- }
- BasicStringPiece(const STRING_TYPE& str)
- : ptr_(str.data()), length_(str.size()) {}
- constexpr BasicStringPiece(const value_type* offset, size_type len)
- : ptr_(offset), length_(len) {}
- BasicStringPiece(const typename STRING_TYPE::const_iterator& begin,
- const typename STRING_TYPE::const_iterator& end) {
- DCHECK(begin <= end) << "StringPiece iterators swapped or invalid.";
- length_ = static_cast<size_t>(std::distance(begin, end));
-
-
- ptr_ = length_ > 0 ? &*begin : nullptr;
- }
-
-
-
-
- constexpr const value_type* data() const { return ptr_; }
- constexpr size_type size() const noexcept { return length_; }
- constexpr size_type length() const noexcept { return length_; }
- constexpr bool empty() const noexcept { return length_ == 0; }
- constexpr value_type operator[](size_type i) const {
- CHECK(i < length_);
- return ptr_[i];
- }
- constexpr value_type front() const {
- CHECK_NE(0UL, length_);
- return ptr_[0];
- }
- constexpr value_type back() const {
- CHECK_NE(0UL, length_);
- return ptr_[length_ - 1];
- }
- constexpr void remove_prefix(size_type n) {
- CHECK(n <= length_);
- ptr_ += n;
- length_ -= n;
- }
- constexpr void remove_suffix(size_type n) {
- CHECK(n <= length_);
- length_ -= n;
- }
- constexpr int compare(BasicStringPiece x) const noexcept {
- int r = CharTraits<value_type>::compare(
- ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_));
- if (r == 0) {
- if (length_ < x.length_) r = -1;
- else if (length_ > x.length_) r = +1;
- }
- return r;
- }
-
- explicit operator STRING_TYPE() const {
- return empty() ? STRING_TYPE() : STRING_TYPE(data(), size());
- }
-
-
- template <typename StrT = STRING_TYPE,
- typename = std::enable_if_t<std::is_same<StrT, std::string>::value>>
- STRING_TYPE as_string() const {
- return STRING_TYPE(*this);
- }
- constexpr const_iterator begin() const noexcept { return ptr_; }
- constexpr const_iterator end() const noexcept { return ptr_ + length_; }
- constexpr const_reverse_iterator rbegin() const noexcept {
- return const_reverse_iterator(ptr_ + length_);
- }
- constexpr const_reverse_iterator rend() const noexcept {
- return const_reverse_iterator(ptr_);
- }
- size_type max_size() const { return length_; }
- size_type capacity() const { return length_; }
- size_type copy(value_type* buf, size_type n, size_type pos = 0) const {
- return internal::copy(*this, buf, n, pos);
- }
-
- size_type find(const BasicStringPiece<STRING_TYPE>& s,
- size_type pos = 0) const {
- return internal::find(*this, s, pos);
- }
- size_type find(value_type c, size_type pos = 0) const {
- return internal::find(*this, c, pos);
- }
-
- size_type rfind(const BasicStringPiece& s,
- size_type pos = BasicStringPiece::npos) const {
- return internal::rfind(*this, s, pos);
- }
- size_type rfind(value_type c, size_type pos = BasicStringPiece::npos) const {
- return internal::rfind(*this, c, pos);
- }
-
- size_type find_first_of(const BasicStringPiece& s,
- size_type pos = 0) const {
- return internal::find_first_of(*this, s, pos);
- }
- size_type find_first_of(value_type c, size_type pos = 0) const {
- return find(c, pos);
- }
-
- size_type find_first_not_of(const BasicStringPiece& s,
- size_type pos = 0) const {
- return internal::find_first_not_of(*this, s, pos);
- }
- size_type find_first_not_of(value_type c, size_type pos = 0) const {
- return internal::find_first_not_of(*this, c, pos);
- }
-
- size_type find_last_of(const BasicStringPiece& s,
- size_type pos = BasicStringPiece::npos) const {
- return internal::find_last_of(*this, s, pos);
- }
- size_type find_last_of(value_type c,
- size_type pos = BasicStringPiece::npos) const {
- return rfind(c, pos);
- }
-
- size_type find_last_not_of(const BasicStringPiece& s,
- size_type pos = BasicStringPiece::npos) const {
- return internal::find_last_not_of(*this, s, pos);
- }
- size_type find_last_not_of(value_type c,
- size_type pos = BasicStringPiece::npos) const {
- return internal::find_last_not_of(*this, c, pos);
- }
-
- constexpr BasicStringPiece substr(
- size_type pos,
- size_type n = BasicStringPiece::npos) const {
- CHECK_LE(pos, size());
- return {data() + pos, std::min(n, size() - pos)};
- }
- protected:
- const value_type* ptr_;
- size_type length_;
- };
- template <typename STRING_TYPE>
- const typename BasicStringPiece<STRING_TYPE>::size_type
- BasicStringPiece<STRING_TYPE>::npos =
- typename BasicStringPiece<STRING_TYPE>::size_type(-1);
- #if !defined(COMPILER_MSVC)
- extern template class BASE_EXPORT BasicStringPiece<std::string>;
- extern template class BASE_EXPORT BasicStringPiece<string16>;
- #endif
- template <typename StringT>
- constexpr bool operator==(BasicStringPiece<StringT> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
- }
- template <typename StringT, int = 1>
- constexpr bool operator==(
- BasicStringPiece<StringT> lhs,
- std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept {
- return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
- }
- template <typename StringT, int = 2>
- constexpr bool operator==(std::common_type_t<BasicStringPiece<StringT>> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
- }
- template <typename StringT>
- constexpr bool operator!=(BasicStringPiece<StringT> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return !(lhs == rhs);
- }
- template <typename StringT, int = 1>
- constexpr bool operator!=(
- BasicStringPiece<StringT> lhs,
- std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept {
- return !(lhs == rhs);
- }
- template <typename StringT, int = 2>
- constexpr bool operator!=(std::common_type_t<BasicStringPiece<StringT>> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return !(lhs == rhs);
- }
- template <typename StringT>
- constexpr bool operator<(BasicStringPiece<StringT> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return lhs.compare(rhs) < 0;
- }
- template <typename StringT, int = 1>
- constexpr bool operator<(
- BasicStringPiece<StringT> lhs,
- std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept {
- return lhs.compare(rhs) < 0;
- }
- template <typename StringT, int = 2>
- constexpr bool operator<(std::common_type_t<BasicStringPiece<StringT>> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return lhs.compare(rhs) < 0;
- }
- template <typename StringT>
- constexpr bool operator>(BasicStringPiece<StringT> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return rhs < lhs;
- }
- template <typename StringT, int = 1>
- constexpr bool operator>(
- BasicStringPiece<StringT> lhs,
- std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept {
- return rhs < lhs;
- }
- template <typename StringT, int = 2>
- constexpr bool operator>(std::common_type_t<BasicStringPiece<StringT>> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return rhs < lhs;
- }
- template <typename StringT>
- constexpr bool operator<=(BasicStringPiece<StringT> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return !(rhs < lhs);
- }
- template <typename StringT, int = 1>
- constexpr bool operator<=(
- BasicStringPiece<StringT> lhs,
- std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept {
- return !(rhs < lhs);
- }
- template <typename StringT, int = 2>
- constexpr bool operator<=(std::common_type_t<BasicStringPiece<StringT>> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return !(rhs < lhs);
- }
- template <typename StringT>
- constexpr bool operator>=(BasicStringPiece<StringT> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return !(lhs < rhs);
- }
- template <typename StringT, int = 1>
- constexpr bool operator>=(
- BasicStringPiece<StringT> lhs,
- std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept {
- return !(lhs < rhs);
- }
- template <typename StringT, int = 2>
- constexpr bool operator>=(std::common_type_t<BasicStringPiece<StringT>> lhs,
- BasicStringPiece<StringT> rhs) noexcept {
- return !(lhs < rhs);
- }
- BASE_EXPORT std::ostream& operator<<(std::ostream& o,
- const StringPiece& piece);
- BASE_EXPORT std::ostream& operator<<(std::ostream& o,
- const StringPiece16& piece);
- template <typename StringPieceType>
- struct StringPieceHashImpl {
- std::size_t operator()(StringPieceType sp) const {
- std::size_t result = 0;
- for (auto c : sp)
- result = (result * 131) + c;
- return result;
- }
- };
- using StringPieceHash = StringPieceHashImpl<StringPiece>;
- using StringPiece16Hash = StringPieceHashImpl<StringPiece16>;
- using WStringPieceHash = StringPieceHashImpl<WStringPiece>;
- }
- #endif
|