Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: base/string_util.cc

Issue 347019: Convert ReplaceStringPlaceholders(std::string...) to take a (Closed)
Patch Set: a Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/string_util.h ('k') | chrome/browser/dom_ui/dom_ui_theme_source.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/string_util.h" 5 #include "base/string_util.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <ctype.h> 9 #include <ctype.h>
10 #include <errno.h> 10 #include <errno.h>
(...skipping 1405 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 std::vector<string16>* result) { 1416 std::vector<string16>* result) {
1417 SplitStringAlongWhitespaceT(str, result); 1417 SplitStringAlongWhitespaceT(str, result);
1418 } 1418 }
1419 #endif 1419 #endif
1420 1420
1421 void SplitStringAlongWhitespace(const std::string& str, 1421 void SplitStringAlongWhitespace(const std::string& str,
1422 std::vector<std::string>* result) { 1422 std::vector<std::string>* result) {
1423 SplitStringAlongWhitespaceT(str, result); 1423 SplitStringAlongWhitespaceT(str, result);
1424 } 1424 }
1425 1425
1426 template<class StringType> 1426 template<class FormatStringType, class OutStringType>
1427 StringType DoReplaceStringPlaceholders(const StringType& format_string, 1427 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string,
1428 const std::vector<StringType>& subst, 1428 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) {
1429 std::vector<size_t>* offsets) {
1430 int substitutions = subst.size(); 1429 int substitutions = subst.size();
1431 DCHECK(substitutions < 10); 1430 DCHECK(substitutions < 10);
1432 1431
1433 int sub_length = 0; 1432 int sub_length = 0;
1434 for (typename std::vector<StringType>::const_iterator iter = subst.begin(); 1433 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin();
1435 iter != subst.end(); 1434 iter != subst.end(); ++iter) {
1436 ++iter) {
1437 sub_length += (*iter).length(); 1435 sub_length += (*iter).length();
1438 } 1436 }
1439 1437
1440 StringType formatted; 1438 OutStringType formatted;
1441 formatted.reserve(format_string.length() + sub_length); 1439 formatted.reserve(format_string.length() + sub_length);
1442 1440
1443 std::vector<ReplacementOffset> r_offsets; 1441 std::vector<ReplacementOffset> r_offsets;
1444 for (typename StringType::const_iterator i = format_string.begin(); 1442 for (typename FormatStringType::const_iterator i = format_string.begin();
1445 i != format_string.end(); ++i) { 1443 i != format_string.end(); ++i) {
1446 if ('$' == *i) { 1444 if ('$' == *i) {
1447 if (i + 1 != format_string.end()) { 1445 if (i + 1 != format_string.end()) {
1448 ++i; 1446 ++i;
1449 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i; 1447 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i;
1450 if ('$' == *i) { 1448 if ('$' == *i) {
1451 formatted.push_back('$'); 1449 formatted.push_back('$');
1452 } else { 1450 } else {
1453 int index = *i - '1'; 1451 int index = *i - '1';
1454 if (offsets) { 1452 if (offsets) {
(...skipping 20 matching lines...) Expand all
1475 } 1473 }
1476 return formatted; 1474 return formatted;
1477 } 1475 }
1478 1476
1479 string16 ReplaceStringPlaceholders(const string16& format_string, 1477 string16 ReplaceStringPlaceholders(const string16& format_string,
1480 const std::vector<string16>& subst, 1478 const std::vector<string16>& subst,
1481 std::vector<size_t>* offsets) { 1479 std::vector<size_t>* offsets) {
1482 return DoReplaceStringPlaceholders(format_string, subst, offsets); 1480 return DoReplaceStringPlaceholders(format_string, subst, offsets);
1483 } 1481 }
1484 1482
1485 std::string ReplaceStringPlaceholders(const std::string& format_string, 1483 std::string ReplaceStringPlaceholders(const base::StringPiece& format_string,
1486 const std::vector<std::string>& subst, 1484 const std::vector<std::string>& subst,
1487 std::vector<size_t>* offsets) { 1485 std::vector<size_t>* offsets) {
1488 return DoReplaceStringPlaceholders(format_string, subst, offsets); 1486 return DoReplaceStringPlaceholders(format_string, subst, offsets);
1489 } 1487 }
1490 1488
1491 string16 ReplaceStringPlaceholders(const string16& format_string, 1489 string16 ReplaceStringPlaceholders(const string16& format_string,
1492 const string16& a, 1490 const string16& a,
1493 size_t* offset) { 1491 size_t* offset) {
1494 std::vector<size_t> offsets; 1492 std::vector<size_t> offsets;
1495 std::vector<string16> subst; 1493 std::vector<string16> subst;
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
1807 // Each input byte creates two output hex characters. 1805 // Each input byte creates two output hex characters.
1808 std::string ret(size * 2, '\0'); 1806 std::string ret(size * 2, '\0');
1809 1807
1810 for (size_t i = 0; i < size; ++i) { 1808 for (size_t i = 0; i < size; ++i) {
1811 char b = reinterpret_cast<const char*>(bytes)[i]; 1809 char b = reinterpret_cast<const char*>(bytes)[i];
1812 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; 1810 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
1813 ret[(i * 2) + 1] = kHexChars[b & 0xf]; 1811 ret[(i * 2) + 1] = kHexChars[b & 0xf];
1814 } 1812 }
1815 return ret; 1813 return ret;
1816 } 1814 }
OLDNEW
« no previous file with comments | « base/string_util.h ('k') | chrome/browser/dom_ui/dom_ui_theme_source.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698