Index: trunk/src/base/strings/string_piece.h |
=================================================================== |
--- trunk/src/base/strings/string_piece.h (revision 255491) |
+++ trunk/src/base/strings/string_piece.h (working copy) |
@@ -39,124 +39,14 @@ |
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. |
-// |
-// 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 { |
+template <typename STRING_TYPE> class StringPieceDetail { |
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; |
@@ -172,15 +62,15 @@ |
// 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). |
- BasicStringPiece() : ptr_(NULL), length_(0) {} |
- BasicStringPiece(const value_type* str) |
+ StringPieceDetail() : ptr_(NULL), length_(0) {} |
+ StringPieceDetail(const value_type* str) |
: ptr_(str), |
length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {} |
- BasicStringPiece(const STRING_TYPE& str) |
+ StringPieceDetail(const STRING_TYPE& str) |
: ptr_(str.data()), length_(str.size()) {} |
- BasicStringPiece(const value_type* offset, size_type len) |
+ StringPieceDetail(const value_type* offset, size_type len) |
: ptr_(offset), length_(len) {} |
- BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, |
+ StringPieceDetail(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) {} |
@@ -251,113 +141,213 @@ |
return STRING_TYPE::traits_type::compare(p, p2, N); |
} |
- // 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 { |
+ 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 { |
internal::CopyToString(*this, target); |
} |
- void AppendToString(STRING_TYPE* target) const { |
+ void AppendToString(std::string* 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 ((this->length_ >= x.length_) && |
- (wordmemcmp(this->ptr_, x.ptr_, x.length_) == 0)); |
+ return ((length_ >= x.length_) && |
+ (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); |
} |
// Does "this" end with "x" |
bool ends_with(const BasicStringPiece& x) const { |
- return ((this->length_ >= x.length_) && |
- (wordmemcmp(this->ptr_ + (this->length_-x.length_), |
- x.ptr_, x.length_) == 0)); |
+ return ((length_ >= x.length_) && |
+ (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
} |
- // find: Search for a character or substring at a given offset. |
- size_type find(const BasicStringPiece<STRING_TYPE>& s, |
- size_type pos = 0) const { |
+ size_type copy(char* buf, size_type n, size_type pos = 0) const { |
+ return internal::copy(*this, buf, n, pos); |
+ } |
+ |
+ size_type find(const BasicStringPiece& s, size_type pos = 0) const { |
return internal::find(*this, s, pos); |
} |
- size_type find(value_type c, size_type pos = 0) const { |
+ |
+ size_type find(char c, size_type pos = 0) const { |
return internal::find(*this, c, pos); |
} |
- // rfind: Reverse find. |
- size_type rfind(const BasicStringPiece& s, |
- size_type pos = BasicStringPiece::npos) const { |
+ size_type rfind(const BasicStringPiece& s, size_type pos = npos) const { |
return internal::rfind(*this, s, pos); |
} |
- size_type rfind(value_type c, size_type pos = BasicStringPiece::npos) const { |
+ |
+ size_type rfind(char c, size_type pos = npos) const { |
return internal::rfind(*this, c, pos); |
} |
- // 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 { |
+ 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 { |
+ |
+ size_type find_first_of(char 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(value_type c, size_type pos = 0) const { |
+ |
+ size_type find_first_not_of(char 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 = BasicStringPiece::npos) const { |
+ size_type pos = npos) const { |
return internal::find_last_of(*this, s, pos); |
} |
- size_type find_last_of(value_type c, |
- size_type pos = BasicStringPiece::npos) const { |
+ |
+ size_type find_last_of(char c, size_type pos = 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 = BasicStringPiece::npos) const { |
+ size_type pos = 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 { |
+ |
+ size_type find_last_not_of(char c, size_type pos = npos) const { |
return internal::find_last_not_of(*this, c, pos); |
} |
- // substr. |
- BasicStringPiece substr(size_type pos, |
- size_type n = BasicStringPiece::npos) const { |
+ BasicStringPiece substr(size_type pos, size_type n = 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) |
-extern template class BASE_EXPORT BasicStringPiece<std::string>; |
+// 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<string16>; |
#endif |
-// StingPiece operators -------------------------------------------------------- |
- |
BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y); |
inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
@@ -382,8 +372,6 @@ |
return !(x < y); |
} |
-// StringPiece16 operators ----------------------------------------------------- |
- |
inline bool operator==(const StringPiece16& x, const StringPiece16& y) { |
if (x.size() != y.size()) |
return false; |
@@ -418,8 +406,6 @@ |
} // namespace base |
-// Hashing --------------------------------------------------------------------- |
- |
// We provide appropriate hash functions so StringPiece and StringPiece16 can |
// be used as keys in hash sets and maps. |