Extract common code from StringPiece and StringPiece16 into a templated base...

Extract common code from StringPiece and StringPiece16 into a templated base class. Convert copy-and-pasted unit tests into TYPED_TESTs.

The motivation is that I wish to add constructors for string::iterator ranges and I don't want to do it twice.

The motivation for adding the string::iterator range constructors is to reduce the number of overloads in string_number_conversions.h .

BUG=87634
TEST=

Review URL: http://codereview.chromium.org/8659047

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114242 0039d316-1c4b-4281-b951-d872f2087c98
parent ea097c60
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#pragma once #pragma once
#include "base/i18n/base_i18n_export.h" #include "base/i18n/base_i18n_export.h"
#include "base/string16.h"
#include "base/string_piece.h" #include "base/string_piece.h"
namespace base { namespace base {
......
...@@ -983,7 +983,8 @@ inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { ...@@ -983,7 +983,8 @@ inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
namespace base { namespace base {
class StringPiece; template <typename STRING_TYPE> class BasicStringPiece;
typedef BasicStringPiece<std::string> StringPiece;
// Allows StringPiece to be logged. // Allows StringPiece to be logged.
BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece); BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
......
...@@ -10,6 +10,17 @@ ...@@ -10,6 +10,17 @@
namespace base { 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; typedef StringPiece::size_type size_type;
bool operator==(const StringPiece& x, const StringPiece& y) { bool operator==(const StringPiece& x, const StringPiece& y) {
...@@ -19,22 +30,25 @@ bool operator==(const StringPiece& x, const StringPiece& y) { ...@@ -19,22 +30,25 @@ bool operator==(const StringPiece& x, const StringPiece& y) {
return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
} }
void StringPiece::CopyToString(std::string* target) const { void BasicStringPiece<std::string>::CopyToString(std::string* target) const {
target->assign(!empty() ? data() : "", size()); target->assign(!empty() ? data() : "", size());
} }
void StringPiece::AppendToString(std::string* target) const { void BasicStringPiece<std::string>::AppendToString(std::string* target) const {
if (!empty()) if (!empty())
target->append(data(), size()); target->append(data(), size());
} }
size_type StringPiece::copy(char* buf, size_type n, size_type pos) const { size_type BasicStringPiece<std::string>::copy(char* buf,
size_type n,
size_type pos) const {
size_type ret = std::min(length_ - pos, n); size_type ret = std::min(length_ - pos, n);
memcpy(buf, ptr_ + pos, ret); memcpy(buf, ptr_ + pos, ret);
return ret; return ret;
} }
size_type StringPiece::find(const StringPiece& s, size_type pos) const { size_type BasicStringPiece<std::string>::find(const BasicStringPiece& s,
size_type pos) const {
if (pos > length_) if (pos > length_)
return npos; return npos;
...@@ -44,7 +58,7 @@ size_type StringPiece::find(const StringPiece& s, size_type pos) const { ...@@ -44,7 +58,7 @@ size_type StringPiece::find(const StringPiece& s, size_type pos) const {
return xpos + s.length_ <= length_ ? xpos : npos; return xpos + s.length_ <= length_ ? xpos : npos;
} }
size_type StringPiece::find(char c, size_type pos) const { size_type BasicStringPiece<std::string>::find(char c, size_type pos) const {
if (pos >= length_) if (pos >= length_)
return npos; return npos;
...@@ -52,7 +66,8 @@ size_type StringPiece::find(char c, size_type pos) const { ...@@ -52,7 +66,8 @@ size_type StringPiece::find(char c, size_type pos) const {
return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos; return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos;
} }
size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { size_type BasicStringPiece<std::string>::rfind(const BasicStringPiece& s,
size_type pos) const {
if (length_ < s.length_) if (length_ < s.length_)
return npos; return npos;
...@@ -64,7 +79,7 @@ size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { ...@@ -64,7 +79,7 @@ size_type StringPiece::rfind(const StringPiece& s, size_type pos) const {
return result != last ? static_cast<size_t>(result - ptr_) : npos; return result != last ? static_cast<size_t>(result - ptr_) : npos;
} }
size_type StringPiece::rfind(char c, size_type pos) const { size_type BasicStringPiece<std::string>::rfind(char c, size_type pos) const {
if (length_ == 0) if (length_ == 0)
return npos; return npos;
...@@ -94,8 +109,8 @@ static inline void BuildLookupTable(const StringPiece& characters_wanted, ...@@ -94,8 +109,8 @@ static inline void BuildLookupTable(const StringPiece& characters_wanted,
} }
} }
size_type StringPiece::find_first_of(const StringPiece& s, size_type BasicStringPiece<std::string>::find_first_of(
size_type pos) const { const BasicStringPiece& s, size_type pos) const {
if (length_ == 0 || s.length_ == 0) if (length_ == 0 || s.length_ == 0)
return npos; return npos;
...@@ -113,8 +128,8 @@ size_type StringPiece::find_first_of(const StringPiece& s, ...@@ -113,8 +128,8 @@ size_type StringPiece::find_first_of(const StringPiece& s,
return npos; return npos;
} }
size_type StringPiece::find_first_not_of(const StringPiece& s, size_type BasicStringPiece<std::string>::find_first_not_of(
size_type pos) const { const BasicStringPiece& s, size_type pos) const {
if (length_ == 0) if (length_ == 0)
return npos; return npos;
...@@ -135,7 +150,8 @@ size_type StringPiece::find_first_not_of(const StringPiece& s, ...@@ -135,7 +150,8 @@ size_type StringPiece::find_first_not_of(const StringPiece& s,
return npos; return npos;
} }
size_type StringPiece::find_first_not_of(char c, size_type pos) const { size_type BasicStringPiece<std::string>::find_first_not_of(
char c, size_type pos) const {
if (length_ == 0) if (length_ == 0)
return npos; return npos;
...@@ -147,7 +163,8 @@ size_type StringPiece::find_first_not_of(char c, size_type pos) const { ...@@ -147,7 +163,8 @@ size_type StringPiece::find_first_not_of(char c, size_type pos) const {
return npos; return npos;
} }
size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const { size_type BasicStringPiece<std::string>::find_last_of(const StringPiece& s,
size_type pos) const {
if (length_ == 0 || s.length_ == 0) if (length_ == 0 || s.length_ == 0)
return npos; return npos;
...@@ -166,8 +183,8 @@ size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const { ...@@ -166,8 +183,8 @@ size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const {
return npos; return npos;
} }
size_type StringPiece::find_last_not_of(const StringPiece& s, size_type BasicStringPiece<std::string>::find_last_not_of(
size_type pos) const { const BasicStringPiece& s, size_type pos) const {
if (length_ == 0) if (length_ == 0)
return npos; return npos;
...@@ -190,7 +207,8 @@ size_type StringPiece::find_last_not_of(const StringPiece& s, ...@@ -190,7 +207,8 @@ size_type StringPiece::find_last_not_of(const StringPiece& s,
return npos; return npos;
} }
size_type StringPiece::find_last_not_of(char c, size_type pos) const { size_type BasicStringPiece<std::string>::find_last_not_of(char c,
size_type pos) const {
if (length_ == 0) if (length_ == 0)
return npos; return npos;
...@@ -203,12 +221,11 @@ size_type StringPiece::find_last_not_of(char c, size_type pos) const { ...@@ -203,12 +221,11 @@ size_type StringPiece::find_last_not_of(char c, size_type pos) const {
return npos; return npos;
} }
StringPiece StringPiece::substr(size_type pos, size_type n) const { BasicStringPiece<std::string> BasicStringPiece<std::string>::substr(
size_type pos, size_type n) const {
if (pos > length_) pos = length_; if (pos > length_) pos = length_;
if (n > length_ - pos) n = length_ - pos; if (n > length_ - pos) n = length_ - pos;
return StringPiece(ptr_ + pos, n); return StringPiece(ptr_ + pos, n);
} }
const StringPiece::size_type StringPiece::npos = size_type(-1);
} // namespace base } // namespace base
This diff is collapsed.
This diff is collapsed.
...@@ -11,11 +11,7 @@ ...@@ -11,11 +11,7 @@
#include "base/base_export.h" #include "base/base_export.h"
#include "base/string16.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 // 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 // offsets (|offset[s]_for_adjustment|) into the source strings, each offset
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment