diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-30 08:16:10 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-30 08:16:10 +0000 |
commit | ef92dbefb269e1ca548ccb7a7fecb1d69c3a87fe (patch) | |
tree | 092af8f9dd66777c0b34975cf999d54b37d6279e | |
parent | c8884db174f04db3f766da5d3caadbd2432c6f84 (diff) | |
download | chromium_src-ef92dbefb269e1ca548ccb7a7fecb1d69c3a87fe.zip chromium_src-ef92dbefb269e1ca548ccb7a7fecb1d69c3a87fe.tar.gz chromium_src-ef92dbefb269e1ca548ccb7a7fecb1d69c3a87fe.tar.bz2 |
Make various string_util functions take StringPieces instead of char[].
This allows most callers to pass string literals or results of ASCIIToUTF16(),
rather than construct error-prone character arrays that must be manually
nul-terminated.
Also cleans up various bits of the callers.
BUG=104260
TEST=none
Review URL: https://codereview.chromium.org/296593003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@273765 0039d316-1c4b-4281-b951-d872f2087c98
27 files changed, 152 insertions, 191 deletions
diff --git a/apps/app_window.cc b/apps/app_window.cc index 6412b10..2c421db 100644 --- a/apps/app_window.cc +++ b/apps/app_window.cc @@ -538,22 +538,21 @@ gfx::Rect AppWindow::GetClientBounds() const { } base::string16 AppWindow::GetTitle() const { - base::string16 title; const extensions::Extension* extension = GetExtension(); if (!extension) - return title; + return base::string16(); // WebContents::GetTitle() will return the page's URL if there's no <title> // specified. However, we'd prefer to show the name of the extension in that // case, so we directly inspect the NavigationEntry's title. + base::string16 title; if (!web_contents() || !web_contents()->GetController().GetActiveEntry() || web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) { title = base::UTF8ToUTF16(extension->name()); } else { title = web_contents()->GetTitle(); } - const base::char16 kBadChars[] = {'\n', 0}; - base::RemoveChars(title, kBadChars, &title); + base::RemoveChars(title, base::ASCIIToUTF16("\n"), &title); return title; } diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc index e23317e..e64b95f 100644 --- a/base/strings/string_util.cc +++ b/base/strings/string_util.cc @@ -113,7 +113,7 @@ const string16& EmptyString16() { template<typename STR> bool ReplaceCharsT(const STR& input, - const typename STR::value_type replace_chars[], + const STR& replace_chars, const STR& replace_with, STR* output) { bool removed = false; @@ -132,41 +132,41 @@ bool ReplaceCharsT(const STR& input, } bool ReplaceChars(const string16& input, - const char16 replace_chars[], + const base::StringPiece16& replace_chars, const string16& replace_with, string16* output) { - return ReplaceCharsT(input, replace_chars, replace_with, output); + return ReplaceCharsT(input, replace_chars.as_string(), replace_with, output); } bool ReplaceChars(const std::string& input, - const char replace_chars[], + const base::StringPiece& replace_chars, const std::string& replace_with, std::string* output) { - return ReplaceCharsT(input, replace_chars, replace_with, output); + return ReplaceCharsT(input, replace_chars.as_string(), replace_with, output); } bool RemoveChars(const string16& input, - const char16 remove_chars[], + const base::StringPiece16& remove_chars, string16* output) { - return ReplaceChars(input, remove_chars, string16(), output); + return ReplaceChars(input, remove_chars.as_string(), string16(), output); } bool RemoveChars(const std::string& input, - const char remove_chars[], + const base::StringPiece& remove_chars, std::string* output) { - return ReplaceChars(input, remove_chars, std::string(), output); + return ReplaceChars(input, remove_chars.as_string(), std::string(), output); } template<typename STR> TrimPositions TrimStringT(const STR& input, - const typename STR::value_type trim_chars[], + const STR& trim_chars, TrimPositions positions, STR* output) { // Find the edges of leading/trailing whitespace as desired. - const typename STR::size_type last_char = input.length() - 1; - const typename STR::size_type first_good_char = (positions & TRIM_LEADING) ? + const size_t last_char = input.length() - 1; + const size_t first_good_char = (positions & TRIM_LEADING) ? input.find_first_not_of(trim_chars) : 0; - const typename STR::size_type last_good_char = (positions & TRIM_TRAILING) ? + const size_t last_good_char = (positions & TRIM_TRAILING) ? input.find_last_not_of(trim_chars) : last_char; // When the string was all whitespace, report that we stripped off whitespace @@ -190,15 +190,17 @@ TrimPositions TrimStringT(const STR& input, } bool TrimString(const string16& input, - const char16 trim_chars[], + const base::StringPiece16& trim_chars, string16* output) { - return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; + return TrimStringT(input, trim_chars.as_string(), TRIM_ALL, output) != + TRIM_NONE; } bool TrimString(const std::string& input, - const char trim_chars[], + const base::StringPiece& trim_chars, std::string* output) { - return TrimStringT(input, trim_chars, TRIM_ALL, output) != TRIM_NONE; + return TrimStringT(input, trim_chars.as_string(), TRIM_ALL, output) != + TRIM_NONE; } void TruncateUTF8ToByteSize(const std::string& input, @@ -240,13 +242,14 @@ void TruncateUTF8ToByteSize(const std::string& input, TrimPositions TrimWhitespace(const string16& input, TrimPositions positions, string16* output) { - return TrimStringT(input, kWhitespaceUTF16, positions, output); + return TrimStringT(input, base::string16(kWhitespaceUTF16), positions, + output); } TrimPositions TrimWhitespaceASCII(const std::string& input, TrimPositions positions, std::string* output) { - return TrimStringT(input, kWhitespaceASCII, positions, output); + return TrimStringT(input, std::string(kWhitespaceASCII), positions, output); } // This function is only for backward-compatibility. @@ -435,17 +438,15 @@ bool StartsWith(const string16& str, const string16& search, template <typename STR> bool EndsWithT(const STR& str, const STR& search, bool case_sensitive) { - typename STR::size_type str_length = str.length(); - typename STR::size_type search_length = search.length(); + size_t str_length = str.length(); + size_t search_length = search.length(); if (search_length > str_length) return false; - if (case_sensitive) { + if (case_sensitive) return str.compare(str_length - search_length, search_length, search) == 0; - } else { - return std::equal(search.begin(), search.end(), - str.begin() + (str_length - search_length), - base::CaseInsensitiveCompare<typename STR::value_type>()); - } + return std::equal(search.begin(), search.end(), + str.begin() + (str_length - search_length), + base::CaseInsensitiveCompare<typename STR::value_type>()); } bool EndsWith(const std::string& str, const std::string& search, @@ -491,7 +492,7 @@ string16 FormatBytesUnlocalized(int64 bytes) { template<class StringType> void DoReplaceSubstringsAfterOffset(StringType* str, - typename StringType::size_type start_offset, + size_t start_offset, const StringType& find_this, const StringType& replace_with, bool replace_all) { @@ -499,7 +500,7 @@ void DoReplaceSubstringsAfterOffset(StringType* str, return; DCHECK(!find_this.empty()); - for (typename StringType::size_type offs(str->find(find_this, start_offset)); + for (size_t offs(str->find(find_this, start_offset)); offs != StringType::npos; offs = str->find(find_this, offs)) { str->replace(offs, find_this.length(), replace_with); offs += replace_with.length(); @@ -510,7 +511,7 @@ void DoReplaceSubstringsAfterOffset(StringType* str, } void ReplaceFirstSubstringAfterOffset(string16* str, - string16::size_type start_offset, + size_t start_offset, const string16& find_this, const string16& replace_with) { DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, @@ -518,7 +519,7 @@ void ReplaceFirstSubstringAfterOffset(string16* str, } void ReplaceFirstSubstringAfterOffset(std::string* str, - std::string::size_type start_offset, + size_t start_offset, const std::string& find_this, const std::string& replace_with) { DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, @@ -526,7 +527,7 @@ void ReplaceFirstSubstringAfterOffset(std::string* str, } void ReplaceSubstringsAfterOffset(string16* str, - string16::size_type start_offset, + size_t start_offset, const string16& find_this, const string16& replace_with) { DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, @@ -534,7 +535,7 @@ void ReplaceSubstringsAfterOffset(string16* str, } void ReplaceSubstringsAfterOffset(std::string* str, - std::string::size_type start_offset, + size_t start_offset, const std::string& find_this, const std::string& replace_with) { DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, @@ -548,9 +549,9 @@ static size_t TokenizeT(const STR& str, std::vector<STR>* tokens) { tokens->clear(); - typename STR::size_type start = str.find_first_not_of(delimiters); + size_t start = str.find_first_not_of(delimiters); while (start != STR::npos) { - typename STR::size_type end = str.find_first_of(delimiters, start + 1); + size_t end = str.find_first_of(delimiters, start + 1); if (end == STR::npos) { tokens->push_back(str.substr(start)); break; diff --git a/base/strings/string_util.h b/base/strings/string_util.h index 9478a0c..a573e22 100644 --- a/base/strings/string_util.h +++ b/base/strings/string_util.h @@ -150,10 +150,10 @@ BASE_EXPORT extern const char kUtf8ByteOrderMark[]; // if any characters were removed. |remove_chars| must be null-terminated. // NOTE: Safe to use the same variable for both |input| and |output|. BASE_EXPORT bool RemoveChars(const string16& input, - const char16 remove_chars[], + const base::StringPiece16& remove_chars, string16* output); BASE_EXPORT bool RemoveChars(const std::string& input, - const char remove_chars[], + const base::StringPiece& remove_chars, std::string* output); // Replaces characters in |replace_chars| from anywhere in |input| with @@ -162,11 +162,11 @@ BASE_EXPORT bool RemoveChars(const std::string& input, // |replace_chars| must be null-terminated. // NOTE: Safe to use the same variable for both |input| and |output|. BASE_EXPORT bool ReplaceChars(const string16& input, - const char16 replace_chars[], + const base::StringPiece16& replace_chars, const string16& replace_with, string16* output); BASE_EXPORT bool ReplaceChars(const std::string& input, - const char replace_chars[], + const base::StringPiece& replace_chars, const std::string& replace_with, std::string* output); @@ -174,10 +174,10 @@ BASE_EXPORT bool ReplaceChars(const std::string& input, // |trim_chars| must be null-terminated. // NOTE: Safe to use the same variable for both |input| and |output|. BASE_EXPORT bool TrimString(const string16& input, - const char16 trim_chars[], + const base::StringPiece16& trim_chars, string16* output); BASE_EXPORT bool TrimString(const std::string& input, - const char trim_chars[], + const base::StringPiece& trim_chars, std::string* output); // Truncates a string to the nearest UTF-8 character that will leave @@ -378,12 +378,12 @@ BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes); // |find_this| with |replace_with|. BASE_EXPORT void ReplaceFirstSubstringAfterOffset( base::string16* str, - base::string16::size_type start_offset, + size_t start_offset, const base::string16& find_this, const base::string16& replace_with); BASE_EXPORT void ReplaceFirstSubstringAfterOffset( std::string* str, - std::string::size_type start_offset, + size_t start_offset, const std::string& find_this, const std::string& replace_with); @@ -395,14 +395,13 @@ BASE_EXPORT void ReplaceFirstSubstringAfterOffset( // std::replace(str.begin(), str.end(), 'a', 'b'); BASE_EXPORT void ReplaceSubstringsAfterOffset( base::string16* str, - base::string16::size_type start_offset, + size_t start_offset, const base::string16& find_this, const base::string16& replace_with); -BASE_EXPORT void ReplaceSubstringsAfterOffset( - std::string* str, - std::string::size_type start_offset, - const std::string& find_this, - const std::string& replace_with); +BASE_EXPORT void ReplaceSubstringsAfterOffset(std::string* str, + size_t start_offset, + const std::string& find_this, + const std::string& replace_with); // Reserves enough memory in |str| to accommodate |length_with_null| characters, // sets the size of |str| to |length_with_null - 1| characters, and returns a diff --git a/chrome/browser/autocomplete/autocomplete_input.cc b/chrome/browser/autocomplete/autocomplete_input.cc index af34933..f85b5db 100644 --- a/chrome/browser/autocomplete/autocomplete_input.cc +++ b/chrome/browser/autocomplete/autocomplete_input.cc @@ -133,7 +133,7 @@ AutocompleteInput::Type AutocompleteInput::Parse( if (first_non_white == base::string16::npos) return INVALID; // All whitespace. - if (text.at(first_non_white) == L'?') { + if (text[first_non_white] == L'?') { // If the first non-whitespace character is a '?', we magically treat this // as a query. return FORCED_QUERY; diff --git a/chrome/browser/autocomplete/builtin_provider.cc b/chrome/browser/autocomplete/builtin_provider.cc index 5b35c75..df0fc43 100644 --- a/chrome/browser/autocomplete/builtin_provider.cc +++ b/chrome/browser/autocomplete/builtin_provider.cc @@ -107,8 +107,7 @@ void BuiltinProvider::Start(const AutocompleteInput& input, !url.has_query() && !url.has_ref()) { // Include the path for sub-pages (e.g. "chrome://settings/browser"). base::string16 host_and_path = base::UTF8ToUTF16(url.host() + url.path()); - base::TrimString(host_and_path, base::ASCIIToUTF16("/").c_str(), - &host_and_path); + base::TrimString(host_and_path, base::ASCIIToUTF16("/"), &host_and_path); size_t match_length = kChrome.length() + host_and_path.length(); for (Builtins::const_iterator i(builtins_.begin()); (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { diff --git a/chrome/browser/chromeos/drive/file_system_util.cc b/chrome/browser/chromeos/drive/file_system_util.cc index 5cba03b..1abe96f 100644 --- a/chrome/browser/chromeos/drive/file_system_util.cc +++ b/chrome/browser/chromeos/drive/file_system_util.cc @@ -48,21 +48,6 @@ namespace util { namespace { -const base::FilePath::CharType kSpecialMountPointRoot[] = - FILE_PATH_LITERAL("/special"); - -const char kDriveMountPointNameBase[] = "drive"; - -const base::FilePath::CharType kDriveMyDriveRootPath[] = - FILE_PATH_LITERAL("drive/root"); - -const base::FilePath::CharType kFileCacheVersionDir[] = - FILE_PATH_LITERAL("v1"); - -const char kSlash[] = "/"; -const char kDot = '.'; -const char kEscapedChars[] = "_"; - std::string ReadStringFromGDocFile(const base::FilePath& file_path, const std::string& key) { const int64 kMaxGDocSize = 4096; @@ -123,12 +108,15 @@ const base::FilePath& GetDriveGrandRootPath() { const base::FilePath& GetDriveMyDriveRootPath() { CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_root_path, - (kDriveMyDriveRootPath)); + (FILE_PATH_LITERAL("drive/root"))); return drive_root_path; } base::FilePath GetDriveMountPointPathForUserIdHash( const std::string user_id_hash) { + static const base::FilePath::CharType kSpecialMountPointRoot[] = + FILE_PATH_LITERAL("/special"); + static const char kDriveMountPointNameBase[] = "drive"; return base::FilePath(kSpecialMountPointRoot).AppendASCII( net::EscapePath(kDriveMountPointNameBase + (user_id_hash.empty() ? "" : "-" + user_id_hash))); @@ -267,6 +255,8 @@ base::FilePath GetCacheRootPath(Profile* profile) { chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); base::FilePath cache_root_path = cache_base_path.Append(chromeos::kDriveCacheDirname); + static const base::FilePath::CharType kFileCacheVersionDir[] = + FILE_PATH_LITERAL("v1"); return cache_root_path.Append(kFileCacheVersionDir); } @@ -304,9 +294,9 @@ std::string NormalizeFileName(const std::string& input) { std::string output; if (!base::ConvertToUtf8AndNormalize(input, base::kCodepageUTF8, &output)) output = input; - base::ReplaceChars(output, kSlash, std::string(kEscapedChars), &output); - if (!output.empty() && output.find_first_not_of(kDot, 0) == std::string::npos) - output = kEscapedChars; + base::ReplaceChars(output, "/", "_", &output); + if (!output.empty() && output.find_first_not_of('.', 0) == std::string::npos) + output = "_"; return output; } diff --git a/chrome/browser/drive/drive_api_util.cc b/chrome/browser/drive/drive_api_util.cc index b7b350a..13cefa6 100644 --- a/chrome/browser/drive/drive_api_util.cc +++ b/chrome/browser/drive/drive_api_util.cc @@ -66,8 +66,7 @@ std::string TranslateQuery(const std::string& original_query) { // In order to handle non-ascii white spaces correctly, convert to UTF16. base::string16 query = base::UTF8ToUTF16(original_query); const base::string16 kDelimiter( - base::kWhitespaceUTF16 + - base::string16(1, static_cast<base::char16>('"'))); + base::kWhitespaceUTF16 + base::ASCIIToUTF16("\"")); std::string result; for (size_t index = query.find_first_not_of(base::kWhitespaceUTF16); diff --git a/chrome/browser/errorpage_browsertest.cc b/chrome/browser/errorpage_browsertest.cc index c23c7d0..025492f 100644 --- a/chrome/browser/errorpage_browsertest.cc +++ b/chrome/browser/errorpage_browsertest.cc @@ -89,8 +89,9 @@ bool WARN_UNUSED_RESULT IsDisplayingNetError(Browser* browser, net::Error error_code) { // Get the error as a string, and remove the leading "net::", which is not // included on error pages. - std::string error_string = net::ErrorToString(error_code); - base::RemoveChars(error_string, "net:", &error_string); + std::string error_string(net::ErrorToString(error_code)); + DCHECK(StartsWithASCII(error_string, "net::", true)); + error_string.erase(0, 5); return IsDisplayingText(browser, error_string); } diff --git a/chrome/browser/guest_view/ad_view/ad_view_guest.cc b/chrome/browser/guest_view/ad_view/ad_view_guest.cc index 5144729..155e589 100644 --- a/chrome/browser/guest_view/ad_view/ad_view_guest.cc +++ b/chrome/browser/guest_view/ad_view/ad_view_guest.cc @@ -49,8 +49,9 @@ void AdViewGuest::DidFailProvisionalLoad( const base::string16& error_description, content::RenderViewHost* render_view_host) { // Translate the |error_code| into an error string. - std::string error_type; - base::RemoveChars(net::ErrorToString(error_code), "net::", &error_type); + std::string error_type(net::ErrorToString(error_code)); + DCHECK(StartsWithASCII(error_type, "net::", true)); + error_type.erase(0, 5); scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); args->SetBoolean(guestview::kIsTopLevel, is_main_frame); diff --git a/chrome/browser/guest_view/web_view/web_view_guest.cc b/chrome/browser/guest_view/web_view/web_view_guest.cc index bf6e50e..e1073dc 100644 --- a/chrome/browser/guest_view/web_view/web_view_guest.cc +++ b/chrome/browser/guest_view/web_view/web_view_guest.cc @@ -822,8 +822,9 @@ void WebViewGuest::DidFailProvisionalLoad( const base::string16& error_description, content::RenderViewHost* render_view_host) { // Translate the |error_code| into an error string. - std::string error_type; - base::RemoveChars(net::ErrorToString(error_code), "net::", &error_type); + std::string error_type(net::ErrorToString(error_code)); + DCHECK(StartsWithASCII(error_type, "net::", true)); + error_type.erase(0, 5); LoadAbort(is_main_frame, validated_url, error_type); } @@ -1076,9 +1077,9 @@ void WebViewGuest::NavigateGuest(const std::string& src) { !url.SchemeIs(content::kAboutScheme)) || url.SchemeIs(url::kJavaScriptScheme); if (scheme_is_blocked || !url.is_valid()) { - std::string error_type; - base::RemoveChars(net::ErrorToString(net::ERR_ABORTED), "net::", - &error_type); + std::string error_type(net::ErrorToString(net::ERR_ABORTED)); + DCHECK(StartsWithASCII(error_type, "net::", true)); + error_type.erase(0, 5); LoadAbort(true /* is_top_level */, url, error_type); return; } diff --git a/chrome/browser/profile_resetter/resettable_settings_snapshot.cc b/chrome/browser/profile_resetter/resettable_settings_snapshot.cc index 7ba8be2..44bee23 100644 --- a/chrome/browser/profile_resetter/resettable_settings_snapshot.cc +++ b/chrome/browser/profile_resetter/resettable_settings_snapshot.cc @@ -200,7 +200,7 @@ std::string SerializeSettingsReport(const ResettableSettingsSnapshot& snapshot, i != shortcuts.end(); ++i) { base::string16 arguments; // Replace "\"" to simplify server-side analysis. - base::ReplaceChars(i->second, base::ASCIIToUTF16("\"").c_str(), + base::ReplaceChars(i->second, base::ASCIIToUTF16("\""), base::ASCIIToUTF16("\'"), &arguments); list->AppendString(arguments); } diff --git a/chrome/browser/renderer_context_menu/render_view_context_menu.cc b/chrome/browser/renderer_context_menu/render_view_context_menu.cc index be70702..2e67237 100644 --- a/chrome/browser/renderer_context_menu/render_view_context_menu.cc +++ b/chrome/browser/renderer_context_menu/render_view_context_menu.cc @@ -372,8 +372,8 @@ void AddCustomItemsToMenu(const std::vector<content::MenuItem>& items, // Helper function to escape "&" as "&&". void EscapeAmpersands(base::string16* text) { - const base::char16 ampersand[] = {'&', 0}; - base::ReplaceChars(*text, ampersand, base::ASCIIToUTF16("&&"), text); + base::ReplaceChars(*text, base::ASCIIToUTF16("&"), base::ASCIIToUTF16("&&"), + text); } } // namespace diff --git a/chrome/browser/safe_browsing/safe_browsing_util.cc b/chrome/browser/safe_browsing/safe_browsing_util.cc index 0334637..4b04c66 100644 --- a/chrome/browser/safe_browsing/safe_browsing_util.cc +++ b/chrome/browser/safe_browsing/safe_browsing_util.cc @@ -335,9 +335,8 @@ void CanonicalizeUrl(const GURL& url, (parsed.host.len > 0) ? url_unescaped_str.substr(parsed.host.begin, parsed.host.len) : std::string(); - const char kCharsToTrim[] = "."; std::string host_without_end_dots; - base::TrimString(host, kCharsToTrim, &host_without_end_dots); + base::TrimString(host, ".", &host_without_end_dots); // 4. In hostname, replace consecutive dots with a single dot. std::string host_without_consecutive_dots(RemoveConsecutiveChars( diff --git a/chrome/browser/ui/omnibox/location_bar_util.cc b/chrome/browser/ui/omnibox/location_bar_util.cc index 93c8b5f..42a24f5 100644 --- a/chrome/browser/ui/omnibox/location_bar_util.cc +++ b/chrome/browser/ui/omnibox/location_bar_util.cc @@ -6,6 +6,7 @@ #include "base/i18n/rtl.h" #include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" #include "chrome/browser/extensions/extension_action.h" #include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/effects/SkGradientShader.h" @@ -18,16 +19,10 @@ namespace location_bar_util { base::string16 CalculateMinString(const base::string16& description) { // Chop at the first '.' or whitespace. - const size_t dot_index = description.find('.'); - const size_t ws_index = description.find_first_of(base::kWhitespaceUTF16); - size_t chop_index = std::min(dot_index, ws_index); - base::string16 min_string; - if (chop_index == base::string16::npos) { - // No dot or whitespace, truncate to at most 3 chars. - min_string = gfx::TruncateString(description, 3); - } else { - min_string = description.substr(0, chop_index); - } + const size_t chop_index = description.find_first_of( + base::kWhitespaceUTF16 + base::ASCIIToUTF16(".")); + base::string16 min_string((chop_index == base::string16::npos) ? + gfx::TruncateString(description, 3) : description.substr(0, chop_index)); base::i18n::AdjustStringForLocaleDirection(&min_string); return min_string; } diff --git a/chrome/common/localized_error.cc b/chrome/common/localized_error.cc index 1645d11..ecff88f 100644 --- a/chrome/common/localized_error.cc +++ b/chrome/common/localized_error.cc @@ -580,7 +580,8 @@ void LocalizedError::GetStrings(int error_code, // Non-internationalized error string, for debugging Chrome itself. std::string ascii_error_string = net::ErrorToString(error_code); // Remove the leading "net::" from the returned string. - base::RemoveChars(ascii_error_string, "net:", &ascii_error_string); + DCHECK(StartsWithASCII(ascii_error_string, "net::", true)); + ascii_error_string.erase(0, 5); error_string = base::ASCIIToUTF16(ascii_error_string); } else if (error_domain == chrome_common_net::kDnsProbeErrorDomain) { std::string ascii_error_string = diff --git a/chrome/installer/setup/install.cc b/chrome/installer/setup/install.cc index cb675ac..05e5b9b 100644 --- a/chrome/installer/setup/install.cc +++ b/chrome/installer/setup/install.cc @@ -42,11 +42,6 @@ #include "chrome/installer/util/util_constants.h" #include "chrome/installer/util/work_item_list.h" -using base::ASCIIToUTF16; -using base::UTF16ToUTF8; -using installer::InstallerState; -using installer::InstallationState; -using installer::Product; namespace { @@ -78,13 +73,13 @@ void LogShortcutOperation(ShellUtil::ShortcutLocation location, break; case ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR: message.append("Start menu/" + - UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder( + base::UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder( BrowserDistribution::SUBFOLDER_CHROME)) + " "); break; case ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR: message.append("Start menu/" + - UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder( + base::UTF16ToUTF8(dist->GetStartMenuShortcutSubfolder( BrowserDistribution::SUBFOLDER_APPS)) + " "); break; @@ -94,15 +89,15 @@ void LogShortcutOperation(ShellUtil::ShortcutLocation location, message.push_back('"'); if (properties.has_shortcut_name()) - message.append(UTF16ToUTF8(properties.shortcut_name)); + message.append(base::UTF16ToUTF8(properties.shortcut_name)); else - message.append(UTF16ToUTF8(dist->GetDisplayName())); + message.append(base::UTF16ToUTF8(dist->GetDisplayName())); message.push_back('"'); message.append(" shortcut to "); - message.append(UTF16ToUTF8(properties.target.value())); + message.append(base::UTF16ToUTF8(properties.target.value())); if (properties.has_arguments()) - message.append(UTF16ToUTF8(properties.arguments)); + message.append(base::UTF16ToUTF8(properties.arguments)); if (properties.pin_to_taskbar && base::win::GetVersion() >= base::win::VERSION_WIN7) { @@ -146,8 +141,9 @@ void AddChromeToMediaPlayerList() { // Copy master_preferences file provided to installer, in the same folder // as chrome.exe so Chrome first run can find it. This function will be called // only on the first install of Chrome. -void CopyPreferenceFileForFirstRun(const InstallerState& installer_state, - const base::FilePath& prefs_source_path) { +void CopyPreferenceFileForFirstRun( + const installer::InstallerState& installer_state, + const base::FilePath& prefs_source_path) { base::FilePath prefs_dest_path(installer_state.target_path().AppendASCII( installer::kDefaultMasterPrefs)); if (!base::CopyFile(prefs_source_path, prefs_dest_path)) { @@ -177,8 +173,8 @@ void CopyPreferenceFileForFirstRun(const InstallerState& installer_state, // (typical new install), the function creates package during install // and removes the whole directory during rollback. installer::InstallStatus InstallNewVersion( - const InstallationState& original_state, - const InstallerState& installer_state, + const installer::InstallationState& original_state, + const installer::InstallerState& installer_state, const base::FilePath& setup_path, const base::FilePath& archive_path, const base::FilePath& src_path, @@ -256,7 +252,7 @@ installer::InstallStatus InstallNewVersion( // Launch shortcut. Both of these were created prior to Chrome 24; in Chrome 24, // the uninstall shortcut was removed and the Default user Quick Launch shortcut // was replaced by per-user shortcuts created via Active Setup. -void CleanupLegacyShortcuts(const InstallerState& installer_state, +void CleanupLegacyShortcuts(const installer::InstallerState& installer_state, BrowserDistribution* dist, const base::FilePath& chrome_exe) { ShellUtil::ShellChange shortcut_level = installer_state.system_install() ? @@ -278,8 +274,8 @@ void CleanupLegacyShortcuts(const InstallerState& installer_state, // Returns the appropriate shortcut operations for App Launcher, // based on state of installation and master_preferences. installer::InstallShortcutOperation GetAppLauncherShortcutOperation( - const InstallationState& original_state, - const InstallerState& installer_state) { + const installer::InstallationState& original_state, + const installer::InstallerState& installer_state) { const installer::ProductState* original_app_host_state = original_state.GetProductState(installer_state.system_install(), BrowserDistribution::CHROME_APP_HOST); @@ -297,15 +293,18 @@ installer::InstallShortcutOperation GetAppLauncherShortcutOperation( namespace installer { void EscapeXmlAttributeValueInSingleQuotes(base::string16* att_value) { - base::ReplaceChars(*att_value, L"&", L"&", att_value); - base::ReplaceChars(*att_value, L"'", L"'", att_value); - base::ReplaceChars(*att_value, L"<", L"<", att_value); + base::ReplaceChars(*att_value, base::ASCIIToUTF16("&"), + base::ASCIIToUTF16("&"), att_value); + base::ReplaceChars(*att_value, base::ASCIIToUTF16("'"), + base::ASCIIToUTF16("'"), att_value); + base::ReplaceChars(*att_value, base::ASCIIToUTF16("<"), + base::ASCIIToUTF16("<"), att_value); } bool CreateVisualElementsManifest(const base::FilePath& src_path, const Version& version) { // Construct the relative path to the versioned VisualElements directory. - base::string16 elements_dir(ASCIIToUTF16(version.GetString())); + base::string16 elements_dir(base::ASCIIToUTF16(version.GetString())); elements_dir.push_back(base::FilePath::kSeparators[0]); elements_dir.append(installer::kVisualElements); @@ -333,7 +332,8 @@ bool CreateVisualElementsManifest(const base::FilePath& src_path, " </VisualElements>\r\n" "</Application>"; - const base::string16 manifest_template(ASCIIToUTF16(kManifestTemplate)); + const base::string16 manifest_template( + base::ASCIIToUTF16(kManifestTemplate)); BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution( BrowserDistribution::CHROME_BROWSER); @@ -347,7 +347,7 @@ bool CreateVisualElementsManifest(const base::FilePath& src_path, manifest_template.c_str(), display_name.c_str(), elements_dir.c_str())); // Write the manifest to |src_path|. - const std::string manifest(UTF16ToUTF8(manifest16)); + const std::string manifest(base::UTF16ToUTF8(manifest16)); int size = base::checked_cast<int>(manifest.size()); if (base::WriteFile( src_path.Append(installer::kVisualElementsManifest), @@ -365,7 +365,7 @@ bool CreateVisualElementsManifest(const base::FilePath& src_path, void CreateOrUpdateShortcuts( const base::FilePath& target, - const Product& product, + const installer::Product& product, const MasterPreferences& prefs, InstallShortcutLevel install_level, InstallShortcutOperation install_operation) { @@ -469,8 +469,8 @@ void CreateOrUpdateShortcuts( start_menu_properties, shortcut_operation); } -void RegisterChromeOnMachine(const InstallerState& installer_state, - const Product& product, +void RegisterChromeOnMachine(const installer::InstallerState& installer_state, + const installer::Product& product, bool make_chrome_default) { DCHECK(product.is_chrome()); @@ -496,8 +496,8 @@ void RegisterChromeOnMachine(const InstallerState& installer_state, } InstallStatus InstallOrUpdateProduct( - const InstallationState& original_state, - const InstallerState& installer_state, + const installer::InstallationState& original_state, + const installer::InstallerState& installer_state, const base::FilePath& setup_path, const base::FilePath& archive_path, const base::FilePath& install_temp_path, @@ -542,7 +542,7 @@ InstallStatus InstallOrUpdateProduct( installer_state.UpdateStage(installer::CREATING_SHORTCUTS); - const Product* app_launcher_product = + const installer::Product* app_launcher_product = installer_state.FindProduct(BrowserDistribution::CHROME_APP_HOST); // Creates shortcuts for App Launcher. if (app_launcher_product) { @@ -558,7 +558,7 @@ InstallStatus InstallOrUpdateProduct( CURRENT_USER, app_launcher_shortcut_operation); } - const Product* chrome_product = + const installer::Product* chrome_product = installer_state.FindProduct(BrowserDistribution::CHROME_BROWSER); // Creates shortcuts for Chrome. if (chrome_product) { @@ -623,7 +623,7 @@ InstallStatus InstallOrUpdateProduct( &auto_launch_chrome); if (auto_launch_chrome) { auto_launch_util::EnableForegroundStartAtLogin( - ASCIIToUTF16(chrome::kInitialProfile), + base::ASCIIToUTF16(chrome::kInitialProfile), installer_state.target_path()); } } @@ -640,8 +640,8 @@ InstallStatus InstallOrUpdateProduct( return result; } -void HandleOsUpgradeForBrowser(const InstallerState& installer_state, - const Product& chrome) { +void HandleOsUpgradeForBrowser(const installer::InstallerState& installer_state, + const installer::Product& chrome) { DCHECK(chrome.is_chrome()); // Upon upgrading to Windows 8, we need to fix Chrome shortcuts and register // Chrome, so that Metro Chrome would work if Chrome is the default browser. @@ -669,7 +669,7 @@ void HandleOsUpgradeForBrowser(const InstallerState& installer_state, // in install_worker.cc needs to be increased for Active Setup to invoke this // again for all users of this install. void HandleActiveSetupForBrowser(const base::FilePath& installation_root, - const Product& chrome, + const installer::Product& chrome, bool force) { DCHECK(chrome.is_chrome()); // Only create shortcuts on Active Setup if the first run sentinel is not diff --git a/chrome/installer/util/shell_util.cc b/chrome/installer/util/shell_util.cc index 4f97095..222d8b7 100644 --- a/chrome/installer/util/shell_util.cc +++ b/chrome/installer/util/shell_util.cc @@ -297,7 +297,7 @@ class RegistryEntry { entries->push_back(new RegistryEntry( chrome_html_prog_id, dist->GetBrowserProgIdDesc())); entries->push_back(new RegistryEntry( - chrome_html_prog_id, ShellUtil::kRegUrlProtocol, L"")); + chrome_html_prog_id, ShellUtil::kRegUrlProtocol, base::string16())); entries->push_back(new RegistryEntry( chrome_html_prog_id + ShellUtil::kRegDefaultIcon, icon_path)); entries->push_back(new RegistryEntry( @@ -474,7 +474,7 @@ class RegistryEntry { // so IE, explorer and other apps will route it to our handler. // <root hkey>\Software\Classes\<protocol>\URL Protocol entries->push_back(new RegistryEntry(url_key, - ShellUtil::kRegUrlProtocol, L"")); + ShellUtil::kRegUrlProtocol, base::string16())); // <root hkey>\Software\Classes\<protocol>\DefaultIcon base::string16 icon_key = url_key + ShellUtil::kRegDefaultIcon; @@ -486,7 +486,7 @@ class RegistryEntry { // <root hkey>\Software\Classes\<protocol>\shell\open\ddeexec base::string16 dde_key = url_key + L"\\shell\\open\\ddeexec"; - entries->push_back(new RegistryEntry(dde_key, L"")); + entries->push_back(new RegistryEntry(dde_key, base::string16())); // <root hkey>\Software\Classes\<protocol>\shell\@ base::string16 protocol_shell_key = url_key + ShellUtil::kRegShellPath; @@ -1759,7 +1759,8 @@ base::string16 ShellUtil::BuildAppModelId( } } // No spaces are allowed in the AppUserModelId according to MSDN. - base::ReplaceChars(app_id, L" ", L"_", &app_id); + base::ReplaceChars(app_id, base::ASCIIToUTF16(" "), base::ASCIIToUTF16("_"), + &app_id); return app_id; } @@ -2057,8 +2058,8 @@ bool ShellUtil::RegisterChromeBrowser(BrowserDistribution* dist, result = (AddRegistryEntries(root, progid_and_appreg_entries) && AddRegistryEntries(root, shell_entries)); } else if (elevate_if_not_admin && - base::win::GetVersion() >= base::win::VERSION_VISTA && - ElevateAndRegisterChrome(dist, chrome_exe, suffix, L"")) { + base::win::GetVersion() >= base::win::VERSION_VISTA && + ElevateAndRegisterChrome(dist, chrome_exe, suffix, base::string16())) { // If the user is not an admin and OS is between Vista and Windows 7 // inclusively, try to elevate and register. This is only intended for // user-level installs as system-level installs should always be run with diff --git a/chrome/renderer/chrome_render_view_observer.cc b/chrome/renderer/chrome_render_view_observer.cc index 6bca112..8c32b5d 100644 --- a/chrome/renderer/chrome_render_view_observer.cc +++ b/chrome/renderer/chrome_render_view_observer.cc @@ -569,9 +569,8 @@ void ChromeRenderViewObserver::CaptureText(WebFrame* frame, // terminate the string at the last space to ensure no words are clipped. if (contents->size() == kMaxIndexChars) { size_t last_space_index = contents->find_last_of(base::kWhitespaceUTF16); - if (last_space_index == base::string16::npos) - return; // don't index if we got a huge block of text with no spaces - contents->resize(last_space_index); + if (last_space_index != base::string16::npos) + contents->resize(last_space_index); } } diff --git a/chromeos/system/name_value_pairs_parser.cc b/chromeos/system/name_value_pairs_parser.cc index 39b852b..211cce7 100644 --- a/chromeos/system/name_value_pairs_parser.cc +++ b/chromeos/system/name_value_pairs_parser.cc @@ -19,9 +19,6 @@ namespace system { namespace { -const char kQuoteChars[] = "\""; -const char kTrimChars[] = "\" "; - bool GetToolOutput(int argc, const char* argv[], std::string& output) { DCHECK_GE(argc, 1); @@ -72,7 +69,7 @@ bool NameValuePairsParser::ParseNameValuePairsWithComments( bool all_valid = true; // Set up the pair tokenizer. base::StringTokenizer pair_toks(in_string, delim); - pair_toks.set_quote_chars(kQuoteChars); + pair_toks.set_quote_chars("\""); // Process token pairs. while (pair_toks.GetNext()) { std::string pair(pair_toks.token()); @@ -90,6 +87,7 @@ bool NameValuePairsParser::ParseNameValuePairsWithComments( value_size = comment_pos - eq_pos - 1; } + static const char kTrimChars[] = "\" "; std::string key; std::string value; base::TrimString(pair.substr(0, eq_pos), kTrimChars, &key); diff --git a/components/autofill/core/browser/autofill_field.cc b/components/autofill/core/browser/autofill_field.cc index c15700a..bad3b5b 100644 --- a/components/autofill/core/browser/autofill_field.cc +++ b/components/autofill/core/browser/autofill_field.cc @@ -291,12 +291,9 @@ void FillStreetAddress(const base::string16& value, return; } - base::string16 one_line_value; - const base::char16 kNewline[] = { '\n', 0 }; - const base::string16 separator = + const base::string16& separator = l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_LINE_SEPARATOR); - base::ReplaceChars(value, kNewline, separator, &one_line_value); - field->value = one_line_value; + base::ReplaceChars(value, base::ASCIIToUTF16("\n"), separator, &field->value); } std::string Hash32Bit(const std::string& str) { diff --git a/components/autofill/core/browser/autofill_profile.cc b/components/autofill/core/browser/autofill_profile.cc index b37acd5..fc3f39e 100644 --- a/components/autofill/core/browser/autofill_profile.cc +++ b/components/autofill/core/browser/autofill_profile.cc @@ -707,10 +707,9 @@ base::string16 AutofillProfile::ConstructInferredLabel( } // Flatten the label if need be. - const base::char16 kNewline[] = { '\n', 0 }; - const base::string16 newline_separator = + const base::string16& line_separator = l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_LINE_SEPARATOR); - base::ReplaceChars(label, kNewline, newline_separator, &label); + base::ReplaceChars(label, base::ASCIIToUTF16("\n"), line_separator, &label); return label; } diff --git a/components/autofill/core/browser/credit_card.cc b/components/autofill/core/browser/credit_card.cc index 10090c5..bc9cebf 100644 --- a/components/autofill/core/browser/credit_card.cc +++ b/components/autofill/core/browser/credit_card.cc @@ -133,9 +133,8 @@ CreditCard::~CreditCard() {} // static const base::string16 CreditCard::StripSeparators(const base::string16& number) { - const base::char16 kSeparators[] = {'-', ' ', '\0'}; base::string16 stripped; - base::RemoveChars(number, kSeparators, &stripped); + base::RemoveChars(number, base::ASCIIToUTF16("- "), &stripped); return stripped; } diff --git a/components/autofill/core/browser/phone_number.cc b/components/autofill/core/browser/phone_number.cc index 57afcc9..66971ae 100644 --- a/components/autofill/core/browser/phone_number.cc +++ b/components/autofill/core/browser/phone_number.cc @@ -17,12 +17,6 @@ namespace autofill { namespace { -const base::char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 }; - -void StripPunctuation(base::string16* number) { - base::RemoveChars(*number, kPhoneNumberSeparators, number); -} - // Returns the region code for this phone number, which is an ISO 3166 2-letter // country code. The returned value is based on the |profile|; if the |profile| // does not have a country code associated with it, falls back to the country @@ -150,7 +144,7 @@ void PhoneNumber::GetMatchingTypes(const base::string16& text, const std::string& app_locale, ServerFieldTypeSet* matching_types) const { base::string16 stripped_text = text; - StripPunctuation(&stripped_text); + base::RemoveChars(stripped_text, base::ASCIIToUTF16(" .()-"), &stripped_text); FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); // For US numbers, also compare to the three-digit prefix and the four-digit diff --git a/components/autofill/core/browser/validation.cc b/components/autofill/core/browser/validation.cc index 62509cb..c1b11d9 100644 --- a/components/autofill/core/browser/validation.cc +++ b/components/autofill/core/browser/validation.cc @@ -13,14 +13,6 @@ #include "components/autofill/core/browser/credit_card.h" #include "components/autofill/core/browser/state_names.h" -using base::StringPiece16; - -namespace { - -// The separator characters for SSNs. -const base::char16 kSSNSeparators[] = {' ', '-', 0}; - -} // namespace namespace autofill { @@ -156,7 +148,7 @@ bool IsValidZip(const base::string16& text) { bool IsSSN(const base::string16& text) { base::string16 number_string; - base::RemoveChars(text, kSSNSeparators, &number_string); + base::RemoveChars(text, base::ASCIIToUTF16("- "), &number_string); // A SSN is of the form AAA-GG-SSSS (A = area number, G = group number, S = // serial number). The validation we do here is simply checking if the area, @@ -186,8 +178,8 @@ bool IsSSN(const base::string16& text) { return false; int area; - if (!base::StringToInt(StringPiece16(number_string.begin(), - number_string.begin() + 3), + if (!base::StringToInt(base::StringPiece16(number_string.begin(), + number_string.begin() + 3), &area)) { return false; } @@ -198,16 +190,16 @@ bool IsSSN(const base::string16& text) { } int group; - if (!base::StringToInt(StringPiece16(number_string.begin() + 3, - number_string.begin() + 5), + if (!base::StringToInt(base::StringPiece16(number_string.begin() + 3, + number_string.begin() + 5), &group) || group == 0) { return false; } int serial; - if (!base::StringToInt(StringPiece16(number_string.begin() + 5, - number_string.begin() + 9), + if (!base::StringToInt(base::StringPiece16(number_string.begin() + 5, + number_string.begin() + 9), &serial) || serial == 0) { return false; diff --git a/components/cloud_devices/common/cloud_devices_urls.cc b/components/cloud_devices/common/cloud_devices_urls.cc index 33f6acc..9e1090e 100644 --- a/components/cloud_devices/common/cloud_devices_urls.cc +++ b/components/cloud_devices/common/cloud_devices_urls.cc @@ -33,6 +33,7 @@ namespace { const char kCloudPrintURL[] = "https://www.google.com/cloudprint"; const char kCloudDevicesUrl[] = "https://www.googleapis.com/clouddevices/v1"; + } // Returns the root service URL for the cloud print service. The default is to @@ -50,7 +51,7 @@ GURL GetCloudPrintURL() { GURL GetCloudPrintRelativeURL(const std::string& relative_path) { GURL url = GetCloudPrintURL(); std::string path; - const char kURLPathSeparator[] = "/"; + static const char kURLPathSeparator[] = "/"; base::TrimString(url.path(), kURLPathSeparator, &path); std::string trimmed_path; base::TrimString(relative_path, kURLPathSeparator, &trimmed_path); diff --git a/components/webdata/common/web_database_migration_unittest.cc b/components/webdata/common/web_database_migration_unittest.cc index b0925bc..aa3d0dd 100644 --- a/components/webdata/common/web_database_migration_unittest.cc +++ b/components/webdata/common/web_database_migration_unittest.cc @@ -157,10 +157,9 @@ void CheckNoBackupData(const sql::Connection& connection, } std::string RemoveQuotes(const std::string& has_quotes) { - // SQLite quotes: http://www.sqlite.org/lang_keywords.html - static const char kQuotes[] = "\"[]`"; std::string no_quotes; - base::RemoveChars(has_quotes, kQuotes, &no_quotes); + // SQLite quotes: http://www.sqlite.org/lang_keywords.html + base::RemoveChars(has_quotes, "\"[]`", &no_quotes); return no_quotes; } diff --git a/extensions/common/permissions/socket_permission_entry.cc b/extensions/common/permissions/socket_permission_entry.cc index ddadf22..ba409c5 100644 --- a/extensions/common/permissions/socket_permission_entry.cc +++ b/extensions/common/permissions/socket_permission_entry.cc @@ -28,11 +28,8 @@ const int kWildcardPortNumber = 0; const int kInvalidPort = -1; bool StartsOrEndsWithWhitespace(const std::string& str) { - if (str.find_first_not_of(base::kWhitespaceASCII) != 0) - return true; - if (str.find_last_not_of(base::kWhitespaceASCII) != str.length() - 1) - return true; - return false; + return !str.empty() && + (IsWhitespace(str[0]) || IsWhitespace(str[str.length() - 1])); } } // namespace |