summaryrefslogtreecommitdiffstats
path: root/base/strings
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-28 20:07:40 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-28 20:07:40 +0000
commit7d7b0ac60fdf827321f3df1a97175daa79853389 (patch)
treeb13cfebc79fa38b16f24912a5191530cf2652370 /base/strings
parent001fc2c4c1caad53c29bfb0c079c4ab3a55f150a (diff)
downloadchromium_src-7d7b0ac60fdf827321f3df1a97175daa79853389.zip
chromium_src-7d7b0ac60fdf827321f3df1a97175daa79853389.tar.gz
chromium_src-7d7b0ac60fdf827321f3df1a97175daa79853389.tar.bz2
Move string_piece.h to base/strings.
This keeps a forwarding header and does not update any code to use the new location. This will be updated in a later pass. BUG= Review URL: https://codereview.chromium.org/12982018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191206 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/strings')
-rw-r--r--base/strings/string_piece.cc255
-rw-r--r--base/strings/string_piece.h451
-rw-r--r--base/strings/string_piece_unittest.cc677
3 files changed, 1383 insertions, 0 deletions
diff --git a/base/strings/string_piece.cc b/base/strings/string_piece.cc
new file mode 100644
index 0000000..79a42d7
--- /dev/null
+++ b/base/strings/string_piece.cc
@@ -0,0 +1,255 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+// Copied from strings/stringpiece.cc with modifications
+
+#include "base/strings/string_piece.h"
+
+#include <algorithm>
+#include <ostream>
+
+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<string16>;
+#endif
+
+bool operator==(const StringPiece& x, const StringPiece& y) {
+ if (x.size() != y.size())
+ return false;
+
+ return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
+}
+
+std::ostream& operator<<(std::ostream& o, const StringPiece& piece) {
+ o.write(piece.data(), static_cast<std::streamsize>(piece.size()));
+ return o;
+}
+
+namespace internal {
+void CopyToString(const StringPiece& self, std::string* target) {
+ target->assign(!self.empty() ? self.data() : "", self.size());
+}
+
+void AppendToString(const StringPiece& self, std::string* target) {
+ if (!self.empty())
+ target->append(self.data(), self.size());
+}
+
+StringPiece::size_type copy(const StringPiece& self,
+ char* buf,
+ StringPiece::size_type n,
+ StringPiece::size_type pos) {
+ StringPiece::size_type ret = std::min(self.size() - pos, n);
+ memcpy(buf, self.data() + pos, ret);
+ return ret;
+}
+
+StringPiece::size_type find(const StringPiece& self,
+ const StringPiece& s,
+ StringPiece::size_type pos) {
+ if (pos > self.size())
+ return StringPiece::npos;
+
+ StringPiece::const_iterator result =
+ std::search(self.begin() + pos, self.end(), s.begin(), s.end());
+ const StringPiece::size_type xpos =
+ static_cast<size_t>(result - self.begin());
+ return xpos + s.size() <= self.size() ? xpos : StringPiece::npos;
+}
+
+StringPiece::size_type find(const StringPiece& self,
+ char c,
+ StringPiece::size_type pos) {
+ if (pos >= self.size())
+ return StringPiece::npos;
+
+ StringPiece::const_iterator result =
+ std::find(self.begin() + pos, self.end(), c);
+ return result != self.end() ?
+ static_cast<size_t>(result - self.begin()) : StringPiece::npos;
+}
+
+StringPiece::size_type rfind(const StringPiece& self,
+ const StringPiece& s,
+ StringPiece::size_type pos) {
+ if (self.size() < s.size())
+ return StringPiece::npos;
+
+ if (s.empty())
+ return std::min(self.size(), pos);
+
+ StringPiece::const_iterator last =
+ self.begin() + std::min(self.size() - s.size(), pos) + s.size();
+ StringPiece::const_iterator result =
+ std::find_end(self.begin(), last, s.begin(), s.end());
+ return result != last ?
+ static_cast<size_t>(result - self.begin()) : StringPiece::npos;
+}
+
+StringPiece::size_type rfind(const StringPiece& self,
+ char c,
+ StringPiece::size_type pos) {
+ if (self.size() == 0)
+ return StringPiece::npos;
+
+ for (StringPiece::size_type i = std::min(pos, self.size() - 1); ; --i) {
+ if (self.data()[i] == c)
+ return i;
+ if (i == 0)
+ break;
+ }
+ return StringPiece::npos;
+}
+
+// For each character in characters_wanted, sets the index corresponding
+// to the ASCII code of that character to 1 in table. This is used by
+// the find_.*_of methods below to tell whether or not a character is in
+// the lookup table in constant time.
+// The argument `table' must be an array that is large enough to hold all
+// the possible values of an unsigned char. Thus it should be be declared
+// as follows:
+// bool table[UCHAR_MAX + 1]
+static inline void BuildLookupTable(const StringPiece& characters_wanted,
+ bool* table) {
+ const StringPiece::size_type length = characters_wanted.length();
+ const char* const data = characters_wanted.data();
+ for (StringPiece::size_type i = 0; i < length; ++i) {
+ table[static_cast<unsigned char>(data[i])] = true;
+ }
+}
+
+StringPiece::size_type find_first_of(const StringPiece& self,
+ const StringPiece& s,
+ StringPiece::size_type pos) {
+ if (self.size() == 0 || s.size() == 0)
+ return StringPiece::npos;
+
+ // Avoid the cost of BuildLookupTable() for a single-character search.
+ if (s.size() == 1)
+ return find(self, s.data()[0], pos);
+
+ bool lookup[UCHAR_MAX + 1] = { false };
+ BuildLookupTable(s, lookup);
+ for (StringPiece::size_type i = pos; i < self.size(); ++i) {
+ if (lookup[static_cast<unsigned char>(self.data()[i])]) {
+ return i;
+ }
+ }
+ return StringPiece::npos;
+}
+
+StringPiece::size_type find_first_not_of(const StringPiece& self,
+ const StringPiece& s,
+ StringPiece::size_type pos) {
+ if (self.size() == 0)
+ return StringPiece::npos;
+
+ if (s.size() == 0)
+ return 0;
+
+ // Avoid the cost of BuildLookupTable() for a single-character search.
+ if (s.size() == 1)
+ return find_first_not_of(self, s.data()[0], pos);
+
+ bool lookup[UCHAR_MAX + 1] = { false };
+ BuildLookupTable(s, lookup);
+ for (StringPiece::size_type i = pos; i < self.size(); ++i) {
+ if (!lookup[static_cast<unsigned char>(self.data()[i])]) {
+ return i;
+ }
+ }
+ return StringPiece::npos;
+}
+
+StringPiece::size_type find_first_not_of(const StringPiece& self,
+ char c,
+ StringPiece::size_type pos) {
+ if (self.size() == 0)
+ return StringPiece::npos;
+
+ for (; pos < self.size(); ++pos) {
+ if (self.data()[pos] != c) {
+ return pos;
+ }
+ }
+ return StringPiece::npos;
+}
+
+StringPiece::size_type find_last_of(const StringPiece& self,
+ const StringPiece& s,
+ StringPiece::size_type pos) {
+ if (self.size() == 0 || s.size() == 0)
+ return StringPiece::npos;
+
+ // Avoid the cost of BuildLookupTable() for a single-character search.
+ if (s.size() == 1)
+ return rfind(self, s.data()[0], pos);
+
+ bool lookup[UCHAR_MAX + 1] = { false };
+ BuildLookupTable(s, lookup);
+ for (StringPiece::size_type i = std::min(pos, self.size() - 1); ; --i) {
+ if (lookup[static_cast<unsigned char>(self.data()[i])])
+ return i;
+ if (i == 0)
+ break;
+ }
+ return StringPiece::npos;
+}
+
+StringPiece::size_type find_last_not_of(const StringPiece& self,
+ const StringPiece& s,
+ StringPiece::size_type pos) {
+ if (self.size() == 0)
+ return StringPiece::npos;
+
+ StringPiece::size_type i = std::min(pos, self.size() - 1);
+ if (s.size() == 0)
+ return i;
+
+ // Avoid the cost of BuildLookupTable() for a single-character search.
+ if (s.size() == 1)
+ return find_last_not_of(self, s.data()[0], pos);
+
+ bool lookup[UCHAR_MAX + 1] = { false };
+ BuildLookupTable(s, lookup);
+ for (; ; --i) {
+ if (!lookup[static_cast<unsigned char>(self.data()[i])])
+ return i;
+ if (i == 0)
+ break;
+ }
+ return StringPiece::npos;
+}
+
+StringPiece::size_type find_last_not_of(const StringPiece& self,
+ char c,
+ StringPiece::size_type pos) {
+ if (self.size() == 0)
+ return StringPiece::npos;
+
+ for (StringPiece::size_type i = std::min(pos, self.size() - 1); ; --i) {
+ if (self.data()[i] != c)
+ return i;
+ if (i == 0)
+ break;
+ }
+ return StringPiece::npos;
+}
+
+StringPiece substr(const StringPiece& self,
+ StringPiece::size_type pos,
+ StringPiece::size_type n) {
+ if (pos > self.size()) pos = self.size();
+ if (n > self.size() - pos) n = self.size() - pos;
+ return StringPiece(self.data() + pos, n);
+}
+
+} // namespace internal
+} // namespace base
diff --git a/base/strings/string_piece.h b/base/strings/string_piece.h
new file mode 100644
index 0000000..13ca009
--- /dev/null
+++ b/base/strings/string_piece.h
@@ -0,0 +1,451 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+// Copied from strings/stringpiece.h with modifications
+//
+// A string-like object that points to a sized piece of memory.
+//
+// Functions or methods may use const StringPiece& parameters to accept either
+// a "const char*" or a "string" value that will be implicitly converted to
+// a StringPiece. The implicit conversion means that it is often appropriate
+// to include this .h file in other files rather than forward-declaring
+// StringPiece as would be appropriate for most other Google classes.
+//
+// Systematic usage of StringPiece is encouraged as it will reduce unnecessary
+// conversions from "const char*" to "string" and back again.
+//
+// StringPiece16 is similar to StringPiece but for base::string16 instead of
+// std::string. We do not define as large of a subset of the STL functions
+// from basic_string as in StringPiece, but this can be changed if these
+// functions (find, find_first_of, etc.) are found to be useful in this context.
+//
+
+#ifndef BASE_STRINGS_STRING_PIECE_H_
+#define BASE_STRINGS_STRING_PIECE_H_
+
+#include <stddef.h>
+
+#include <iosfwd>
+#include <string>
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+#include "base/hash_tables.h"
+#include "base/string16.h"
+
+namespace base {
+
+template <typename STRING_TYPE> class BasicStringPiece;
+typedef BasicStringPiece<std::string> StringPiece;
+typedef BasicStringPiece<string16> StringPiece16;
+
+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 {
+ 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 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:
+ // 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) {}
+ 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) {}
+
+ // 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_; }
+ size_type size() const { return length_; }
+ size_type length() const { return length_; }
+ bool empty() const { return length_ == 0; }
+
+ void clear() {
+ ptr_ = NULL;
+ length_ = 0;
+ }
+ void set(const value_type* data, size_type len) {
+ ptr_ = data;
+ length_ = len;
+ }
+ void set(const value_type* str) {
+ ptr_ = str;
+ length_ = str ? STRING_TYPE::traits_type::length(str) : 0;
+ }
+
+ value_type operator[](size_type i) const { return ptr_[i]; }
+
+ void remove_prefix(size_type n) {
+ ptr_ += n;
+ length_ -= n;
+ }
+
+ void remove_suffix(size_type n) {
+ length_ -= n;
+ }
+
+ int compare(const BasicStringPiece<STRING_TYPE>& x) const {
+ int r = wordmemcmp(
+ 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;
+ }
+
+ STRING_TYPE 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());
+ }
+
+ const_iterator begin() const { return ptr_; }
+ const_iterator end() const { return ptr_ + length_; }
+ const_reverse_iterator rbegin() const {
+ return const_reverse_iterator(ptr_ + length_);
+ }
+ const_reverse_iterator rend() const {
+ return const_reverse_iterator(ptr_);
+ }
+
+ 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);
+ }
+
+ 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(std::string* target) const {
+ internal::AppendToString(*this, target);
+ }
+
+ // Does "this" start with "x"
+ bool starts_with(const BasicStringPiece& x) const {
+ return ((length_ >= x.length_) &&
+ (wordmemcmp(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);
+ }
+
+ size_type find(const BasicStringPiece& s, size_type pos = 0) const {
+ return internal::find(*this, s, pos);
+ }
+
+ size_type find(char c, size_type pos = 0) const {
+ return internal::find(*this, c, pos);
+ }
+
+ size_type rfind(const BasicStringPiece& s, size_type pos = npos) const {
+ return internal::rfind(*this, s, pos);
+ }
+
+ size_type rfind(char c, size_type pos = 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(char 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(char 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 = npos) const {
+ return internal::find_last_of(*this, s, pos);
+ }
+
+ size_type find_last_of(char c, size_type pos = npos) const {
+ return rfind(c, pos);
+ }
+
+ size_type find_last_not_of(const BasicStringPiece& s,
+ size_type pos = npos) const {
+ return internal::find_last_not_of(*this, s, pos);
+ }
+
+ size_type find_last_not_of(char c, size_type pos = npos) const {
+ return internal::find_last_not_of(*this, c, pos);
+ }
+
+ BasicStringPiece substr(size_type pos, size_type n = npos) const {
+ return internal::substr(*this, pos, n);
+ }
+};
+
+// 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<string16>;
+#endif
+
+BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y);
+
+inline bool operator!=(const StringPiece& x, const StringPiece& y) {
+ return !(x == y);
+}
+
+inline bool operator<(const StringPiece& x, const StringPiece& y) {
+ const int r = StringPiece::wordmemcmp(
+ x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
+ return ((r < 0) || ((r == 0) && (x.size() < y.size())));
+}
+
+inline bool operator>(const StringPiece& x, const StringPiece& y) {
+ return y < x;
+}
+
+inline bool operator<=(const StringPiece& x, const StringPiece& y) {
+ return !(x > y);
+}
+
+inline bool operator>=(const StringPiece& x, const StringPiece& y) {
+ return !(x < y);
+}
+
+inline bool operator==(const StringPiece16& x, const StringPiece16& y) {
+ if (x.size() != y.size())
+ return false;
+
+ return StringPiece16::wordmemcmp(x.data(), y.data(), x.size()) == 0;
+}
+
+inline bool operator!=(const StringPiece16& x, const StringPiece16& y) {
+ return !(x == y);
+}
+
+inline bool operator<(const StringPiece16& x, const StringPiece16& y) {
+ const int r = StringPiece16::wordmemcmp(
+ x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
+ return ((r < 0) || ((r == 0) && (x.size() < y.size())));
+}
+
+inline bool operator>(const StringPiece16& x, const StringPiece16& y) {
+ return y < x;
+}
+
+inline bool operator<=(const StringPiece16& x, const StringPiece16& y) {
+ return !(x > y);
+}
+
+inline bool operator>=(const StringPiece16& x, const StringPiece16& y) {
+ return !(x < y);
+}
+
+BASE_EXPORT std::ostream& operator<<(std::ostream& o,
+ const StringPiece& piece);
+
+} // namespace base
+
+// We provide appropriate hash functions so StringPiece and StringPiece16 can
+// be used as keys in hash sets and maps.
+
+// This hash function is copied from base/hash_tables.h. We don't use the
+// ones already defined for string and string16 directly because it would
+// require the string constructors to be called, which we don't want.
+#define HASH_STRING_PIECE(StringPieceType, string_piece) \
+ std::size_t result = 0; \
+ for (StringPieceType::const_iterator i = string_piece.begin(); \
+ i != string_piece.end(); ++i) \
+ result = (result * 131) + *i; \
+ return result; \
+
+namespace BASE_HASH_NAMESPACE {
+#if defined(COMPILER_GCC)
+
+template<>
+struct hash<base::StringPiece> {
+ std::size_t operator()(const base::StringPiece& sp) const {
+ HASH_STRING_PIECE(base::StringPiece, sp);
+ }
+};
+template<>
+struct hash<base::StringPiece16> {
+ std::size_t operator()(const base::StringPiece16& sp16) const {
+ HASH_STRING_PIECE(base::StringPiece16, sp16);
+ }
+};
+
+#elif defined(COMPILER_MSVC)
+
+inline size_t hash_value(const base::StringPiece& sp) {
+ HASH_STRING_PIECE(base::StringPiece, sp);
+}
+inline size_t hash_value(const base::StringPiece16& sp16) {
+ HASH_STRING_PIECE(base::StringPiece16, sp16);
+}
+
+#endif // COMPILER
+
+} // namespace BASE_HASH_NAMESPACE
+
+#endif // BASE_STRINGS_STRING_PIECE_H_
diff --git a/base/strings/string_piece_unittest.cc b/base/strings/string_piece_unittest.cc
new file mode 100644
index 0000000..6265888
--- /dev/null
+++ b/base/strings/string_piece_unittest.cc
@@ -0,0 +1,677 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+
+#include "base/string16.h"
+#include "base/strings/string_piece.h"
+#include "base/utf_string_conversions.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+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)); \
+ }
+
+#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)); \
+ }
+
+ CMP_Y(==, "", "");
+ CMP_Y(==, "a", "a");
+ CMP_Y(==, "aa", "aa");
+ CMP_N(==, "a", "");
+ CMP_N(==, "", "a");
+ CMP_N(==, "a", "b");
+ CMP_N(==, "a", "aa");
+ CMP_N(==, "aa", "a");
+
+ CMP_N(!=, "", "");
+ CMP_N(!=, "a", "a");
+ CMP_N(!=, "aa", "aa");
+ CMP_Y(!=, "a", "");
+ CMP_Y(!=, "", "a");
+ CMP_Y(!=, "a", "b");
+ CMP_Y(!=, "a", "aa");
+ CMP_Y(!=, "aa", "a");
+
+ CMP_Y(<, "a", "b");
+ CMP_Y(<, "a", "aa");
+ CMP_Y(<, "aa", "b");
+ CMP_Y(<, "aa", "bb");
+ CMP_N(<, "a", "a");
+ CMP_N(<, "b", "a");
+ CMP_N(<, "aa", "a");
+ CMP_N(<, "b", "aa");
+ CMP_N(<, "bb", "aa");
+
+ CMP_Y(<=, "a", "a");
+ CMP_Y(<=, "a", "b");
+ CMP_Y(<=, "a", "aa");
+ CMP_Y(<=, "aa", "b");
+ CMP_Y(<=, "aa", "bb");
+ CMP_N(<=, "b", "a");
+ CMP_N(<=, "aa", "a");
+ CMP_N(<=, "b", "aa");
+ CMP_N(<=, "bb", "aa");
+
+ CMP_N(>=, "a", "b");
+ CMP_N(>=, "a", "aa");
+ CMP_N(>=, "aa", "b");
+ CMP_N(>=, "aa", "bb");
+ CMP_Y(>=, "a", "a");
+ CMP_Y(>=, "b", "a");
+ CMP_Y(>=, "aa", "a");
+ CMP_Y(>=, "b", "aa");
+ CMP_Y(>=, "bb", "aa");
+
+ CMP_N(>, "a", "a");
+ CMP_N(>, "a", "b");
+ CMP_N(>, "a", "aa");
+ CMP_N(>, "aa", "b");
+ CMP_N(>, "aa", "bb");
+ CMP_Y(>, "b", "a");
+ CMP_Y(>, "aa", "a");
+ CMP_Y(>, "b", "aa");
+ CMP_Y(>, "bb", "aa");
+
+ std::string x;
+ for (int i = 0; i < 256; i++) {
+ x += 'a';
+ std::string y = x;
+ CMP_Y(==, x, y);
+ for (int j = 0; j < i; j++) {
+ std::string z = x;
+ z[j] = 'b'; // Differs in position 'j'
+ CMP_N(==, x, z);
+ }
+ }
+
+#undef CMP_Y
+#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'));
+ 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.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_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(), 7U);
+
+ 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());
+}
+
+// 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);
+ ASSERT_EQ(buf[0], a[0]);
+ ASSERT_EQ(buf[1], a[1]);
+ ASSERT_EQ(buf[2], a[2]);
+ ASSERT_EQ(buf[3], a[3]);
+ ASSERT_EQ(a.copy(buf, 3, 7), 3U);
+ ASSERT_EQ(buf[0], a[7]);
+ ASSERT_EQ(buf[1], a[8]);
+ ASSERT_EQ(buf[2], a[9]);
+ ASSERT_EQ(buf[3], a[3]);
+ ASSERT_EQ(c.copy(buf, 99), 3U);
+ ASSERT_EQ(buf[0], c[0]);
+ ASSERT_EQ(buf[1], c[1]);
+ ASSERT_EQ(buf[2], c[2]);
+ ASSERT_EQ(buf[3], a[3]);
+
+ ASSERT_EQ(StringPiece::npos, std::string::npos);
+
+ ASSERT_EQ(a.find(b), 0U);
+ ASSERT_EQ(a.find(b, 1), StringPiece::npos);
+ ASSERT_EQ(a.find(c), 23U);
+ ASSERT_EQ(a.find(c, 9), 23U);
+ ASSERT_EQ(a.find(c, StringPiece::npos), StringPiece::npos);
+ ASSERT_EQ(b.find(c), StringPiece::npos);
+ ASSERT_EQ(b.find(c, StringPiece::npos), StringPiece::npos);
+ ASSERT_EQ(a.find(d), 0U);
+ ASSERT_EQ(a.find(e), 0U);
+ ASSERT_EQ(a.find(d, 12), 12U);
+ ASSERT_EQ(a.find(e, 17), 17U);
+ StringPiece g("xx not found bb");
+ ASSERT_EQ(a.find(g), StringPiece::npos);
+ // empty string nonsense
+ ASSERT_EQ(d.find(b), StringPiece::npos);
+ ASSERT_EQ(e.find(b), StringPiece::npos);
+ ASSERT_EQ(d.find(b, 4), StringPiece::npos);
+ ASSERT_EQ(e.find(b, 7), StringPiece::npos);
+
+ size_t empty_search_pos = std::string().find(std::string());
+ ASSERT_EQ(d.find(d), empty_search_pos);
+ ASSERT_EQ(d.find(e), empty_search_pos);
+ ASSERT_EQ(e.find(d), empty_search_pos);
+ ASSERT_EQ(e.find(e), empty_search_pos);
+ ASSERT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
+ ASSERT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
+ ASSERT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
+ ASSERT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
+
+ ASSERT_EQ(a.find('a'), 0U);
+ ASSERT_EQ(a.find('c'), 2U);
+ ASSERT_EQ(a.find('z'), 25U);
+ ASSERT_EQ(a.find('$'), StringPiece::npos);
+ ASSERT_EQ(a.find('\0'), StringPiece::npos);
+ ASSERT_EQ(f.find('\0'), 3U);
+ ASSERT_EQ(f.find('3'), 2U);
+ ASSERT_EQ(f.find('5'), 5U);
+ ASSERT_EQ(g.find('o'), 4U);
+ ASSERT_EQ(g.find('o', 4), 4U);
+ ASSERT_EQ(g.find('o', 5), 8U);
+ ASSERT_EQ(a.find('b', 5), StringPiece::npos);
+ // empty string nonsense
+ ASSERT_EQ(d.find('\0'), StringPiece::npos);
+ ASSERT_EQ(e.find('\0'), StringPiece::npos);
+ ASSERT_EQ(d.find('\0', 4), StringPiece::npos);
+ ASSERT_EQ(e.find('\0', 7), StringPiece::npos);
+ ASSERT_EQ(d.find('x'), StringPiece::npos);
+ ASSERT_EQ(e.find('x'), StringPiece::npos);
+ ASSERT_EQ(d.find('x', 4), StringPiece::npos);
+ ASSERT_EQ(e.find('x', 7), StringPiece::npos);
+
+ ASSERT_EQ(a.rfind(b), 0U);
+ ASSERT_EQ(a.rfind(b, 1), 0U);
+ ASSERT_EQ(a.rfind(c), 23U);
+ ASSERT_EQ(a.rfind(c, 22U), StringPiece::npos);
+ ASSERT_EQ(a.rfind(c, 1U), StringPiece::npos);
+ ASSERT_EQ(a.rfind(c, 0U), StringPiece::npos);
+ ASSERT_EQ(b.rfind(c), StringPiece::npos);
+ ASSERT_EQ(b.rfind(c, 0U), StringPiece::npos);
+ ASSERT_EQ(a.rfind(d), (size_t) a.as_string().rfind(std::string()));
+ ASSERT_EQ(a.rfind(e), a.as_string().rfind(std::string()));
+ ASSERT_EQ(a.rfind(d, 12), 12U);
+ ASSERT_EQ(a.rfind(e, 17), 17U);
+ ASSERT_EQ(a.rfind(g), StringPiece::npos);
+ ASSERT_EQ(d.rfind(b), StringPiece::npos);
+ ASSERT_EQ(e.rfind(b), StringPiece::npos);
+ ASSERT_EQ(d.rfind(b, 4), StringPiece::npos);
+ ASSERT_EQ(e.rfind(b, 7), StringPiece::npos);
+ // empty string nonsense
+ ASSERT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
+ ASSERT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
+ ASSERT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
+ ASSERT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
+ ASSERT_EQ(d.rfind(d), std::string().rfind(std::string()));
+ ASSERT_EQ(e.rfind(d), std::string().rfind(std::string()));
+ ASSERT_EQ(d.rfind(e), std::string().rfind(std::string()));
+ ASSERT_EQ(e.rfind(e), std::string().rfind(std::string()));
+
+ ASSERT_EQ(g.rfind('o'), 8U);
+ ASSERT_EQ(g.rfind('q'), StringPiece::npos);
+ ASSERT_EQ(g.rfind('o', 8), 8U);
+ ASSERT_EQ(g.rfind('o', 7), 4U);
+ ASSERT_EQ(g.rfind('o', 3), StringPiece::npos);
+ ASSERT_EQ(f.rfind('\0'), 3U);
+ ASSERT_EQ(f.rfind('\0', 12), 3U);
+ ASSERT_EQ(f.rfind('3'), 2U);
+ ASSERT_EQ(f.rfind('5'), 5U);
+ // empty string nonsense
+ ASSERT_EQ(d.rfind('o'), StringPiece::npos);
+ ASSERT_EQ(e.rfind('o'), StringPiece::npos);
+ ASSERT_EQ(d.rfind('o', 4), StringPiece::npos);
+ ASSERT_EQ(e.rfind('o', 7), StringPiece::npos);
+
+ ASSERT_EQ(
+ StringPiece("one,two:three;four").find_first_of(StringPiece(",:"), 1),
+ 3U);
+ ASSERT_EQ(a.find_first_of(b), 0U);
+ ASSERT_EQ(a.find_first_of(b, 0), 0U);
+ ASSERT_EQ(a.find_first_of(b, 1), 1U);
+ ASSERT_EQ(a.find_first_of(b, 2), 2U);
+ ASSERT_EQ(a.find_first_of(b, 3), StringPiece::npos);
+ ASSERT_EQ(a.find_first_of(c), 23U);
+ ASSERT_EQ(a.find_first_of(c, 23), 23U);
+ ASSERT_EQ(a.find_first_of(c, 24), 24U);
+ ASSERT_EQ(a.find_first_of(c, 25), 25U);
+ ASSERT_EQ(a.find_first_of(c, 26), StringPiece::npos);
+ ASSERT_EQ(g.find_first_of(b), 13U);
+ ASSERT_EQ(g.find_first_of(c), 0U);
+ ASSERT_EQ(a.find_first_of(f), StringPiece::npos);
+ ASSERT_EQ(f.find_first_of(a), StringPiece::npos);
+ // empty string nonsense
+ ASSERT_EQ(a.find_first_of(d), StringPiece::npos);
+ ASSERT_EQ(a.find_first_of(e), StringPiece::npos);
+ ASSERT_EQ(d.find_first_of(b), StringPiece::npos);
+ ASSERT_EQ(e.find_first_of(b), StringPiece::npos);
+ ASSERT_EQ(d.find_first_of(d), StringPiece::npos);
+ ASSERT_EQ(e.find_first_of(d), StringPiece::npos);
+ ASSERT_EQ(d.find_first_of(e), StringPiece::npos);
+ ASSERT_EQ(e.find_first_of(e), StringPiece::npos);
+
+ ASSERT_EQ(a.find_first_not_of(b), 3U);
+ ASSERT_EQ(a.find_first_not_of(c), 0U);
+ ASSERT_EQ(b.find_first_not_of(a), StringPiece::npos);
+ ASSERT_EQ(c.find_first_not_of(a), StringPiece::npos);
+ ASSERT_EQ(f.find_first_not_of(a), 0U);
+ ASSERT_EQ(a.find_first_not_of(f), 0U);
+ ASSERT_EQ(a.find_first_not_of(d), 0U);
+ ASSERT_EQ(a.find_first_not_of(e), 0U);
+ // empty string nonsense
+ ASSERT_EQ(d.find_first_not_of(a), StringPiece::npos);
+ ASSERT_EQ(e.find_first_not_of(a), StringPiece::npos);
+ ASSERT_EQ(d.find_first_not_of(d), StringPiece::npos);
+ ASSERT_EQ(e.find_first_not_of(d), StringPiece::npos);
+ ASSERT_EQ(d.find_first_not_of(e), StringPiece::npos);
+ ASSERT_EQ(e.find_first_not_of(e), StringPiece::npos);
+
+ StringPiece h("====");
+ ASSERT_EQ(h.find_first_not_of('='), StringPiece::npos);
+ ASSERT_EQ(h.find_first_not_of('=', 3), StringPiece::npos);
+ ASSERT_EQ(h.find_first_not_of('\0'), 0U);
+ ASSERT_EQ(g.find_first_not_of('x'), 2U);
+ ASSERT_EQ(f.find_first_not_of('\0'), 0U);
+ ASSERT_EQ(f.find_first_not_of('\0', 3), 4U);
+ ASSERT_EQ(f.find_first_not_of('\0', 2), 2U);
+ // empty string nonsense
+ ASSERT_EQ(d.find_first_not_of('x'), StringPiece::npos);
+ ASSERT_EQ(e.find_first_not_of('x'), StringPiece::npos);
+ ASSERT_EQ(d.find_first_not_of('\0'), StringPiece::npos);
+ ASSERT_EQ(e.find_first_not_of('\0'), StringPiece::npos);
+
+ // StringPiece g("xx not found bb");
+ StringPiece i("56");
+ ASSERT_EQ(h.find_last_of(a), StringPiece::npos);
+ ASSERT_EQ(g.find_last_of(a), g.size()-1);
+ ASSERT_EQ(a.find_last_of(b), 2U);
+ ASSERT_EQ(a.find_last_of(c), a.size()-1);
+ ASSERT_EQ(f.find_last_of(i), 6U);
+ ASSERT_EQ(a.find_last_of('a'), 0U);
+ ASSERT_EQ(a.find_last_of('b'), 1U);
+ ASSERT_EQ(a.find_last_of('z'), 25U);
+ ASSERT_EQ(a.find_last_of('a', 5), 0U);
+ ASSERT_EQ(a.find_last_of('b', 5), 1U);
+ ASSERT_EQ(a.find_last_of('b', 0), StringPiece::npos);
+ ASSERT_EQ(a.find_last_of('z', 25), 25U);
+ ASSERT_EQ(a.find_last_of('z', 24), StringPiece::npos);
+ ASSERT_EQ(f.find_last_of(i, 5), 5U);
+ ASSERT_EQ(f.find_last_of(i, 6), 6U);
+ ASSERT_EQ(f.find_last_of(a, 4), StringPiece::npos);
+ // empty string nonsense
+ ASSERT_EQ(f.find_last_of(d), StringPiece::npos);
+ ASSERT_EQ(f.find_last_of(e), StringPiece::npos);
+ ASSERT_EQ(f.find_last_of(d, 4), StringPiece::npos);
+ ASSERT_EQ(f.find_last_of(e, 4), StringPiece::npos);
+ ASSERT_EQ(d.find_last_of(d), StringPiece::npos);
+ ASSERT_EQ(d.find_last_of(e), StringPiece::npos);
+ ASSERT_EQ(e.find_last_of(d), StringPiece::npos);
+ ASSERT_EQ(e.find_last_of(e), StringPiece::npos);
+ ASSERT_EQ(d.find_last_of(f), StringPiece::npos);
+ ASSERT_EQ(e.find_last_of(f), StringPiece::npos);
+ ASSERT_EQ(d.find_last_of(d, 4), StringPiece::npos);
+ ASSERT_EQ(d.find_last_of(e, 4), StringPiece::npos);
+ ASSERT_EQ(e.find_last_of(d, 4), StringPiece::npos);
+ ASSERT_EQ(e.find_last_of(e, 4), StringPiece::npos);
+ ASSERT_EQ(d.find_last_of(f, 4), StringPiece::npos);
+ ASSERT_EQ(e.find_last_of(f, 4), StringPiece::npos);
+
+ ASSERT_EQ(a.find_last_not_of(b), a.size()-1);
+ ASSERT_EQ(a.find_last_not_of(c), 22U);
+ ASSERT_EQ(b.find_last_not_of(a), StringPiece::npos);
+ ASSERT_EQ(b.find_last_not_of(b), StringPiece::npos);
+ ASSERT_EQ(f.find_last_not_of(i), 4U);
+ ASSERT_EQ(a.find_last_not_of(c, 24), 22U);
+ ASSERT_EQ(a.find_last_not_of(b, 3), 3U);
+ ASSERT_EQ(a.find_last_not_of(b, 2), StringPiece::npos);
+ // empty string nonsense
+ ASSERT_EQ(f.find_last_not_of(d), f.size()-1);
+ ASSERT_EQ(f.find_last_not_of(e), f.size()-1);
+ ASSERT_EQ(f.find_last_not_of(d, 4), 4U);
+ ASSERT_EQ(f.find_last_not_of(e, 4), 4U);
+ ASSERT_EQ(d.find_last_not_of(d), StringPiece::npos);
+ ASSERT_EQ(d.find_last_not_of(e), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of(d), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of(e), StringPiece::npos);
+ ASSERT_EQ(d.find_last_not_of(f), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of(f), StringPiece::npos);
+ ASSERT_EQ(d.find_last_not_of(d, 4), StringPiece::npos);
+ ASSERT_EQ(d.find_last_not_of(e, 4), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of(d, 4), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of(e, 4), StringPiece::npos);
+ ASSERT_EQ(d.find_last_not_of(f, 4), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of(f, 4), StringPiece::npos);
+
+ ASSERT_EQ(h.find_last_not_of('x'), h.size() - 1);
+ ASSERT_EQ(h.find_last_not_of('='), StringPiece::npos);
+ ASSERT_EQ(b.find_last_not_of('c'), 1U);
+ ASSERT_EQ(h.find_last_not_of('x', 2), 2U);
+ ASSERT_EQ(h.find_last_not_of('=', 2), StringPiece::npos);
+ ASSERT_EQ(b.find_last_not_of('b', 1), 0U);
+ // empty string nonsense
+ ASSERT_EQ(d.find_last_not_of('x'), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of('x'), StringPiece::npos);
+ ASSERT_EQ(d.find_last_not_of('\0'), StringPiece::npos);
+ ASSERT_EQ(e.find_last_not_of('\0'), StringPiece::npos);
+
+ ASSERT_EQ(a.substr(0, 3), b);
+ ASSERT_EQ(a.substr(23), c);
+ ASSERT_EQ(a.substr(23, 3), c);
+ ASSERT_EQ(a.substr(23, 99), c);
+ ASSERT_EQ(a.substr(0), a);
+ ASSERT_EQ(a.substr(3, 2), "de");
+ // empty string nonsense
+ ASSERT_EQ(a.substr(99, 2), e);
+ ASSERT_EQ(d.substr(99), e);
+ ASSERT_EQ(d.substr(0, 99), e);
+ 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");
+ s1 += '\0';
+ s1 += "456";
+ StringPiece b(s1);
+ StringPiece e;
+ std::string s2;
+
+ // CopyToString
+ a.CopyToString(&s2);
+ ASSERT_EQ(s2.size(), 6U);
+ ASSERT_EQ(s2, "foobar");
+ b.CopyToString(&s2);
+ ASSERT_EQ(s2.size(), 7U);
+ ASSERT_EQ(s1, s2);
+ e.CopyToString(&s2);
+ ASSERT_TRUE(s2.empty());
+
+ // AppendToString
+ s2.erase();
+ a.AppendToString(&s2);
+ ASSERT_EQ(s2.size(), 6U);
+ ASSERT_EQ(s2, "foobar");
+ a.AppendToString(&s2);
+ ASSERT_EQ(s2.size(), 12U);
+ ASSERT_EQ(s2, "foobarfoobar");
+
+ // starts_with
+ ASSERT_TRUE(a.starts_with(a));
+ ASSERT_TRUE(a.starts_with("foo"));
+ ASSERT_TRUE(a.starts_with(e));
+ ASSERT_TRUE(b.starts_with(s1));
+ ASSERT_TRUE(b.starts_with(b));
+ ASSERT_TRUE(b.starts_with(e));
+ ASSERT_TRUE(e.starts_with(""));
+ ASSERT_TRUE(!a.starts_with(b));
+ ASSERT_TRUE(!b.starts_with(a));
+ ASSERT_TRUE(!e.starts_with(a));
+
+ // ends with
+ ASSERT_TRUE(a.ends_with(a));
+ ASSERT_TRUE(a.ends_with("bar"));
+ ASSERT_TRUE(a.ends_with(e));
+ ASSERT_TRUE(b.ends_with(s1));
+ ASSERT_TRUE(b.ends_with(b));
+ ASSERT_TRUE(b.ends_with(e));
+ ASSERT_TRUE(e.ends_with(""));
+ ASSERT_TRUE(!a.ends_with(b));
+ ASSERT_TRUE(!b.ends_with(a));
+ ASSERT_TRUE(!e.ends_with(a));
+
+ StringPiece c;
+ 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);
+}
+
+TYPED_TEST(CommonStringPieceTest, 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);
+ ASSERT_EQ(s.size(), 0U);
+
+ s.set(NULL);
+ ASSERT_EQ(s.data(), (const typename TypeParam::value_type*)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);
+
+ // 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 < BasicStringPiece<TypeParam>(alphabet_z));
+ ASSERT_TRUE(abc.compare(BasicStringPiece<TypeParam>(alphabet_z)) < 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");
+
+ // starts_with
+ ASSERT_TRUE(abc.starts_with(abc));
+ ASSERT_TRUE(abc.starts_with("abcdefghijklm"));
+ ASSERT_TRUE(!abc.starts_with("abcdefguvwxyz"));
+
+ // ends_with
+ ASSERT_TRUE(abc.ends_with(abc));
+ ASSERT_TRUE(!abc.ends_with("abcdefguvwxyz"));
+ 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"));
+}
+
+TYPED_TEST(CommonStringPieceTest, HeterogenousStringPieceEquals) {
+ TypeParam hello(TestFixture::as_string("hello"));
+
+ ASSERT_TRUE(BasicStringPiece<TypeParam>(hello) == hello);
+ ASSERT_TRUE(hello.c_str() == BasicStringPiece<TypeParam>(hello));
+}
+
+// string16-specific stuff
+TEST(StringPiece16Test, CheckSTL) {
+ // Check some non-ascii characters.
+ string16 fifth(ASCIIToUTF16("123"));
+ fifth.push_back(0x0000);
+ fifth.push_back(0xd8c5);
+ fifth.push_back(0xdffe);
+ StringPiece16 f(fifth);
+
+ ASSERT_EQ(f[3], '\0');
+ ASSERT_EQ(f[5], static_cast<char16>(0xdffe));
+
+ ASSERT_EQ(f.size(), 6U);
+}
+
+
+
+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()),
+ "\xf0\x9d\x84\x9e");
+}
+
+TYPED_TEST(CommonStringPieceTest, CheckConstructors) {
+ TypeParam str(TestFixture::as_string("hello world"));
+ TypeParam empty;
+
+ ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str));
+ ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str.c_str()));
+ ASSERT_TRUE(TestFixture::as_string("hello") ==
+ BasicStringPiece<TypeParam>(str.c_str(), 5));
+ ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(str.c_str(), 0U));
+ ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(NULL));
+ ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(NULL, 0U));
+ ASSERT_TRUE(empty == BasicStringPiece<TypeParam>());
+ ASSERT_TRUE(str == BasicStringPiece<TypeParam>(str.begin(), str.end()));
+ ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(str.begin(), str.begin()));
+ ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(empty));
+ ASSERT_TRUE(empty == BasicStringPiece<TypeParam>(empty.begin(), empty.end()));
+}
+
+} // namespace base