diff options
author | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 00:21:58 +0000 |
---|---|---|
committer | glen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 00:21:58 +0000 |
commit | 0078ffb91ddeea8c35cdd75ffe161623030d8dbf (patch) | |
tree | 96089176d6afd0030fa4eb77a8b6ca7bcbb4ec59 /base/string_util.cc | |
parent | 849a62a1aa77e5d7887bc310233d86cf33ebf393 (diff) | |
download | chromium_src-0078ffb91ddeea8c35cdd75ffe161623030d8dbf.zip chromium_src-0078ffb91ddeea8c35cdd75ffe161623030d8dbf.tar.gz chromium_src-0078ffb91ddeea8c35cdd75ffe161623030d8dbf.tar.bz2 |
Allow the new tab page to be themed (you may want to review DOMUIThemeSource as a whole and not just these changes).
Change global std::strings to chars* in browser theme provider.
Add ability for ReplaceStringPlaceHolder to take up to 9 replacements.
BUG=11235,11685
Review URL: http://codereview.chromium.org/115172
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16020 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r-- | base/string_util.cc | 85 |
1 files changed, 34 insertions, 51 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 == '?'; |