summaryrefslogtreecommitdiffstats
path: root/base/strings
diff options
context:
space:
mode:
Diffstat (limited to 'base/strings')
-rw-r--r--base/strings/string_util.cc23
-rw-r--r--base/strings/string_util.h22
-rw-r--r--base/strings/string_util_unittest.cc22
3 files changed, 15 insertions, 52 deletions
diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc
index ae5fb80..a0c95ec 100644
--- a/base/strings/string_util.cc
+++ b/base/strings/string_util.cc
@@ -884,43 +884,36 @@ char16* WriteInto(base::string16* str, size_t length_with_null) {
return WriteIntoT(str, length_with_null);
}
-} // namespace base
-
template<typename STR>
-static STR JoinStringT(const std::vector<STR>& parts, const STR& sep) {
+static STR JoinStringT(const std::vector<STR>& parts,
+ BasicStringPiece<STR> sep) {
if (parts.empty())
return STR();
STR result(parts[0]);
- typename std::vector<STR>::const_iterator iter = parts.begin();
+ auto iter = parts.begin();
++iter;
for (; iter != parts.end(); ++iter) {
- result += sep;
+ sep.AppendToString(&result);
result += *iter;
}
return result;
}
-std::string JoinString(const std::vector<std::string>& parts, char sep) {
- return JoinStringT(parts, std::string(1, sep));
-}
-
-string16 JoinString(const std::vector<string16>& parts, char16 sep) {
- return JoinStringT(parts, string16(1, sep));
-}
-
std::string JoinString(const std::vector<std::string>& parts,
- const std::string& separator) {
+ StringPiece separator) {
return JoinStringT(parts, separator);
}
string16 JoinString(const std::vector<string16>& parts,
- const string16& separator) {
+ StringPiece16 separator) {
return JoinStringT(parts, separator);
}
+} // namespace base
+
template<class FormatStringType, class OutStringType>
OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string,
const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) {
diff --git a/base/strings/string_util.h b/base/strings/string_util.h
index e4abce2..b75a9f88 100644
--- a/base/strings/string_util.h
+++ b/base/strings/string_util.h
@@ -491,6 +491,12 @@ BASE_EXPORT char16* WriteInto(base::string16* str, size_t length_with_null);
BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null);
#endif
+// Does the opposite of SplitString().
+BASE_EXPORT std::string JoinString(const std::vector<std::string>& parts,
+ StringPiece separator);
+BASE_EXPORT string16 JoinString(const std::vector<string16>& parts,
+ StringPiece16 separator);
+
} // namespace base
#if defined(OS_WIN)
@@ -501,22 +507,6 @@ BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null);
#error Define string operations appropriately for your platform
#endif
-//-----------------------------------------------------------------------------
-
-// Does the opposite of SplitString().
-BASE_EXPORT base::string16 JoinString(const std::vector<base::string16>& parts,
- base::char16 s);
-BASE_EXPORT std::string JoinString(
- const std::vector<std::string>& parts, char s);
-
-// Join |parts| using |separator|.
-BASE_EXPORT std::string JoinString(
- const std::vector<std::string>& parts,
- const std::string& separator);
-BASE_EXPORT base::string16 JoinString(
- const std::vector<base::string16>& parts,
- const base::string16& separator);
-
// Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively.
// Additionally, any number of consecutive '$' characters is replaced by that
// number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be
diff --git a/base/strings/string_util_unittest.cc b/base/strings/string_util_unittest.cc
index 1593f81..5d5ba8b 100644
--- a/base/strings/string_util_unittest.cc
+++ b/base/strings/string_util_unittest.cc
@@ -669,26 +669,7 @@ TEST(StringUtilTest, HexDigitToInt) {
EXPECT_EQ(15, HexDigitToInt('f'));
}
-// Test for JoinString
TEST(StringUtilTest, JoinString) {
- std::vector<std::string> in;
- EXPECT_EQ("", JoinString(in, ','));
-
- in.push_back("a");
- EXPECT_EQ("a", JoinString(in, ','));
-
- in.push_back("b");
- in.push_back("c");
- EXPECT_EQ("a,b,c", JoinString(in, ','));
-
- in.push_back(std::string());
- EXPECT_EQ("a,b,c,", JoinString(in, ','));
- in.push_back(" ");
- EXPECT_EQ("a|b|c|| ", JoinString(in, '|'));
-}
-
-// Test for JoinString overloaded with std::string separator
-TEST(StringUtilTest, JoinStringWithString) {
std::string separator(", ");
std::vector<std::string> parts;
EXPECT_EQ(std::string(), JoinString(parts, separator));
@@ -706,8 +687,7 @@ TEST(StringUtilTest, JoinStringWithString) {
EXPECT_EQ("a|b|c|| ", JoinString(parts, "|"));
}
-// Test for JoinString overloaded with string16 separator
-TEST(StringUtilTest, JoinStringWithString16) {
+TEST(StringUtilTest, JoinString16) {
string16 separator = ASCIIToUTF16(", ");
std::vector<string16> parts;
EXPECT_EQ(string16(), JoinString(parts, separator));