Index: base/strings/string_piece.h |
diff --git a/base/strings/string_piece.h b/base/strings/string_piece.h |
index 818d6ca598b11fcfaf467b53b95063e7ae66098a..38e2277e98c874c9db56d5b9cc454f24c972249f 100644 |
--- a/base/strings/string_piece.h |
+++ b/base/strings/string_piece.h |
@@ -39,14 +39,124 @@ template <typename STRING_TYPE> class BasicStringPiece; |
typedef BasicStringPiece<std::string> StringPiece; |
typedef BasicStringPiece<string16> StringPiece16; |
+// internal -------------------------------------------------------------------- |
+ |
+// Many of the StringPiece functions use different implementations for the |
+// 8-bit and 16-bit versions, and we don't want lots of template expansions in |
+// this (very common) header that will slow down compilation. |
+// |
+// So here we define overloaded functions called by the StringPiece template. |
+// For those that share an implementation, the two versions will expand to a |
+// template internal to the .cc file. |
namespace internal { |
+BASE_EXPORT void CopyToString(const StringPiece& self, std::string* target); |
+BASE_EXPORT void CopyToString(const StringPiece16& self, string16* target); |
+ |
+BASE_EXPORT void AppendToString(const StringPiece& self, std::string* target); |
+BASE_EXPORT void AppendToString(const StringPiece16& self, string16* target); |
+ |
+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); |
+ |
+BASE_EXPORT StringPiece substr(const StringPiece& self, |
+ size_t pos, |
+ size_t n); |
+BASE_EXPORT StringPiece16 substr(const StringPiece16& self, |
+ size_t pos, |
+ size_t n); |
+ |
+} // namespace internal |
+ |
+// BasicStringPiece ------------------------------------------------------------ |
+ |
// Defines the types, methods, operators, and data members common to both |
// StringPiece and StringPiece16. Do not refer to this class directly, but |
// rather to BasicStringPiece, StringPiece, or StringPiece16. |
-template <typename STRING_TYPE> class StringPieceDetail { |
+// |
+// This is templatized by string class type rather than character type, so |
+// BasicStringPiece<std::string> or BasicStringPiece<base::string16>. |
+template <typename STRING_TYPE> class BasicStringPiece { |
public: |
- // standard STL container boilerplate |
+ // Standard STL container boilerplate. |
typedef size_t size_type; |
typedef typename STRING_TYPE::value_type value_type; |
typedef const value_type* pointer; |
@@ -62,15 +172,15 @@ template <typename STRING_TYPE> class StringPieceDetail { |
// We provide non-explicit singleton constructors so users can pass |
// in a "const char*" or a "string" wherever a "StringPiece" is |
// expected (likewise for char16, string16, StringPiece16). |
- StringPieceDetail() : ptr_(NULL), length_(0) {} |
- StringPieceDetail(const value_type* str) |
+ BasicStringPiece() : ptr_(NULL), length_(0) {} |
+ BasicStringPiece(const value_type* str) |
: ptr_(str), |
length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {} |
- StringPieceDetail(const STRING_TYPE& str) |
+ BasicStringPiece(const STRING_TYPE& str) |
: ptr_(str.data()), length_(str.size()) {} |
- StringPieceDetail(const value_type* offset, size_type len) |
+ BasicStringPiece(const value_type* offset, size_type len) |
: ptr_(offset), length_(len) {} |
- StringPieceDetail(const typename STRING_TYPE::const_iterator& begin, |
+ BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, |
const typename STRING_TYPE::const_iterator& end) |
: ptr_((end > begin) ? &(*begin) : NULL), |
length_((end > begin) ? (size_type)(end - begin) : 0) {} |
@@ -141,213 +251,113 @@ template <typename STRING_TYPE> class StringPieceDetail { |
return STRING_TYPE::traits_type::compare(p, p2, N); |
} |
- protected: |
- const value_type* ptr_; |
- size_type length_; |
-}; |
- |
-template <typename STRING_TYPE> |
-const typename StringPieceDetail<STRING_TYPE>::size_type |
-StringPieceDetail<STRING_TYPE>::npos = |
- typename StringPieceDetail<STRING_TYPE>::size_type(-1); |
- |
-// MSVC doesn't like complex extern templates and DLLs. |
-#if !defined(COMPILER_MSVC) |
-extern template class BASE_EXPORT StringPieceDetail<std::string>; |
-extern template class BASE_EXPORT StringPieceDetail<string16>; |
-#endif |
- |
-BASE_EXPORT void CopyToString(const StringPiece& self, std::string* target); |
-BASE_EXPORT void AppendToString(const StringPiece& self, std::string* target); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type copy( |
- const StringPiece& self, |
- char* buf, |
- StringPieceDetail<std::string>::size_type n, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find( |
- const StringPiece& self, |
- const StringPiece& s, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find( |
- const StringPiece& self, |
- char c, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type rfind( |
- const StringPiece& self, |
- const StringPiece& s, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type rfind( |
- const StringPiece& self, |
- char c, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find_first_of( |
- const StringPiece& self, |
- const StringPiece& s, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find_first_not_of( |
- const StringPiece& self, |
- const StringPiece& s, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find_first_not_of( |
- const StringPiece& self, |
- char c, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_of( |
- const StringPiece& self, |
- const StringPiece& s, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_of( |
- const StringPiece& self, |
- char c, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_not_of( |
- const StringPiece& self, |
- const StringPiece& s, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_not_of( |
- const StringPiece& self, |
- char c, |
- StringPieceDetail<std::string>::size_type pos); |
-BASE_EXPORT StringPiece substr(const StringPiece& self, |
- StringPieceDetail<std::string>::size_type pos, |
- StringPieceDetail<std::string>::size_type n); |
-} // namespace internal |
- |
-// Defines the template type that is instantiated as either StringPiece or |
-// StringPiece16. |
-template <typename STRING_TYPE> class BasicStringPiece : |
- public internal::StringPieceDetail<STRING_TYPE> { |
- public: |
- typedef typename internal::StringPieceDetail<STRING_TYPE>::value_type |
- value_type; |
- typedef typename internal::StringPieceDetail<STRING_TYPE>::size_type |
- size_type; |
- |
- BasicStringPiece() {} |
- BasicStringPiece(const value_type*str) |
- : internal::StringPieceDetail<STRING_TYPE>(str) {} |
- BasicStringPiece(const STRING_TYPE& str) |
- : internal::StringPieceDetail<STRING_TYPE>(str) {} |
- BasicStringPiece(const value_type* offset, size_type len) |
- : internal::StringPieceDetail<STRING_TYPE>(offset, len) {} |
- BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, |
- const typename STRING_TYPE::const_iterator& end) |
- : internal::StringPieceDetail<STRING_TYPE>(begin, end) {} |
-}; |
- |
-// Specializes BasicStringPiece for std::string to add a few operations that |
-// are not needed for string16. |
-template <> class BasicStringPiece<std::string> : |
- public internal::StringPieceDetail<std::string> { |
- public: |
- BasicStringPiece() {} |
- BasicStringPiece(const char* str) |
- : internal::StringPieceDetail<std::string>(str) {} |
- BasicStringPiece(const std::string& str) |
- : internal::StringPieceDetail<std::string>(str) {} |
- BasicStringPiece(const char* offset, size_type len) |
- : internal::StringPieceDetail<std::string>(offset, len) {} |
- BasicStringPiece(const std::string::const_iterator& begin, |
- const std::string::const_iterator& end) |
- : internal::StringPieceDetail<std::string>(begin, end) {} |
- |
- // Prevent the following overload of set() from hiding the definitions in the |
- // base class. |
- using internal::StringPieceDetail<std::string>::set; |
- |
- void set(const void* data, size_type len) { |
- ptr_ = reinterpret_cast<const value_type*>(data); |
- length_ = len; |
- } |
- |
- void CopyToString(std::string* target) const { |
+ // Sets the value of the given string target type to be the current string. |
+ // This saves a temporary over doing |a = b.as_string()| |
+ void CopyToString(STRING_TYPE* target) const { |
internal::CopyToString(*this, target); |
} |
- void AppendToString(std::string* target) const { |
+ void AppendToString(STRING_TYPE* target) const { |
internal::AppendToString(*this, target); |
} |
+ size_type copy(value_type* buf, size_type n, size_type pos = 0) const { |
+ return internal::copy(*this, buf, n, pos); |
+ } |
+ |
// Does "this" start with "x" |
bool starts_with(const BasicStringPiece& x) const { |
- return ((length_ >= x.length_) && |
- (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); |
+ return ((this->length_ >= x.length_) && |
+ (wordmemcmp(this->ptr_, x.ptr_, x.length_) == 0)); |
} |
// Does "this" end with "x" |
bool ends_with(const BasicStringPiece& x) const { |
- return ((length_ >= x.length_) && |
- (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
- } |
- |
- size_type copy(char* buf, size_type n, size_type pos = 0) const { |
- return internal::copy(*this, buf, n, pos); |
+ return ((this->length_ >= x.length_) && |
+ (wordmemcmp(this->ptr_ + (this->length_-x.length_), |
+ x.ptr_, x.length_) == 0)); |
} |
- size_type find(const BasicStringPiece& s, size_type pos = 0) const { |
+ // find: Search for a character or substring at a given offset. |
+ size_type find(const BasicStringPiece<STRING_TYPE>& s, |
+ size_type pos = 0) const { |
return internal::find(*this, s, pos); |
} |
- |
- size_type find(char c, size_type pos = 0) const { |
+ 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 = npos) const { |
+ // rfind: Reverse find. |
+ size_type rfind(const BasicStringPiece& s, |
+ size_type pos = BasicStringPiece::npos) const { |
return internal::rfind(*this, s, pos); |
} |
- |
- size_type rfind(char c, size_type pos = npos) const { |
+ 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 { |
+ // find_first_of: Find the first occurence of one of a set of characters. |
+ 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(char c, size_type pos = 0) const { |
+ size_type find_first_of(value_type c, size_type pos = 0) const { |
return find(c, pos); |
} |
+ // find_first_not_of: Find the first occurence not of a set of characters. |
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(char c, size_type pos = 0) const { |
+ size_type find_first_not_of(value_type c, size_type pos = 0) const { |
return internal::find_first_not_of(*this, c, pos); |
} |
+ // find_last_of: Find the last occurence of one of a set of characters. |
size_type find_last_of(const BasicStringPiece& s, |
- size_type pos = npos) const { |
+ size_type pos = BasicStringPiece::npos) const { |
return internal::find_last_of(*this, s, pos); |
} |
- |
- size_type find_last_of(char c, size_type pos = npos) const { |
+ size_type find_last_of(value_type c, |
+ size_type pos = BasicStringPiece::npos) const { |
return rfind(c, pos); |
} |
+ // find_last_not_of: Find the last occurence not of a set of characters. |
size_type find_last_not_of(const BasicStringPiece& s, |
- size_type pos = npos) const { |
+ size_type pos = BasicStringPiece::npos) const { |
return internal::find_last_not_of(*this, s, pos); |
} |
- |
- size_type find_last_not_of(char c, size_type pos = npos) const { |
+ size_type find_last_not_of(value_type c, |
+ size_type pos = BasicStringPiece::npos) const { |
return internal::find_last_not_of(*this, c, pos); |
} |
- BasicStringPiece substr(size_type pos, size_type n = npos) const { |
+ // substr. |
+ BasicStringPiece substr(size_type pos, |
+ size_type n = BasicStringPiece::npos) const { |
return internal::substr(*this, pos, n); |
} |
+ |
+ 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); |
+ |
// MSVC doesn't like complex extern templates and DLLs. |
#if !defined(COMPILER_MSVC) |
-// We can't explicitly declare the std::string instantiation here because it was |
-// already instantiated when specialized, above. Not only is it a no-op, but |
-// currently it also crashes Clang (see http://crbug.com/107412). |
+extern template class BASE_EXPORT BasicStringPiece<std::string>; |
extern template class BASE_EXPORT BasicStringPiece<string16>; |
#endif |
+// StingPiece operators -------------------------------------------------------- |
+ |
BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y); |
inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
@@ -372,6 +382,8 @@ inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
return !(x < y); |
} |
+// StringPiece16 operators ----------------------------------------------------- |
+ |
inline bool operator==(const StringPiece16& x, const StringPiece16& y) { |
if (x.size() != y.size()) |
return false; |
@@ -406,6 +418,8 @@ BASE_EXPORT std::ostream& operator<<(std::ostream& o, |
} // namespace base |
+// Hashing --------------------------------------------------------------------- |
+ |
// We provide appropriate hash functions so StringPiece and StringPiece16 can |
// be used as keys in hash sets and maps. |