diff options
author | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 00:42:39 +0000 |
---|---|---|
committer | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 00:42:39 +0000 |
commit | 7746aca763ca441dc956c81443239040ffe54c2a (patch) | |
tree | 5b14921e439ef80f41bfff8b650d21363914bd82 /base | |
parent | 0078ffb91ddeea8c35cdd75ffe161623030d8dbf (diff) | |
download | chromium_src-7746aca763ca441dc956c81443239040ffe54c2a.zip chromium_src-7746aca763ca441dc956c81443239040ffe54c2a.tar.gz chromium_src-7746aca763ca441dc956c81443239040ffe54c2a.tar.bz2 |
Undo 16020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16021 0039d316-1c4b-4281-b951-d872f2087c98
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, 74 insertions, 79 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 790cc7b..2e6f7b3 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -1270,42 +1270,74 @@ void SplitStringAlongWhitespace(const std::wstring& str, } string16 ReplaceStringPlaceholders(const string16& format_string, - const std::vector<const string16>& subst, - std::vector<size_t>* offsets) { - 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(); + 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, + 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 }; string16 formatted; - formatted.reserve(format_string.length() + sub_length); + formatted.reserve(format_string.length() + a.length() + + b.length() + c.length() + d.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) << "Invalid placeholder: " << *i; + DCHECK('$' == *i || ('1' <= *i && *i <= '4')) << + "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); } - if (index < substitutions) - formatted.append(subst.at(index)); + formatted.append(*subst_texts[index]); } } } else { @@ -1314,28 +1346,13 @@ 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 5a51b2d..c32bef3 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -553,17 +553,30 @@ 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..$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. +// 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. string16 ReplaceStringPlaceholders(const string16& format_string, - const std::vector<const string16>& subst, + const string16& a, + size_t* offset); + +string16 ReplaceStringPlaceholders(const string16& format_string, + const string16& a, + const string16& b, std::vector<size_t>* offsets); -// Single-string shortcut for ReplaceStringHolders. string16 ReplaceStringPlaceholders(const string16& format_string, const string16& a, - size_t* offset); + 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); // 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 581cb1f..ed6b68f 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -1423,13 +1423,11 @@ 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."), - subst, + ASCIIToUTF16("1"), + ASCIIToUTF16("2"), &offsets); EXPECT_EQ(2U, offsets.size()); EXPECT_EQ(7U, offsets[0]); @@ -1437,7 +1435,8 @@ TEST(StringUtilTest, GetStringFWithOffsets) { offsets.clear(); ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $2. Your number is $1."), - subst, + ASCIIToUTF16("1"), + ASCIIToUTF16("2"), &offsets); EXPECT_EQ(2U, offsets.size()); EXPECT_EQ(25U, offsets[0]); @@ -1445,40 +1444,6 @@ 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; |