michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // Copied from strings/stringpiece.h with modifications michael@0: // michael@0: // A string-like object that points to a sized piece of memory. michael@0: // michael@0: // Functions or methods may use const StringPiece& parameters to accept either michael@0: // a "const char*" or a "string" value that will be implicitly converted to michael@0: // a StringPiece. The implicit conversion means that it is often appropriate michael@0: // to include this .h file in other files rather than forward-declaring michael@0: // StringPiece as would be appropriate for most other Google classes. michael@0: // michael@0: // Systematic usage of StringPiece is encouraged as it will reduce unnecessary michael@0: // conversions from "const char*" to "string" and back again. michael@0: // michael@0: // StringPiece16 is similar to StringPiece but for base::string16 instead of michael@0: // std::string. We do not define as large of a subset of the STL functions michael@0: // from basic_string as in StringPiece, but this can be changed if these michael@0: // functions (find, find_first_of, etc.) are found to be useful in this context. michael@0: // michael@0: michael@0: #ifndef BASE_STRINGS_STRING_PIECE_H_ michael@0: #define BASE_STRINGS_STRING_PIECE_H_ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: #include "base/containers/hash_tables.h" michael@0: #include "base/strings/string16.h" michael@0: michael@0: namespace base { michael@0: michael@0: template class BasicStringPiece; michael@0: typedef BasicStringPiece StringPiece; michael@0: typedef BasicStringPiece StringPiece16; michael@0: michael@0: namespace internal { michael@0: michael@0: // Defines the types, methods, operators, and data members common to both michael@0: // StringPiece and StringPiece16. Do not refer to this class directly, but michael@0: // rather to BasicStringPiece, StringPiece, or StringPiece16. michael@0: template class StringPieceDetail { michael@0: public: michael@0: // standard STL container boilerplate michael@0: typedef size_t size_type; michael@0: typedef typename STRING_TYPE::value_type value_type; michael@0: typedef const value_type* pointer; michael@0: typedef const value_type& reference; michael@0: typedef const value_type& const_reference; michael@0: typedef ptrdiff_t difference_type; michael@0: typedef const value_type* const_iterator; michael@0: typedef std::reverse_iterator const_reverse_iterator; michael@0: michael@0: static const size_type npos; michael@0: michael@0: public: michael@0: // We provide non-explicit singleton constructors so users can pass michael@0: // in a "const char*" or a "string" wherever a "StringPiece" is michael@0: // expected (likewise for char16, string16, StringPiece16). michael@0: StringPieceDetail() : ptr_(NULL), length_(0) {} michael@0: StringPieceDetail(const value_type* str) michael@0: : ptr_(str), michael@0: length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {} michael@0: StringPieceDetail(const STRING_TYPE& str) michael@0: : ptr_(str.data()), length_(str.size()) {} michael@0: StringPieceDetail(const value_type* offset, size_type len) michael@0: : ptr_(offset), length_(len) {} michael@0: StringPieceDetail(const typename STRING_TYPE::const_iterator& begin, michael@0: const typename STRING_TYPE::const_iterator& end) michael@0: : ptr_((end > begin) ? &(*begin) : NULL), michael@0: length_((end > begin) ? (size_type)(end - begin) : 0) {} michael@0: michael@0: // data() may return a pointer to a buffer with embedded NULs, and the michael@0: // returned buffer may or may not be null terminated. Therefore it is michael@0: // typically a mistake to pass data() to a routine that expects a NUL michael@0: // terminated string. michael@0: const value_type* data() const { return ptr_; } michael@0: size_type size() const { return length_; } michael@0: size_type length() const { return length_; } michael@0: bool empty() const { return length_ == 0; } michael@0: michael@0: void clear() { michael@0: ptr_ = NULL; michael@0: length_ = 0; michael@0: } michael@0: void set(const value_type* data, size_type len) { michael@0: ptr_ = data; michael@0: length_ = len; michael@0: } michael@0: void set(const value_type* str) { michael@0: ptr_ = str; michael@0: length_ = str ? STRING_TYPE::traits_type::length(str) : 0; michael@0: } michael@0: michael@0: value_type operator[](size_type i) const { return ptr_[i]; } michael@0: michael@0: void remove_prefix(size_type n) { michael@0: ptr_ += n; michael@0: length_ -= n; michael@0: } michael@0: michael@0: void remove_suffix(size_type n) { michael@0: length_ -= n; michael@0: } michael@0: michael@0: int compare(const BasicStringPiece& x) const { michael@0: int r = wordmemcmp( michael@0: ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); michael@0: if (r == 0) { michael@0: if (length_ < x.length_) r = -1; michael@0: else if (length_ > x.length_) r = +1; michael@0: } michael@0: return r; michael@0: } michael@0: michael@0: STRING_TYPE as_string() const { michael@0: // std::string doesn't like to take a NULL pointer even with a 0 size. michael@0: return empty() ? STRING_TYPE() : STRING_TYPE(data(), size()); michael@0: } michael@0: michael@0: const_iterator begin() const { return ptr_; } michael@0: const_iterator end() const { return ptr_ + length_; } michael@0: const_reverse_iterator rbegin() const { michael@0: return const_reverse_iterator(ptr_ + length_); michael@0: } michael@0: const_reverse_iterator rend() const { michael@0: return const_reverse_iterator(ptr_); michael@0: } michael@0: michael@0: size_type max_size() const { return length_; } michael@0: size_type capacity() const { return length_; } michael@0: michael@0: static int wordmemcmp(const value_type* p, michael@0: const value_type* p2, michael@0: size_type N) { michael@0: return STRING_TYPE::traits_type::compare(p, p2, N); michael@0: } michael@0: michael@0: protected: michael@0: const value_type* ptr_; michael@0: size_type length_; michael@0: }; michael@0: michael@0: template michael@0: const typename StringPieceDetail::size_type michael@0: StringPieceDetail::npos = michael@0: typename StringPieceDetail::size_type(-1); michael@0: michael@0: // MSVC doesn't like complex extern templates and DLLs. michael@0: #if !defined(COMPILER_MSVC) michael@0: extern template class BASE_EXPORT StringPieceDetail; michael@0: extern template class BASE_EXPORT StringPieceDetail; michael@0: #endif michael@0: michael@0: BASE_EXPORT void CopyToString(const StringPiece& self, std::string* target); michael@0: BASE_EXPORT void AppendToString(const StringPiece& self, std::string* target); michael@0: BASE_EXPORT StringPieceDetail::size_type copy( michael@0: const StringPiece& self, michael@0: char* buf, michael@0: StringPieceDetail::size_type n, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find( michael@0: const StringPiece& self, michael@0: const StringPiece& s, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find( michael@0: const StringPiece& self, michael@0: char c, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type rfind( michael@0: const StringPiece& self, michael@0: const StringPiece& s, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type rfind( michael@0: const StringPiece& self, michael@0: char c, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find_first_of( michael@0: const StringPiece& self, michael@0: const StringPiece& s, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find_first_not_of( michael@0: const StringPiece& self, michael@0: const StringPiece& s, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find_first_not_of( michael@0: const StringPiece& self, michael@0: char c, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find_last_of( michael@0: const StringPiece& self, michael@0: const StringPiece& s, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find_last_of( michael@0: const StringPiece& self, michael@0: char c, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find_last_not_of( michael@0: const StringPiece& self, michael@0: const StringPiece& s, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPieceDetail::size_type find_last_not_of( michael@0: const StringPiece& self, michael@0: char c, michael@0: StringPieceDetail::size_type pos); michael@0: BASE_EXPORT StringPiece substr(const StringPiece& self, michael@0: StringPieceDetail::size_type pos, michael@0: StringPieceDetail::size_type n); michael@0: } // namespace internal michael@0: michael@0: // Defines the template type that is instantiated as either StringPiece or michael@0: // StringPiece16. michael@0: template class BasicStringPiece : michael@0: public internal::StringPieceDetail { michael@0: public: michael@0: typedef typename internal::StringPieceDetail::value_type michael@0: value_type; michael@0: typedef typename internal::StringPieceDetail::size_type michael@0: size_type; michael@0: michael@0: BasicStringPiece() {} michael@0: BasicStringPiece(const value_type*str) michael@0: : internal::StringPieceDetail(str) {} michael@0: BasicStringPiece(const STRING_TYPE& str) michael@0: : internal::StringPieceDetail(str) {} michael@0: BasicStringPiece(const value_type* offset, size_type len) michael@0: : internal::StringPieceDetail(offset, len) {} michael@0: BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, michael@0: const typename STRING_TYPE::const_iterator& end) michael@0: : internal::StringPieceDetail(begin, end) {} michael@0: }; michael@0: michael@0: // Specializes BasicStringPiece for std::string to add a few operations that michael@0: // are not needed for string16. michael@0: template <> class BasicStringPiece : michael@0: public internal::StringPieceDetail { michael@0: public: michael@0: BasicStringPiece() {} michael@0: BasicStringPiece(const char* str) michael@0: : internal::StringPieceDetail(str) {} michael@0: BasicStringPiece(const std::string& str) michael@0: : internal::StringPieceDetail(str) {} michael@0: BasicStringPiece(const char* offset, size_type len) michael@0: : internal::StringPieceDetail(offset, len) {} michael@0: BasicStringPiece(const std::string::const_iterator& begin, michael@0: const std::string::const_iterator& end) michael@0: : internal::StringPieceDetail(begin, end) {} michael@0: michael@0: // Prevent the following overload of set() from hiding the definitions in the michael@0: // base class. michael@0: using internal::StringPieceDetail::set; michael@0: michael@0: void set(const void* data, size_type len) { michael@0: ptr_ = reinterpret_cast(data); michael@0: length_ = len; michael@0: } michael@0: michael@0: void CopyToString(std::string* target) const { michael@0: internal::CopyToString(*this, target); michael@0: } michael@0: michael@0: void AppendToString(std::string* target) const { michael@0: internal::AppendToString(*this, target); michael@0: } michael@0: michael@0: // Does "this" start with "x" michael@0: bool starts_with(const BasicStringPiece& x) const { michael@0: return ((length_ >= x.length_) && michael@0: (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); michael@0: } michael@0: michael@0: // Does "this" end with "x" michael@0: bool ends_with(const BasicStringPiece& x) const { michael@0: return ((length_ >= x.length_) && michael@0: (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); michael@0: } michael@0: michael@0: size_type copy(char* buf, size_type n, size_type pos = 0) const { michael@0: return internal::copy(*this, buf, n, pos); michael@0: } michael@0: michael@0: size_type find(const BasicStringPiece& s, size_type pos = 0) const { michael@0: return internal::find(*this, s, pos); michael@0: } michael@0: michael@0: size_type find(char c, size_type pos = 0) const { michael@0: return internal::find(*this, c, pos); michael@0: } michael@0: michael@0: size_type rfind(const BasicStringPiece& s, size_type pos = npos) const { michael@0: return internal::rfind(*this, s, pos); michael@0: } michael@0: michael@0: size_type rfind(char c, size_type pos = npos) const { michael@0: return internal::rfind(*this, c, pos); michael@0: } michael@0: michael@0: size_type find_first_of(const BasicStringPiece& s, size_type pos = 0) const { michael@0: return internal::find_first_of(*this, s, pos); michael@0: } michael@0: michael@0: size_type find_first_of(char c, size_type pos = 0) const { michael@0: return find(c, pos); michael@0: } michael@0: michael@0: size_type find_first_not_of(const BasicStringPiece& s, michael@0: size_type pos = 0) const { michael@0: return internal::find_first_not_of(*this, s, pos); michael@0: } michael@0: michael@0: size_type find_first_not_of(char c, size_type pos = 0) const { michael@0: return internal::find_first_not_of(*this, c, pos); michael@0: } michael@0: michael@0: size_type find_last_of(const BasicStringPiece& s, michael@0: size_type pos = npos) const { michael@0: return internal::find_last_of(*this, s, pos); michael@0: } michael@0: michael@0: size_type find_last_of(char c, size_type pos = npos) const { michael@0: return rfind(c, pos); michael@0: } michael@0: michael@0: size_type find_last_not_of(const BasicStringPiece& s, michael@0: size_type pos = npos) const { michael@0: return internal::find_last_not_of(*this, s, pos); michael@0: } michael@0: michael@0: size_type find_last_not_of(char c, size_type pos = npos) const { michael@0: return internal::find_last_not_of(*this, c, pos); michael@0: } michael@0: michael@0: BasicStringPiece substr(size_type pos, size_type n = npos) const { michael@0: return internal::substr(*this, pos, n); michael@0: } michael@0: }; michael@0: michael@0: // MSVC doesn't like complex extern templates and DLLs. michael@0: #if !defined(COMPILER_MSVC) michael@0: // We can't explicitly declare the std::string instantiation here because it was michael@0: // already instantiated when specialized, above. Not only is it a no-op, but michael@0: // currently it also crashes Clang (see http://crbug.com/107412). michael@0: extern template class BASE_EXPORT BasicStringPiece; michael@0: #endif michael@0: michael@0: BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y); michael@0: michael@0: inline bool operator!=(const StringPiece& x, const StringPiece& y) { michael@0: return !(x == y); michael@0: } michael@0: michael@0: inline bool operator<(const StringPiece& x, const StringPiece& y) { michael@0: const int r = StringPiece::wordmemcmp( michael@0: x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); michael@0: return ((r < 0) || ((r == 0) && (x.size() < y.size()))); michael@0: } michael@0: michael@0: inline bool operator>(const StringPiece& x, const StringPiece& y) { michael@0: return y < x; michael@0: } michael@0: michael@0: inline bool operator<=(const StringPiece& x, const StringPiece& y) { michael@0: return !(x > y); michael@0: } michael@0: michael@0: inline bool operator>=(const StringPiece& x, const StringPiece& y) { michael@0: return !(x < y); michael@0: } michael@0: michael@0: inline bool operator==(const StringPiece16& x, const StringPiece16& y) { michael@0: if (x.size() != y.size()) michael@0: return false; michael@0: michael@0: return StringPiece16::wordmemcmp(x.data(), y.data(), x.size()) == 0; michael@0: } michael@0: michael@0: inline bool operator!=(const StringPiece16& x, const StringPiece16& y) { michael@0: return !(x == y); michael@0: } michael@0: michael@0: inline bool operator<(const StringPiece16& x, const StringPiece16& y) { michael@0: const int r = StringPiece16::wordmemcmp( michael@0: x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); michael@0: return ((r < 0) || ((r == 0) && (x.size() < y.size()))); michael@0: } michael@0: michael@0: inline bool operator>(const StringPiece16& x, const StringPiece16& y) { michael@0: return y < x; michael@0: } michael@0: michael@0: inline bool operator<=(const StringPiece16& x, const StringPiece16& y) { michael@0: return !(x > y); michael@0: } michael@0: michael@0: inline bool operator>=(const StringPiece16& x, const StringPiece16& y) { michael@0: return !(x < y); michael@0: } michael@0: michael@0: BASE_EXPORT std::ostream& operator<<(std::ostream& o, michael@0: const StringPiece& piece); michael@0: michael@0: } // namespace base michael@0: michael@0: // We provide appropriate hash functions so StringPiece and StringPiece16 can michael@0: // be used as keys in hash sets and maps. michael@0: michael@0: // This hash function is copied from base/containers/hash_tables.h. We don't michael@0: // use the ones already defined for string and string16 directly because it michael@0: // would require the string constructors to be called, which we don't want. michael@0: #define HASH_STRING_PIECE(StringPieceType, string_piece) \ michael@0: std::size_t result = 0; \ michael@0: for (StringPieceType::const_iterator i = string_piece.begin(); \ michael@0: i != string_piece.end(); ++i) \ michael@0: result = (result * 131) + *i; \ michael@0: return result; \ michael@0: michael@0: namespace BASE_HASH_NAMESPACE { michael@0: #if defined(COMPILER_GCC) michael@0: michael@0: template<> michael@0: struct hash { michael@0: std::size_t operator()(const base::StringPiece& sp) const { michael@0: HASH_STRING_PIECE(base::StringPiece, sp); michael@0: } michael@0: }; michael@0: template<> michael@0: struct hash { michael@0: std::size_t operator()(const base::StringPiece16& sp16) const { michael@0: HASH_STRING_PIECE(base::StringPiece16, sp16); michael@0: } michael@0: }; michael@0: michael@0: #elif defined(COMPILER_MSVC) michael@0: michael@0: inline size_t hash_value(const base::StringPiece& sp) { michael@0: HASH_STRING_PIECE(base::StringPiece, sp); michael@0: } michael@0: inline size_t hash_value(const base::StringPiece16& sp16) { michael@0: HASH_STRING_PIECE(base::StringPiece16, sp16); michael@0: } michael@0: michael@0: #endif // COMPILER michael@0: michael@0: } // namespace BASE_HASH_NAMESPACE michael@0: michael@0: #endif // BASE_STRINGS_STRING_PIECE_H_