summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/i18n/case_conversion.h1
-rw-r--r--base/logging.h3
-rw-r--r--base/string_piece.cc55
-rw-r--r--base/string_piece.h258
-rw-r--r--base/string_piece_unittest.cc465
-rw-r--r--base/utf_offset_string_conversions.h6
6 files changed, 449 insertions, 339 deletions
diff --git a/base/i18n/case_conversion.h b/base/i18n/case_conversion.h
index c8d84f8..be321d4 100644
--- a/base/i18n/case_conversion.h
+++ b/base/i18n/case_conversion.h
@@ -7,7 +7,6 @@
#pragma once
#include "base/i18n/base_i18n_export.h"
-#include "base/string16.h"
#include "base/string_piece.h"
namespace base {
diff --git a/base/logging.h b/base/logging.h
index 9c660fa..94a82ad 100644
--- a/base/logging.h
+++ b/base/logging.h
@@ -983,8 +983,7 @@ inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
namespace base {
-template <typename STRING_TYPE> class BasicStringPiece;
-typedef BasicStringPiece<std::string> StringPiece;
+class StringPiece;
// Allows StringPiece to be logged.
BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
diff --git a/base/string_piece.cc b/base/string_piece.cc
index 2960702..bf6291c 100644
--- a/base/string_piece.cc
+++ b/base/string_piece.cc
@@ -10,17 +10,6 @@
namespace base {
-// MSVC doesn't like complex extern templates and DLLs.
-#if !defined(COMPILER_MSVC)
-namespace internal {
-template class StringPieceDetail<std::string>;
-template class StringPieceDetail<string16>;
-} // namespace internal
-
-template class BasicStringPiece<std::string>;
-template class BasicStringPiece<string16>;
-#endif
-
typedef StringPiece::size_type size_type;
bool operator==(const StringPiece& x, const StringPiece& y) {
@@ -30,25 +19,22 @@ bool operator==(const StringPiece& x, const StringPiece& y) {
return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
}
-void BasicStringPiece<std::string>::CopyToString(std::string* target) const {
+void StringPiece::CopyToString(std::string* target) const {
target->assign(!empty() ? data() : "", size());
}
-void BasicStringPiece<std::string>::AppendToString(std::string* target) const {
+void StringPiece::AppendToString(std::string* target) const {
if (!empty())
target->append(data(), size());
}
-size_type BasicStringPiece<std::string>::copy(char* buf,
- size_type n,
- size_type pos) const {
+size_type StringPiece::copy(char* buf, size_type n, size_type pos) const {
size_type ret = std::min(length_ - pos, n);
memcpy(buf, ptr_ + pos, ret);
return ret;
}
-size_type BasicStringPiece<std::string>::find(const BasicStringPiece& s,
- size_type pos) const {
+size_type StringPiece::find(const StringPiece& s, size_type pos) const {
if (pos > length_)
return npos;
@@ -58,7 +44,7 @@ size_type BasicStringPiece<std::string>::find(const BasicStringPiece& s,
return xpos + s.length_ <= length_ ? xpos : npos;
}
-size_type BasicStringPiece<std::string>::find(char c, size_type pos) const {
+size_type StringPiece::find(char c, size_type pos) const {
if (pos >= length_)
return npos;
@@ -66,8 +52,7 @@ size_type BasicStringPiece<std::string>::find(char c, size_type pos) const {
return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos;
}
-size_type BasicStringPiece<std::string>::rfind(const BasicStringPiece& s,
- size_type pos) const {
+size_type StringPiece::rfind(const StringPiece& s, size_type pos) const {
if (length_ < s.length_)
return npos;
@@ -79,7 +64,7 @@ size_type BasicStringPiece<std::string>::rfind(const BasicStringPiece& s,
return result != last ? static_cast<size_t>(result - ptr_) : npos;
}
-size_type BasicStringPiece<std::string>::rfind(char c, size_type pos) const {
+size_type StringPiece::rfind(char c, size_type pos) const {
if (length_ == 0)
return npos;
@@ -109,8 +94,8 @@ static inline void BuildLookupTable(const StringPiece& characters_wanted,
}
}
-size_type BasicStringPiece<std::string>::find_first_of(
- const BasicStringPiece& s, size_type pos) const {
+size_type StringPiece::find_first_of(const StringPiece& s,
+ size_type pos) const {
if (length_ == 0 || s.length_ == 0)
return npos;
@@ -128,8 +113,8 @@ size_type BasicStringPiece<std::string>::find_first_of(
return npos;
}
-size_type BasicStringPiece<std::string>::find_first_not_of(
- const BasicStringPiece& s, size_type pos) const {
+size_type StringPiece::find_first_not_of(const StringPiece& s,
+ size_type pos) const {
if (length_ == 0)
return npos;
@@ -150,8 +135,7 @@ size_type BasicStringPiece<std::string>::find_first_not_of(
return npos;
}
-size_type BasicStringPiece<std::string>::find_first_not_of(
- char c, size_type pos) const {
+size_type StringPiece::find_first_not_of(char c, size_type pos) const {
if (length_ == 0)
return npos;
@@ -163,8 +147,7 @@ size_type BasicStringPiece<std::string>::find_first_not_of(
return npos;
}
-size_type BasicStringPiece<std::string>::find_last_of(const StringPiece& s,
- size_type pos) const {
+size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const {
if (length_ == 0 || s.length_ == 0)
return npos;
@@ -183,8 +166,8 @@ size_type BasicStringPiece<std::string>::find_last_of(const StringPiece& s,
return npos;
}
-size_type BasicStringPiece<std::string>::find_last_not_of(
- const BasicStringPiece& s, size_type pos) const {
+size_type StringPiece::find_last_not_of(const StringPiece& s,
+ size_type pos) const {
if (length_ == 0)
return npos;
@@ -207,8 +190,7 @@ size_type BasicStringPiece<std::string>::find_last_not_of(
return npos;
}
-size_type BasicStringPiece<std::string>::find_last_not_of(char c,
- size_type pos) const {
+size_type StringPiece::find_last_not_of(char c, size_type pos) const {
if (length_ == 0)
return npos;
@@ -221,11 +203,12 @@ size_type BasicStringPiece<std::string>::find_last_not_of(char c,
return npos;
}
-BasicStringPiece<std::string> BasicStringPiece<std::string>::substr(
- size_type pos, size_type n) const {
+StringPiece StringPiece::substr(size_type pos, size_type n) const {
if (pos > length_) pos = length_;
if (n > length_ - pos) n = length_ - pos;
return StringPiece(ptr_ + pos, n);
}
+const StringPiece::size_type StringPiece::npos = size_type(-1);
+
} // namespace base
diff --git a/base/string_piece.h b/base/string_piece.h
index f80ca5b..278c7b6 100644
--- a/base/string_piece.h
+++ b/base/string_piece.h
@@ -33,47 +33,39 @@
namespace base {
-template <typename STRING_TYPE> class BasicStringPiece;
-
-namespace internal {
-
-// 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 {
+class BASE_EXPORT StringPiece {
public:
// standard STL container boilerplate
typedef size_t size_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 char value_type;
+ typedef const char* pointer;
+ typedef const char& reference;
+ typedef const char& const_reference;
typedef ptrdiff_t difference_type;
- typedef const value_type* const_iterator;
+ typedef const char* const_iterator;
+ typedef const char* iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
+ typedef std::reverse_iterator<iterator> reverse_iterator;
static const size_type npos;
public:
// 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)
- : ptr_(str),
- length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {}
- StringPieceDetail(const STRING_TYPE& str)
- : ptr_(str.data()),
- length_(str.size()) {}
- StringPieceDetail(const value_type* offset, size_type len)
- : ptr_(offset),
- length_(len) {}
+ // expected.
+ StringPiece() : ptr_(NULL), length_(0) { }
+ StringPiece(const char* str)
+ : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { }
+ StringPiece(const std::string& str)
+ : ptr_(str.data()), length_(str.size()) { }
+ StringPiece(const char* offset, size_type len)
+ : ptr_(offset), length_(len) { }
// data() may return a pointer to a buffer with embedded NULs, and the
// returned buffer may or may not be null terminated. Therefore it is
// typically a mistake to pass data() to a routine that expects a NUL
// terminated string.
- const value_type* data() const { return ptr_; }
+ const char* data() const { return ptr_; }
size_type size() const { return length_; }
size_type length() const { return length_; }
bool empty() const { return length_ == 0; }
@@ -82,16 +74,20 @@ template <typename STRING_TYPE> class StringPieceDetail {
ptr_ = NULL;
length_ = 0;
}
- void set(const value_type* data, size_type len) {
+ void set(const char* data, size_type len) {
ptr_ = data;
length_ = len;
}
- void set(const value_type* str) {
+ void set(const char* str) {
ptr_ = str;
- length_ = str ? STRING_TYPE::traits_type::length(str) : 0;
+ length_ = str ? strlen(str) : 0;
+ }
+ void set(const void* data, size_type len) {
+ ptr_ = reinterpret_cast<const char*>(data);
+ length_ = len;
}
- value_type operator[](size_type i) const { return ptr_[i]; }
+ char operator[](size_type i) const { return ptr_[i]; }
void remove_prefix(size_type n) {
ptr_ += n;
@@ -102,7 +98,7 @@ template <typename STRING_TYPE> class StringPieceDetail {
length_ -= n;
}
- int compare(const BasicStringPiece<STRING_TYPE>& x) const {
+ int compare(const StringPiece& x) const {
int r = wordmemcmp(
ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_));
if (r == 0) {
@@ -112,13 +108,28 @@ template <typename STRING_TYPE> class StringPieceDetail {
return r;
}
- STRING_TYPE as_string() const {
+ std::string as_string() const {
// std::string doesn't like to take a NULL pointer even with a 0 size.
- return empty() ? STRING_TYPE() : STRING_TYPE(data(), size());
+ return std::string(!empty() ? data() : "", size());
+ }
+
+ void CopyToString(std::string* target) const;
+ void AppendToString(std::string* target) const;
+
+ // Does "this" start with "x"
+ bool starts_with(const StringPiece& x) const {
+ return ((length_ >= x.length_) &&
+ (wordmemcmp(ptr_, x.ptr_, x.length_) == 0));
}
- const_iterator begin() const { return ptr_; }
- const_iterator end() const { return ptr_ + length_; }
+ // Does "this" end with "x"
+ bool ends_with(const StringPiece& x) const {
+ return ((length_ >= x.length_) &&
+ (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0));
+ }
+
+ iterator begin() const { return ptr_; }
+ iterator end() const { return ptr_ + length_; }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(ptr_ + length_);
}
@@ -129,119 +140,114 @@ template <typename STRING_TYPE> class StringPieceDetail {
size_type max_size() const { return length_; }
size_type capacity() const { return length_; }
- static int wordmemcmp(const value_type* p,
- const value_type* p2,
- size_type N) {
- return STRING_TYPE::traits_type::compare(p, p2, N);
- }
+ size_type copy(char* buf, size_type n, size_type pos = 0) const;
- protected:
- const value_type* ptr_;
- size_type length_;
-};
+ size_type find(const StringPiece& s, size_type pos = 0) const;
+ size_type find(char c, size_type pos = 0) const;
+ size_type rfind(const StringPiece& s, size_type pos = npos) const;
+ size_type rfind(char c, size_type pos = npos) const;
-template <typename STRING_TYPE>
-const typename StringPieceDetail<STRING_TYPE>::size_type
-StringPieceDetail<STRING_TYPE>::npos =
- typename StringPieceDetail<STRING_TYPE>::size_type(-1);
+ size_type find_first_of(const StringPiece& s, size_type pos = 0) const;
+ size_type find_first_of(char c, size_type pos = 0) const {
+ return find(c, pos);
+ }
+ size_type find_first_not_of(const StringPiece& s, size_type pos = 0) const;
+ size_type find_first_not_of(char c, size_type pos = 0) const;
+ size_type find_last_of(const StringPiece& s, size_type pos = npos) const;
+ size_type find_last_of(char c, size_type pos = npos) const {
+ return rfind(c, pos);
+ }
+ size_type find_last_not_of(const StringPiece& s, size_type pos = npos) const;
+ size_type find_last_not_of(char c, size_type pos = npos) const;
-// 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
+ StringPiece substr(size_type pos, size_type n = npos) const;
-} // namespace internal
+ static int wordmemcmp(const char* p, const char* p2, size_type N) {
+ return memcmp(p, p2, N);
+ }
-// 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) {}
+ private:
+ const char* ptr_;
+ size_type length_;
};
-// Specializes BasicStringPiece for std::string to add a few operations that
-// are not needed for string16.
-template <> class BASE_EXPORT BasicStringPiece<std::string> :
- public internal::StringPieceDetail<std::string> {
+class BASE_EXPORT StringPiece16 {
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) {}
-
- // Prevent the following overload of set() from hiding the definitions in the
- // base class.
- using internal::StringPieceDetail<std::string>::set;
+ // standard STL container boilerplate
+ typedef size_t size_type;
+ typedef char16 value_type;
+ typedef const char16* pointer;
+ typedef const char16& reference;
+ typedef const char16& const_reference;
+ typedef ptrdiff_t difference_type;
+ typedef const char16* const_iterator;
+ typedef const char16* iterator;
+ typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
+ typedef std::reverse_iterator<iterator> reverse_iterator;
- void set(const void* data, size_type len) {
- ptr_ = reinterpret_cast<const value_type*>(data);
- length_ = len;
- }
+ public:
+ // We provide non-explicit singleton constructors so users can pass
+ // in a "const char16*" or a "string16" wherever a "StringPiece16" is
+ // expected.
+ StringPiece16() : ptr_(NULL), length_(0) { }
+ StringPiece16(const char16* str)
+ : ptr_(str),
+ length_((str == NULL) ? 0 : string16::traits_type::length(str)) { }
+ StringPiece16(const string16& str)
+ : ptr_(str.data()), length_(str.size()) { }
+ StringPiece16(const char16* offset, size_type len)
+ : ptr_(offset), length_(len) { }
- void CopyToString(std::string* target) const;
- void AppendToString(std::string* target) const;
+ // data() may return a pointer to a buffer with embedded NULs, and the
+ // returned buffer may or may not be null terminated. Therefore it is
+ // typically a mistake to pass data() to a routine that expects a NUL
+ // terminated string.
+ const char16* data() const { return ptr_; }
+ size_type size() const { return length_; }
+ size_type length() const { return length_; }
+ bool empty() const { return length_ == 0; }
- // Does "this" start with "x"
- bool starts_with(const BasicStringPiece& x) const {
- return ((length_ >= x.length_) &&
- (wordmemcmp(ptr_, x.ptr_, x.length_) == 0));
+ void clear() {
+ ptr_ = NULL;
+ 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));
+ void set(const char16* data, size_type len) {
+ ptr_ = data;
+ length_ = len;
+ }
+ void set(const char16* str) {
+ ptr_ = str;
+ length_ = str ? string16::traits_type::length(str) : 0;
}
- size_type copy(char* buf, size_type n, size_type pos = 0) const;
+ char16 operator[](size_type i) const { return ptr_[i]; }
- size_type find(const BasicStringPiece& s, size_type pos = 0) const;
- size_type find(char c, size_type pos = 0) const;
- size_type rfind(const BasicStringPiece& s, size_type pos = npos) const;
- size_type rfind(char c, size_type pos = npos) const;
+ string16 as_string16() const {
+ // StringPiece claims that this is bad when data() is NULL, but unittesting
+ // seems to say otherwise.
+ return string16(data(), size());
+ }
- size_type find_first_of(const BasicStringPiece& s, size_type pos = 0) const;
- size_type find_first_of(char c, size_type pos = 0) const {
- return find(c, pos);
+ iterator begin() const { return ptr_; }
+ iterator end() const { return ptr_ + length_; }
+ const_reverse_iterator rbegin() const {
+ return const_reverse_iterator(ptr_ + length_);
}
- size_type find_first_not_of(const BasicStringPiece& s,
- size_type pos = 0) const;
- size_type find_first_not_of(char c, size_type pos = 0) const;
- size_type find_last_of(const BasicStringPiece& s, size_type pos = npos) const;
- size_type find_last_of(char c, size_type pos = npos) const {
- return rfind(c, pos);
+ const_reverse_iterator rend() const {
+ return const_reverse_iterator(ptr_);
}
- size_type find_last_not_of(const BasicStringPiece& s,
- size_type pos = npos) const;
- size_type find_last_not_of(char c, size_type pos = npos) const;
- BasicStringPiece substr(size_type pos, size_type n = npos) const;
-};
+ size_type max_size() const { return length_; }
+ size_type capacity() const { return length_; }
-// MSVC doesn't like complex extern templates and DLLs.
-#if !defined(COMPILER_MSVC)
-extern template class BasicStringPiece<std::string>;
-extern template class BASE_EXPORT BasicStringPiece<string16>;
-#endif
+ static int wordmemcmp(const char16* p, const char16* p2, size_type N) {
+ return string16::traits_type::compare(p, p2, N);
+ }
-typedef BasicStringPiece<std::string> StringPiece;
-typedef BasicStringPiece<string16> StringPiece16;
+ private:
+ const char16* ptr_;
+ size_type length_;
+};
BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y);
diff --git a/base/string_piece_unittest.cc b/base/string_piece_unittest.cc
index 1362d4e..694c43b 100644
--- a/base/string_piece_unittest.cc
+++ b/base/string_piece_unittest.cc
@@ -11,52 +11,14 @@
namespace base {
-template <typename T>
-class CommonStringPieceTest : public ::testing::Test {
- public:
- static const T as_string(const char* input) {
- return T(input);
- }
- static const T& as_string(const T& input) {
- return input;
- }
-};
-
-template <>
-class CommonStringPieceTest<string16> : public ::testing::Test {
- public:
- static const string16 as_string(const char* input) {
- return ASCIIToUTF16(input);
- }
- static const string16 as_string(const std::string& input) {
- return ASCIIToUTF16(input);
- }
-};
-
-typedef ::testing::Types<std::string, string16> SupportedStringTypes;
-
-TYPED_TEST_CASE(CommonStringPieceTest, SupportedStringTypes);
-
-TYPED_TEST(CommonStringPieceTest, CheckComparisonOperators) {
-#define CMP_Y(op, x, y) \
- { \
- TypeParam lhs(TestFixture::as_string(x)); \
- TypeParam rhs(TestFixture::as_string(y)); \
- ASSERT_TRUE( (BasicStringPiece<TypeParam>((lhs.c_str())) op \
- BasicStringPiece<TypeParam>((rhs.c_str())))); \
- ASSERT_TRUE( (BasicStringPiece<TypeParam>((lhs.c_str())).compare( \
- BasicStringPiece<TypeParam>((rhs.c_str()))) op 0)); \
- }
+TEST(StringPieceTest, CheckComparisonOperators) {
+#define CMP_Y(op, x, y) \
+ ASSERT_TRUE( (StringPiece((x)) op StringPiece((y)))); \
+ ASSERT_TRUE( (StringPiece((x)).compare(StringPiece((y))) op 0))
-#define CMP_N(op, x, y) \
- { \
- TypeParam lhs(TestFixture::as_string(x)); \
- TypeParam rhs(TestFixture::as_string(y)); \
- ASSERT_FALSE( (BasicStringPiece<TypeParam>((lhs.c_str())) op \
- BasicStringPiece<TypeParam>((rhs.c_str())))); \
- ASSERT_FALSE( (BasicStringPiece<TypeParam>((lhs.c_str())).compare( \
- BasicStringPiece<TypeParam>((rhs.c_str()))) op 0)); \
- }
+#define CMP_N(op, x, y) \
+ ASSERT_FALSE(StringPiece((x)) op StringPiece((y))); \
+ ASSERT_FALSE(StringPiece((x)).compare(StringPiece((y))) op 0)
CMP_Y(==, "", "");
CMP_Y(==, "a", "a");
@@ -132,40 +94,34 @@ TYPED_TEST(CommonStringPieceTest, CheckComparisonOperators) {
#undef CMP_N
}
-TYPED_TEST(CommonStringPieceTest, CheckSTL) {
- TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
- TypeParam abc(TestFixture::as_string("abc"));
- TypeParam xyz(TestFixture::as_string("xyz"));
- TypeParam foobar(TestFixture::as_string("foobar"));
-
- BasicStringPiece<TypeParam> a(alphabet);
- BasicStringPiece<TypeParam> b(abc);
- BasicStringPiece<TypeParam> c(xyz);
- BasicStringPiece<TypeParam> d(foobar);
- BasicStringPiece<TypeParam> e;
- TypeParam temp(TestFixture::as_string("123"));
- temp += static_cast<typename TypeParam::value_type>(0);
- temp += TestFixture::as_string("456");
- BasicStringPiece<TypeParam> f(temp);
-
- ASSERT_EQ(a[6], static_cast<typename TypeParam::value_type>('g'));
- ASSERT_EQ(b[0], static_cast<typename TypeParam::value_type>('a'));
- ASSERT_EQ(c[2], static_cast<typename TypeParam::value_type>('z'));
- ASSERT_EQ(f[3], static_cast<typename TypeParam::value_type>('\0'));
- ASSERT_EQ(f[5], static_cast<typename TypeParam::value_type>('5'));
-
- ASSERT_EQ(*d.data(), static_cast<typename TypeParam::value_type>('f'));
- ASSERT_EQ(d.data()[5], static_cast<typename TypeParam::value_type>('r'));
+TEST(StringPieceTest, CheckSTL) {
+ StringPiece a("abcdefghijklmnopqrstuvwxyz");
+ StringPiece b("abc");
+ StringPiece c("xyz");
+ StringPiece d("foobar");
+ StringPiece e;
+ std::string temp("123");
+ temp += '\0';
+ temp += "456";
+ StringPiece f(temp);
+
+ ASSERT_EQ(a[6], 'g');
+ ASSERT_EQ(b[0], 'a');
+ ASSERT_EQ(c[2], 'z');
+ ASSERT_EQ(f[3], '\0');
+ ASSERT_EQ(f[5], '5');
+
+ ASSERT_EQ(*d.data(), 'f');
+ ASSERT_EQ(d.data()[5], 'r');
ASSERT_TRUE(e.data() == NULL);
- ASSERT_EQ(*a.begin(), static_cast<typename TypeParam::value_type>('a'));
- ASSERT_EQ(*(b.begin() + 2), static_cast<typename TypeParam::value_type>('c'));
- ASSERT_EQ(*(c.end() - 1), static_cast<typename TypeParam::value_type>('z'));
+ ASSERT_EQ(*a.begin(), 'a');
+ ASSERT_EQ(*(b.begin() + 2), 'c');
+ ASSERT_EQ(*(c.end() - 1), 'z');
- ASSERT_EQ(*a.rbegin(), static_cast<typename TypeParam::value_type>('z'));
- ASSERT_EQ(*(b.rbegin() + 2),
- static_cast<typename TypeParam::value_type>('a'));
- ASSERT_EQ(*(c.rend() - 1), static_cast<typename TypeParam::value_type>('x'));
+ ASSERT_EQ(*a.rbegin(), 'z');
+ ASSERT_EQ(*(b.rbegin() + 2), 'a');
+ ASSERT_EQ(*(c.rend() - 1), 'x');
ASSERT_TRUE(a.rbegin() + 26 == a.rend());
ASSERT_EQ(a.size(), 26U);
@@ -190,20 +146,6 @@ TYPED_TEST(CommonStringPieceTest, CheckSTL) {
ASSERT_GE(a.max_size(), a.capacity());
ASSERT_GE(a.capacity(), a.size());
-}
-
-// STL stuff only supported by the std::string version
-TEST(StringPieceTest, CheckSTL) {
- StringPiece a("abcdefghijklmnopqrstuvwxyz");
- StringPiece b("abc");
- StringPiece c("xyz");
- StringPiece d("foobar");
- d.clear();
- StringPiece e;
- std::string temp("123");
- temp += '\0';
- temp += "456";
- StringPiece f(temp);
char buf[4] = { '%', '%', '%', '%' };
ASSERT_EQ(a.copy(buf, 4), 4U);
@@ -458,53 +400,6 @@ TEST(StringPieceTest, CheckSTL) {
ASSERT_EQ(d.substr(99, 99), e);
}
-TYPED_TEST(CommonStringPieceTest, CheckCustom) {
- TypeParam foobar(TestFixture::as_string("foobar"));
- BasicStringPiece<TypeParam> a(foobar);
- TypeParam s1(TestFixture::as_string("123"));
- s1 += static_cast<typename TypeParam::value_type>('\0');
- s1 += TestFixture::as_string("456");
- BasicStringPiece<TypeParam> b(s1);
- BasicStringPiece<TypeParam> e;
- TypeParam s2;
-
- // remove_prefix
- BasicStringPiece<TypeParam> c(a);
- c.remove_prefix(3);
- ASSERT_EQ(c, TestFixture::as_string("bar"));
- c = a;
- c.remove_prefix(0);
- ASSERT_EQ(c, a);
- c.remove_prefix(c.size());
- ASSERT_EQ(c, e);
-
- // remove_suffix
- c = a;
- c.remove_suffix(3);
- ASSERT_EQ(c, TestFixture::as_string("foo"));
- c = a;
- c.remove_suffix(0);
- ASSERT_EQ(c, a);
- c.remove_suffix(c.size());
- ASSERT_EQ(c, e);
-
- // set
- c.set(foobar.c_str());
- ASSERT_EQ(c, a);
- c.set(foobar.c_str(), 6);
- ASSERT_EQ(c, a);
- c.set(foobar.c_str(), 0);
- ASSERT_EQ(c, e);
- c.set(foobar.c_str(), 7); // Note, has an embedded NULL
- ASSERT_NE(c, a);
-
- // as_string
- TypeParam s3(a.as_string().c_str(), 7); // Note, has an embedded NULL
- ASSERT_TRUE(c == s3);
- TypeParam s4(e.as_string());
- ASSERT_TRUE(s4.empty());
-}
-
TEST(StringPieceTest, CheckCustom) {
StringPiece a("foobar");
std::string s1("123");
@@ -557,50 +452,74 @@ TEST(StringPieceTest, CheckCustom) {
ASSERT_TRUE(!b.ends_with(a));
ASSERT_TRUE(!e.ends_with(a));
- StringPiece c;
+ // remove_prefix
+ StringPiece c(a);
+ c.remove_prefix(3);
+ ASSERT_EQ(c, "bar");
+ c = a;
+ c.remove_prefix(0);
+ ASSERT_EQ(c, a);
+ c.remove_prefix(c.size());
+ ASSERT_EQ(c, e);
+
+ // remove_suffix
+ c = a;
+ c.remove_suffix(3);
+ ASSERT_EQ(c, "foo");
+ c = a;
+ c.remove_suffix(0);
+ ASSERT_EQ(c, a);
+ c.remove_suffix(c.size());
+ ASSERT_EQ(c, e);
+
+ // set
+ c.set("foobar", 6);
+ ASSERT_EQ(c, a);
+ c.set("foobar", 0);
+ ASSERT_EQ(c, e);
+ c.set("foobar", 7);
+ ASSERT_NE(c, a);
+
+ c.set("foobar");
+ ASSERT_EQ(c, a);
+
c.set(static_cast<const void*>("foobar"), 6);
ASSERT_EQ(c, a);
c.set(static_cast<const void*>("foobar"), 0);
ASSERT_EQ(c, e);
c.set(static_cast<const void*>("foobar"), 7);
ASSERT_NE(c, a);
+
+ // as_string
+ std::string s3(a.as_string().c_str(), 7);
+ ASSERT_EQ(c, s3);
+ std::string s4(e.as_string());
+ ASSERT_TRUE(s4.empty());
}
-TYPED_TEST(CommonStringPieceTest, CheckNULL) {
+TEST(StringPieceTest, CheckNULL) {
// we used to crash here, but now we don't.
- BasicStringPiece<TypeParam> s(NULL);
- ASSERT_EQ(s.data(), (const typename TypeParam::value_type*)NULL);
+ StringPiece s(NULL);
+ ASSERT_EQ(s.data(), (const char*)NULL);
ASSERT_EQ(s.size(), 0U);
s.set(NULL);
- ASSERT_EQ(s.data(), (const typename TypeParam::value_type*)NULL);
+ ASSERT_EQ(s.data(), (const char*)NULL);
ASSERT_EQ(s.size(), 0U);
-
- TypeParam str = s.as_string();
- ASSERT_EQ(str.length(), 0U);
- ASSERT_EQ(str, TypeParam());
}
-TYPED_TEST(CommonStringPieceTest, CheckComparisons2) {
- TypeParam alphabet(TestFixture::as_string("abcdefghijklmnopqrstuvwxyz"));
- TypeParam alphabet_z(TestFixture::as_string("abcdefghijklmnopqrstuvwxyzz"));
- TypeParam alphabet_y(TestFixture::as_string("abcdefghijklmnopqrstuvwxyy"));
- BasicStringPiece<TypeParam> abc(alphabet);
+TEST(StringPieceTest, CheckComparisons2) {
+ StringPiece abc("abcdefghijklmnopqrstuvwxyz");
// check comparison operations on strings longer than 4 bytes.
- ASSERT_TRUE(abc == BasicStringPiece<TypeParam>(alphabet));
- ASSERT_TRUE(abc.compare(BasicStringPiece<TypeParam>(alphabet)) == 0);
+ ASSERT_TRUE(abc == StringPiece("abcdefghijklmnopqrstuvwxyz"));
+ ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyz")) == 0);
- ASSERT_TRUE(abc < BasicStringPiece<TypeParam>(alphabet_z));
- ASSERT_TRUE(abc.compare(BasicStringPiece<TypeParam>(alphabet_z)) < 0);
+ ASSERT_TRUE(abc < StringPiece("abcdefghijklmnopqrstuvwxzz"));
+ ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxzz")) < 0);
- ASSERT_TRUE(abc > BasicStringPiece<TypeParam>(alphabet_y));
- ASSERT_TRUE(abc.compare(BasicStringPiece<TypeParam>(alphabet_y)) > 0);
-}
-
-// Test operations only supported by std::string version.
-TEST(StringPieceTest, CheckComparisons2) {
- StringPiece abc("abcdefghijklmnopqrstuvwxyz");
+ ASSERT_TRUE(abc > StringPiece("abcdefghijklmnopqrstuvwxyy"));
+ ASSERT_TRUE(abc.compare(StringPiece("abcdefghijklmnopqrstuvwxyy")) > 0);
// starts_with
ASSERT_TRUE(abc.starts_with(abc));
@@ -613,22 +532,169 @@ TEST(StringPieceTest, CheckComparisons2) {
ASSERT_TRUE(abc.ends_with("nopqrstuvwxyz"));
}
-TYPED_TEST(CommonStringPieceTest, StringCompareNotAmbiguous) {
- ASSERT_TRUE(TestFixture::as_string("hello").c_str() ==
- TestFixture::as_string("hello"));
- ASSERT_TRUE(TestFixture::as_string("hello").c_str() <
- TestFixture::as_string("world"));
+TEST(StringPieceTest, StringCompareNotAmbiguous) {
+ ASSERT_TRUE("hello" == std::string("hello"));
+ ASSERT_TRUE("hello" < std::string("world"));
}
-TYPED_TEST(CommonStringPieceTest, HeterogenousStringPieceEquals) {
- TypeParam hello(TestFixture::as_string("hello"));
+TEST(StringPieceTest, HeterogenousStringPieceEquals) {
+ ASSERT_TRUE(StringPiece("hello") == std::string("hello"));
+ ASSERT_TRUE("hello" == StringPiece("hello"));
+}
- ASSERT_TRUE(BasicStringPiece<TypeParam>(hello) == hello);
- ASSERT_TRUE(hello.c_str() == BasicStringPiece<TypeParam>(hello));
+TEST(StringPiece16Test, CheckComparisonOperators) {
+ ASSERT_TRUE(StringPiece16(string16()) ==
+ StringPiece16(string16()));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) ==
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) ==
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) ==
+ StringPiece16(string16()));
+ ASSERT_FALSE(StringPiece16(string16()) ==
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) ==
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) ==
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) ==
+ StringPiece16(ASCIIToUTF16("a")));
+
+ ASSERT_FALSE(StringPiece16(string16()) !=
+ StringPiece16(string16()));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) !=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) !=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) !=
+ StringPiece16(string16()));
+ ASSERT_TRUE(StringPiece16(string16()) !=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) !=
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) !=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) !=
+ StringPiece16(ASCIIToUTF16("a")));
+
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <
+ StringPiece16(ASCIIToUTF16("bb")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) <
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <
+ StringPiece16(ASCIIToUTF16("aa")));
+
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
+ StringPiece16(ASCIIToUTF16("bb")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <=
+ StringPiece16(ASCIIToUTF16("aa")));
+
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) <=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) <=
+ StringPiece16(ASCIIToUTF16("bb")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) <=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("b")) <=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("bb")) <=
+ StringPiece16(ASCIIToUTF16("aa")));
+
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >=
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >=
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >=
+ StringPiece16(ASCIIToUTF16("bb")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("a")) >=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) >=
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >=
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("bb")) >=
+ StringPiece16(ASCIIToUTF16("aa")));
+
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("a")) >
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >
+ StringPiece16(ASCIIToUTF16("b")));
+ ASSERT_FALSE(StringPiece16(ASCIIToUTF16("aa")) >
+ StringPiece16(ASCIIToUTF16("bb")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("aa")) >
+ StringPiece16(ASCIIToUTF16("a")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("b")) >
+ StringPiece16(ASCIIToUTF16("aa")));
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("bb")) >
+ StringPiece16(ASCIIToUTF16("aa")));
+
+ string16 x;
+ for (int i = 0; i < 256; i++) {
+ x += 'a';
+ string16 y = x;
+ ASSERT_EQ(StringPiece16(x), StringPiece16(y));
+ for (int j = 0; j < i; j++) {
+ string16 z = x;
+ z[j] = 'b'; // Differs in position 'j'
+ ASSERT_NE(StringPiece16(x), StringPiece16(z));
+ }
+ }
}
-// string16-specific stuff
TEST(StringPiece16Test, CheckSTL) {
+ string16 first = ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz");
+ StringPiece16 a(first);
+ string16 second = ASCIIToUTF16("abc");
+ StringPiece16 b(second.c_str());
+ string16 third = ASCIIToUTF16("xyz");
+ StringPiece16 c(third.c_str(), third.size());
+ string16 fourth = ASCIIToUTF16("foobarrandomstuff");
+ StringPiece16 d(fourth.c_str(), 6);
+ StringPiece16 e;
// Check some non-ascii characters.
string16 fifth(ASCIIToUTF16("123"));
fifth.push_back(0x0000);
@@ -636,20 +702,73 @@ TEST(StringPiece16Test, CheckSTL) {
fifth.push_back(0xdffe);
StringPiece16 f(fifth);
+ ASSERT_EQ(a[6], 'g');
+ ASSERT_EQ(b[0], 'a');
+ ASSERT_EQ(c[2], 'z');
ASSERT_EQ(f[3], '\0');
ASSERT_EQ(f[5], static_cast<char16>(0xdffe));
+ ASSERT_EQ(*d.data(), 'f');
+ ASSERT_EQ(d.data()[5], 'r');
+ ASSERT_TRUE(e.data() == NULL);
+
+ ASSERT_EQ(*a.begin(), 'a');
+ ASSERT_EQ(*(b.begin() + 2), 'c');
+ ASSERT_EQ(*(c.end() - 1), 'z');
+
+ ASSERT_EQ(*a.rbegin(), 'z');
+ ASSERT_EQ(*(b.rbegin() + 2), 'a');
+ ASSERT_EQ(*(c.rend() - 1), 'x');
+ ASSERT_TRUE(a.rbegin() + 26 == a.rend());
+
+ ASSERT_EQ(a.size(), 26U);
+ ASSERT_EQ(b.size(), 3U);
+ ASSERT_EQ(c.size(), 3U);
+ ASSERT_EQ(d.size(), 6U);
+ ASSERT_EQ(e.size(), 0U);
ASSERT_EQ(f.size(), 6U);
+
+ ASSERT_TRUE(!d.empty());
+ ASSERT_TRUE(d.begin() != d.end());
+ ASSERT_TRUE(d.begin() + 6 == d.end());
+
+ ASSERT_TRUE(e.empty());
+ ASSERT_TRUE(e.begin() == e.end());
+
+ d.clear();
+ ASSERT_EQ(d.size(), 0U);
+ ASSERT_TRUE(d.empty());
+ ASSERT_TRUE(d.data() == NULL);
+ ASSERT_TRUE(d.begin() == d.end());
+
+ ASSERT_GE(a.max_size(), a.capacity());
+ ASSERT_GE(a.capacity(), a.size());
}
+TEST(StringPiece16Test, CheckNULL) {
+ StringPiece16 s(NULL);
+ ASSERT_EQ(s.data(), (const char16*)NULL);
+ ASSERT_EQ(s.size(), 0U);
+
+ s.set(NULL);
+ ASSERT_EQ(s.data(), (const char16*)NULL);
+ ASSERT_EQ(s.size(), 0U);
+ string16 str = s.as_string16();
+ ASSERT_EQ(s.data(), (const char16*)NULL);
+ ASSERT_EQ(s.size(), 0U);
+}
+
+TEST(StringPiece16Test, HeterogenousStringPieceEquals) {
+ ASSERT_TRUE(StringPiece16(ASCIIToUTF16("hello")) == ASCIIToUTF16("hello"));
+}
TEST(StringPiece16Test, CheckConversion) {
// Make sure that we can convert from UTF8 to UTF16 and back. We use a two
// byte character (G clef) to test this.
ASSERT_EQ(
UTF16ToUTF8(
- StringPiece16(UTF8ToUTF16("\xf0\x9d\x84\x9e")).as_string()),
+ StringPiece16(UTF8ToUTF16("\xf0\x9d\x84\x9e")).as_string16()),
"\xf0\x9d\x84\x9e");
}
diff --git a/base/utf_offset_string_conversions.h b/base/utf_offset_string_conversions.h
index 97b641e..c2faf3a 100644
--- a/base/utf_offset_string_conversions.h
+++ b/base/utf_offset_string_conversions.h
@@ -11,7 +11,11 @@
#include "base/base_export.h"
#include "base/string16.h"
-#include "base/string_piece.h"
+
+namespace base {
+class StringPiece;
+class StringPiece16;
+}
// Like the conversions in utf_string_conversions.h, but also takes one or more
// offsets (|offset[s]_for_adjustment|) into the source strings, each offset