summaryrefslogtreecommitdiffstats
path: root/base/string_util.cc
diff options
context:
space:
mode:
authorglen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-15 22:40:57 +0000
committerglen@chromium.org <glen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-15 22:40:57 +0000
commitdff6d13ca6ddb778867ce2d731d5f8093aef28ea (patch)
tree4758c346a2af607ec2a842c24f222e8fc401b6a8 /base/string_util.cc
parent05062e288e4b8f174a79cc698c3c97d7d12975be (diff)
downloadchromium_src-dff6d13ca6ddb778867ce2d731d5f8093aef28ea.zip
chromium_src-dff6d13ca6ddb778867ce2d731d5f8093aef28ea.tar.gz
chromium_src-dff6d13ca6ddb778867ce2d731d5f8093aef28ea.tar.bz2
Re-land of earlier NTP theme patch; now with mac and linux compatibility.
Review URL: http://codereview.chromium.org/115413 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16205 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r--base/string_util.cc87
1 files changed, 35 insertions, 52 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index 6d9131b..5923c10 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -1275,79 +1275,47 @@ void SplitStringAlongWhitespace(const std::wstring& str,
}
if (!last_was_ws) {
result->push_back(
- str.substr(last_non_ws_start, length - last_non_ws_start));
+ str.substr(last_non_ws_start, length - last_non_ws_start));
}
}
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<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<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 {
@@ -1356,13 +1324,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<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 == '?';