diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/string_util.cc | 85 | ||||
-rw-r--r-- | base/string_util.h | 25 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 43 |
3 files changed, 79 insertions, 74 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 2e6f7b3..790cc7b 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -1270,74 +1270,42 @@ void SplitStringAlongWhitespace(const std::wstring& str, } string16 ReplaceStringPlaceholders(const string16& format_string, - const string16& a, - size_t* offset) { - std::vector<size_t> offsets; - string16 result = ReplaceStringPlaceholders(format_string, a, - string16(), - string16(), - string16(), &offsets); - DCHECK(offsets.size() == 1); - if (offset) { - *offset = offsets[0]; - } - return result; -} - -string16 ReplaceStringPlaceholders(const string16& format_string, - const string16& a, - const string16& b, - std::vector<size_t>* offsets) { - return ReplaceStringPlaceholders(format_string, a, b, string16(), - string16(), offsets); -} - -string16 ReplaceStringPlaceholders(const string16& format_string, - const string16& a, - const string16& b, - const string16& c, + const std::vector<const string16>& subst, std::vector<size_t>* offsets) { - return ReplaceStringPlaceholders(format_string, a, b, c, string16(), - offsets); -} - -string16 ReplaceStringPlaceholders(const string16& format_string, - const string16& a, - const string16& b, - const string16& c, - const string16& d, - std::vector<size_t>* offsets) { - // We currently only support up to 4 place holders ($1 through $4), although - // it's easy enough to add more. - const string16* subst_texts[] = { &a, &b, &c, &d }; + int substitutions = subst.size(); + DCHECK(substitutions < 10); + + int sub_length = 0; + for (std::vector<const string16>::const_iterator iter = subst.begin(); + iter != subst.end(); + ++iter) { + sub_length += (*iter).length(); + } string16 formatted; - formatted.reserve(format_string.length() + a.length() + - b.length() + c.length() + d.length()); + formatted.reserve(format_string.length() + sub_length); std::vector<ReplacementOffset> r_offsets; - - // Replace $$ with $ and $1-$4 with placeholder text if it exists. for (string16::const_iterator i = format_string.begin(); i != format_string.end(); ++i) { if ('$' == *i) { if (i + 1 != format_string.end()) { ++i; - DCHECK('$' == *i || ('1' <= *i && *i <= '4')) << - "Invalid placeholder: " << *i; + DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i; if ('$' == *i) { formatted.push_back('$'); } else { int index = *i - '1'; if (offsets) { ReplacementOffset r_offset(index, - static_cast<int>(formatted.size())); + static_cast<int>(formatted.size())); r_offsets.insert(std::lower_bound(r_offsets.begin(), - r_offsets.end(), r_offset, - &CompareParameter), - r_offset); + r_offsets.end(), r_offset, + &CompareParameter), + r_offset); } - formatted.append(*subst_texts[index]); + if (index < substitutions) + formatted.append(subst.at(index)); } } } else { @@ -1346,13 +1314,28 @@ string16 ReplaceStringPlaceholders(const string16& format_string, } if (offsets) { for (std::vector<ReplacementOffset>::const_iterator i = r_offsets.begin(); - i != r_offsets.end(); ++i) { + i != r_offsets.end(); ++i) { offsets->push_back(i->offset); } } return formatted; } +string16 ReplaceStringPlaceholders(const string16& format_string, + const string16& a, + size_t* offset) { + std::vector<size_t> offsets; + std::vector<const string16> subst; + subst.push_back(a); + string16 result = ReplaceStringPlaceholders(format_string, subst, &offsets); + + DCHECK(offsets.size() == 1); + if (offset) { + *offset = offsets[0]; + } + return result; +} + template <class CHAR> static bool IsWildcard(CHAR character) { return character == '*' || character == '?'; diff --git a/base/string_util.h b/base/string_util.h index c32bef3..5a51b2d 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -553,30 +553,17 @@ std::string JoinString(const std::vector<std::string>& parts, char s); void SplitStringAlongWhitespace(const std::wstring& str, std::vector<std::wstring>* result); -// Replace $1-$2-$3 in the format string with |a| and |b| respectively. -// Additionally, $$ is replaced by $. The offset/offsets parameter here can be -// NULL. +// Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively. +// Additionally, $$ is replaced by $. The offsets parameter here can +// be NULL. This only allows you to use up to nine replacements. string16 ReplaceStringPlaceholders(const string16& format_string, - const string16& a, - size_t* offset); - -string16 ReplaceStringPlaceholders(const string16& format_string, - const string16& a, - const string16& b, + const std::vector<const string16>& subst, std::vector<size_t>* offsets); +// Single-string shortcut for ReplaceStringHolders. string16 ReplaceStringPlaceholders(const string16& format_string, const string16& a, - const string16& b, - const string16& c, - std::vector<size_t>* offsets); - -string16 ReplaceStringPlaceholders(const string16& format_string, - const string16& a, - const string16& b, - const string16& c, - const string16& d, - std::vector<size_t>* offsets); + size_t* offset); // If the size of |input| is more than |max_len|, this function returns true and // |input| is shortened into |output| by removing chars in the middle (they are diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index ed6b68f..581cb1f 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -1423,11 +1423,13 @@ TEST(StringUtilTest, StartsWith) { } TEST(StringUtilTest, GetStringFWithOffsets) { + std::vector<const string16> subst; + subst.push_back(ASCIIToUTF16("1")); + subst.push_back(ASCIIToUTF16("2")); std::vector<size_t> offsets; ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $1. Your number is $2."), - ASCIIToUTF16("1"), - ASCIIToUTF16("2"), + subst, &offsets); EXPECT_EQ(2U, offsets.size()); EXPECT_EQ(7U, offsets[0]); @@ -1435,8 +1437,7 @@ TEST(StringUtilTest, GetStringFWithOffsets) { offsets.clear(); ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $2. Your number is $1."), - ASCIIToUTF16("1"), - ASCIIToUTF16("2"), + subst, &offsets); EXPECT_EQ(2U, offsets.size()); EXPECT_EQ(25U, offsets[0]); @@ -1444,6 +1445,40 @@ TEST(StringUtilTest, GetStringFWithOffsets) { offsets.clear(); } +TEST(StringUtilTest, ReplaceStringPlaceholders) { + std::vector<const string16> subst; + subst.push_back(ASCIIToUTF16("9a")); + subst.push_back(ASCIIToUTF16("8b")); + subst.push_back(ASCIIToUTF16("7c")); + subst.push_back(ASCIIToUTF16("6d")); + subst.push_back(ASCIIToUTF16("5e")); + subst.push_back(ASCIIToUTF16("4f")); + subst.push_back(ASCIIToUTF16("3g")); + subst.push_back(ASCIIToUTF16("2h")); + subst.push_back(ASCIIToUTF16("1i")); + + string16 formatted = + ReplaceStringPlaceholders( + ASCIIToUTF16("$1a,$2b,$3c,$4d,$5e,$6f,$7g,$8h,$9i"), subst, NULL); + + EXPECT_EQ(formatted, ASCIIToUTF16("9aa,8bb,7cc,6dd,5ee,4ff,3gg,2hh,1ii")); +} + +TEST(StringUtilTest, ReplaceStringPlaceholdersTooFew) { + // Test whether replacestringplaceholders works as expected when there + // are fewer inputs than outputs. + std::vector<const string16> subst; + subst.push_back(ASCIIToUTF16("9a")); + subst.push_back(ASCIIToUTF16("8b")); + subst.push_back(ASCIIToUTF16("7c")); + + string16 formatted = + ReplaceStringPlaceholders( + ASCIIToUTF16("$1a,$2b,$3c,$4d,$5e,$6f,$1g,$2h,$3i"), subst, NULL); + + EXPECT_EQ(formatted, ASCIIToUTF16("9aa,8bb,7cc,d,e,f,9ag,8bh,7ci")); +} + TEST(StringUtilTest, SplitStringAlongWhitespace) { struct TestData { const std::wstring input; |