Skip to content

Commit

Permalink
Replace StringToLowerASCII with base::ToLowerASCII
Browse files Browse the repository at this point in the history
Standardize on using string pieces and returning strings. Remove in-place version (this was only used in a couple places and they were not performance-critical).

De-templatize the character versions of ToUpperASCII/ToLowerASCII. This would lead to bizarre errors if you pass other things (like a string). This is so little code, it's now just duplicated.

I renamed StringToLowerASCII to just be ToLowerASCII so you can pass whatever you want to ToLowerASCII and it does the right thing. This seems simpler to me.

This replaces all calls of StringToUpperASCII to the new form. The lowercase version is more common and will be done in a separate pass.

Review URL: https://codereview.chromium.org/1280473002

Cr-Commit-Position: refs/heads/master@{#342219}
  • Loading branch information
brettw authored and Commit bot committed Aug 6, 2015
1 parent edfd250 commit c15100c
Show file tree
Hide file tree
Showing 27 changed files with 115 additions and 80 deletions.
4 changes: 2 additions & 2 deletions base/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class EnvironmentImpl : public Environment {
char first_char = variable_name[0];
std::string alternate_case_var;
if (first_char >= 'a' && first_char <= 'z')
alternate_case_var = StringToUpperASCII(std::string(variable_name));
alternate_case_var = ToUpperASCII(variable_name);
else if (first_char >= 'A' && first_char <= 'Z')
alternate_case_var = StringToLowerASCII(std::string(variable_name));
alternate_case_var = ToLowerASCII(variable_name);
else
return false;
return GetVarImpl(alternate_case_var.c_str(), result);
Expand Down
4 changes: 2 additions & 2 deletions base/files/file_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {

TEST_F(FileUtilTest, DevicePathToDriveLetter) {
// Get a drive letter.
std::wstring real_drive_letter = temp_dir_.path().value().substr(0, 2);
StringToUpperASCII(&real_drive_letter);
string16 real_drive_letter =
ToUpperASCII(temp_dir_.path().value().substr(0, 2));
if (!isalpha(real_drive_letter[0]) || ':' != real_drive_letter[1]) {
LOG(ERROR) << "Can't get a drive letter to test with.";
return;
Expand Down
4 changes: 2 additions & 2 deletions base/guid_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ TEST(GUIDTest, GUIDCorrectlyFormatted) {
for (int it = 0; it < kIterations; ++it) {
std::string guid = GenerateGUID();
EXPECT_TRUE(IsValidGUID(guid));
EXPECT_TRUE(IsValidGUID(StringToLowerASCII(guid)));
EXPECT_TRUE(IsValidGUID(StringToUpperASCII(guid)));
EXPECT_TRUE(IsValidGUID(ToLowerASCII(guid)));
EXPECT_TRUE(IsValidGUID(ToUpperASCII(guid)));
}
}

Expand Down
38 changes: 38 additions & 0 deletions base/strings/string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,44 @@ bool IsWprintfFormatPortable(const wchar_t* format) {
return true;
}

namespace {

template<typename StringType>
StringType ToLowerASCIIImpl(BasicStringPiece<StringType> str) {
StringType ret;
ret.reserve(str.size());
for (size_t i = 0; i < str.size(); i++)
ret.push_back(ToLowerASCII(str[i]));
return ret;
}

template<typename StringType>
StringType ToUpperASCIIImpl(BasicStringPiece<StringType> str) {
StringType ret;
ret.reserve(str.size());
for (size_t i = 0; i < str.size(); i++)
ret.push_back(ToUpperASCII(str[i]));
return ret;
}

} // namespace

std::string ToLowerASCII(StringPiece str) {
return ToLowerASCIIImpl<std::string>(str);
}

string16 ToLowerASCII(StringPiece16 str) {
return ToLowerASCIIImpl<string16>(str);
}

std::string ToUpperASCII(StringPiece str) {
return ToUpperASCIIImpl<std::string>(str);
}

string16 ToUpperASCII(StringPiece16 str) {
return ToUpperASCIIImpl<string16>(str);
}

template<class StringType>
int CompareCaseInsensitiveASCIIT(BasicStringPiece<StringType> a,
BasicStringPiece<StringType> b) {
Expand Down
34 changes: 18 additions & 16 deletions base/strings/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,30 @@ BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);

// ASCII-specific tolower. The standard library's tolower is locale sensitive,
// so we don't want to use it here.
template <class Char> inline Char ToLowerASCII(Char c) {
inline char ToLowerASCII(char c) {
return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
}
inline char16 ToLowerASCII(char16 c) {
return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
}

// ASCII-specific toupper. The standard library's toupper is locale sensitive,
// so we don't want to use it here.
template <class Char> inline Char ToUpperASCII(Char c) {
inline char ToUpperASCII(char c) {
return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
}
inline char16 ToUpperASCII(char16 c) {
return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
}

// Converts the given string to it's ASCII-lowercase equivalent.
BASE_EXPORT std::string ToLowerASCII(StringPiece str);
BASE_EXPORT string16 ToLowerASCII(StringPiece16 str);

// Converts the given string to it's ASCII-uppercase equivalent.
BASE_EXPORT std::string ToUpperASCII(StringPiece str);
BASE_EXPORT string16 ToUpperASCII(StringPiece16 str);

// Functor for case-insensitive ASCII comparisons for STL algorithms like
// std::search.
//
Expand Down Expand Up @@ -291,32 +305,20 @@ BASE_EXPORT bool IsStringASCII(const std::wstring& str);

// Converts the elements of the given string. This version uses a pointer to
// clearly differentiate it from the non-pointer variant.
// TODO(brettw) remove this. Callers should use base::ToLowerASCII above.
template <class str> inline void StringToLowerASCII(str* s) {
for (typename str::iterator i = s->begin(); i != s->end(); ++i)
*i = ToLowerASCII(*i);
}

// TODO(brettw) remove this. Callers should use base::ToLowerASCII above.
template <class str> inline str StringToLowerASCII(const str& s) {
// for std::string and std::wstring
str output(s);
StringToLowerASCII(&output);
return output;
}

// Converts the elements of the given string. This version uses a pointer to
// clearly differentiate it from the non-pointer variant.
template <class str> inline void StringToUpperASCII(str* s) {
for (typename str::iterator i = s->begin(); i != s->end(); ++i)
*i = ToUpperASCII(*i);
}

template <class str> inline str StringToUpperASCII(const str& s) {
// for std::string and std::wstring
str output(s);
StringToUpperASCII(&output);
return output;
}

// Compare the lower-case form of the given string against the given
// previously-lower-cased ASCII string (typically a constant).
BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece str,
Expand Down
36 changes: 18 additions & 18 deletions base/strings/string_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,30 +503,30 @@ TEST(StringUtilTest, ConvertASCII) {
EXPECT_EQ(0, string_with_nul.compare(narrow_with_nul));
}

TEST(StringUtilTest, ToLowerASCII) {
EXPECT_EQ('c', ToLowerASCII('C'));
EXPECT_EQ('c', ToLowerASCII('c'));
EXPECT_EQ('2', ToLowerASCII('2'));

EXPECT_EQ(static_cast<char16>('c'), ToLowerASCII(static_cast<char16>('C')));
EXPECT_EQ(static_cast<char16>('c'), ToLowerASCII(static_cast<char16>('c')));
EXPECT_EQ(static_cast<char16>('2'), ToLowerASCII(static_cast<char16>('2')));

EXPECT_EQ("cc2", ToLowerASCII("Cc2"));
EXPECT_EQ(ASCIIToUTF16("cc2"), ToLowerASCII(ASCIIToUTF16("Cc2")));
}

TEST(StringUtilTest, ToUpperASCII) {
EXPECT_EQ('C', ToUpperASCII('C'));
EXPECT_EQ('C', ToUpperASCII('c'));
EXPECT_EQ('2', ToUpperASCII('2'));

EXPECT_EQ(L'C', ToUpperASCII(L'C'));
EXPECT_EQ(L'C', ToUpperASCII(L'c'));
EXPECT_EQ(L'2', ToUpperASCII(L'2'));

std::string in_place_a("Cc2");
StringToUpperASCII(&in_place_a);
EXPECT_EQ("CC2", in_place_a);

std::wstring in_place_w(L"Cc2");
StringToUpperASCII(&in_place_w);
EXPECT_EQ(L"CC2", in_place_w);

std::string original_a("Cc2");
std::string upper_a = StringToUpperASCII(original_a);
EXPECT_EQ("CC2", upper_a);
EXPECT_EQ(static_cast<char16>('C'), ToUpperASCII(static_cast<char16>('C')));
EXPECT_EQ(static_cast<char16>('C'), ToUpperASCII(static_cast<char16>('c')));
EXPECT_EQ(static_cast<char16>('2'), ToUpperASCII(static_cast<char16>('2')));

std::wstring original_w(L"Cc2");
std::wstring upper_w = StringToUpperASCII(original_w);
EXPECT_EQ(L"CC2", upper_w);
EXPECT_EQ("CC2", ToUpperASCII("Cc2"));
EXPECT_EQ(ASCIIToUTF16("CC2"), ToUpperASCII(ASCIIToUTF16("Cc2")));
}

TEST(StringUtilTest, LowerCaseEqualsASCII) {
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chromeos/input_method/input_method_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ std::string InputMethodUtil::GetLocalizedDisplayName(
if (disp.find("__MSG_") == 0) {
const InputMethodNameMap* map = kInputMethodNameMap;
size_t map_size = arraysize(kInputMethodNameMap);
std::string name = base::StringToUpperASCII(disp);
std::string name = base::ToUpperASCII(disp);
const InputMethodNameMap map_key = {name.c_str(), 0};
const InputMethodNameMap* p =
std::lower_bound(map, map + map_size, map_key);
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/prerender/prerender_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ bool PrerenderManager::HasRecentlyBeenNavigatedTo(Origin origin,
bool PrerenderManager::IsValidHttpMethod(const std::string& method) {
// method has been canonicalized to upper case at this point so we can just
// compare them.
DCHECK_EQ(method, base::StringToUpperASCII(method));
DCHECK_EQ(method, base::ToUpperASCII(method));
for (size_t i = 0; i < arraysize(kValidHttpMethods); ++i) {
if (method.compare(kValidHttpMethods[i]) == 0)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ PushMessagingAppIdentifier PushMessagingAppIdentifier::FindByAppId(
DCHECK_EQ(kPushMessagingAppIdentifierPrefix, app_id.substr(0, kPrefixLength));
DCHECK_GE(app_id.size(), kPrefixLength + kGuidLength);
DCHECK_EQ(app_id.substr(app_id.size() - kGuidLength),
base::StringToUpperASCII(
app_id.substr(app_id.size() - kGuidLength)));
base::ToUpperASCII(app_id.substr(app_id.size() - kGuidLength)));

const base::DictionaryValue* map =
profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool FilterBuilder::AddPattern(const std::string& pattern, int site_id) {
}

void FilterBuilder::AddHostnameHash(const std::string& hash, int site_id) {
contents_->hash_site_map.insert(std::make_pair(base::StringToUpperASCII(hash),
contents_->hash_site_map.insert(std::make_pair(base::ToUpperASCII(hash),
site_id));
}

Expand Down
4 changes: 2 additions & 2 deletions chrome/installer/util/l10n_string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ std::wstring GetLocalizedEulaResource() {
return L"";

// The resource names are more or less the upcased language names.
std::wstring language(GetLanguageSelector().selected_translation());
base::string16 language(GetLanguageSelector().selected_translation());
std::replace(language.begin(), language.end(), L'-', L'_');
base::StringToUpperASCII(&language);
language = base::ToUpperASCII(language);

std::wstring resource(L"IDR_OEMPG_");
resource.append(language).append(L".HTML");
Expand Down
2 changes: 1 addition & 1 deletion components/autofill/core/browser/address.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ bool Address::SetInfo(const AutofillType& type,
return false;
}

country_code_ = base::StringToUpperASCII(base::UTF16ToASCII(value));
country_code_ = base::ToUpperASCII(base::UTF16ToASCII(value));
return true;
} else if (type.html_type() == HTML_TYPE_FULL_ADDRESS) {
// Parsing a full address is too hard.
Expand Down
6 changes: 2 additions & 4 deletions components/autofill/core/browser/autofill_country.cc
Original file line number Diff line number Diff line change
Expand Up @@ -937,10 +937,8 @@ CountryNames::~CountryNames() {
const std::string CountryNames::GetCountryCode(const base::string16& country,
const std::string& locale) {
// First, check common country names, including 2- and 3-letter country codes.
std::string country_utf8 = base::UTF16ToUTF8(
base::StringToUpperASCII(country));
std::map<std::string, std::string>::const_iterator result =
common_names_.find(country_utf8);
std::string country_utf8 = base::UTF16ToUTF8(base::ToUpperASCII(country));
const auto result = common_names_.find(country_utf8);
if (result != common_names_.end())
return result->second;

Expand Down
2 changes: 1 addition & 1 deletion components/autofill/core/browser/personal_data_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ std::string PersonalDataManager::MostCommonCountryCodeFromProfiles() const {
std::vector<std::string> country_codes;
AutofillCountry::GetAvailableCountries(&country_codes);
for (size_t i = 0; i < profiles.size(); ++i) {
std::string country_code = base::StringToUpperASCII(base::UTF16ToASCII(
std::string country_code = base::ToUpperASCII(base::UTF16ToASCII(
profiles[i]->GetRawInfo(ADDRESS_HOME_COUNTRY)));

if (std::find(country_codes.begin(), country_codes.end(), country_code) !=
Expand Down
2 changes: 1 addition & 1 deletion components/dom_distiller/core/viewer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ scoped_ptr<ViewerHandle> CreateViewRequest(
std::string entry_id =
url_utils::GetValueForKeyInUrlPathQuery(path, kEntryIdKey);
bool has_valid_entry_id = !entry_id.empty();
entry_id = base::StringToUpperASCII(entry_id);
entry_id = base::ToUpperASCII(entry_id);

std::string requested_url_str =
url_utils::GetValueForKeyInUrlPathQuery(path, kUrlKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ TEST_F(CloudPolicyValidatorTest, SuccessfulRunValidationWithNoDMTokens) {

TEST_F(CloudPolicyValidatorTest, UsernameCanonicalization) {
policy_.policy_data().set_username(
base::StringToUpperASCII(std::string(PolicyBuilder::kFakeUsername)));
base::ToUpperASCII(PolicyBuilder::kFakeUsername));
Validate(CheckStatus(CloudPolicyValidatorBase::VALIDATION_OK));
}

Expand Down
3 changes: 2 additions & 1 deletion components/test_runner/event_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,8 @@ void EventSender::KeyDown(const std::string& code_str,
code -= 'a' - 'A';
if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z')) {
domString.assign("Key");
domString.push_back(base::ToUpperASCII(code));
domString.push_back(
base::ToUpperASCII(static_cast<base::char16>(code)));
} else if (code >= '0' && code <= '9') {
domString.assign("Digit");
domString.push_back(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ void CorrectLanguageCodeTypo(std::string* code) {
// Change everything up to a dash to lower-case and everything after to upper.
size_t dash_index = code->find('-');
if (dash_index != std::string::npos) {
*code = base::StringToLowerASCII(code->substr(0, dash_index)) +
base::StringToUpperASCII(code->substr(dash_index));
*code = base::ToLowerASCII(code->substr(0, dash_index)) +
base::ToUpperASCII(code->substr(dash_index));
} else {
*code = base::StringToLowerASCII(*code);
*code = base::ToLowerASCII(*code);
}
}

Expand Down
9 changes: 5 additions & 4 deletions content/browser/renderer_host/input/web_input_event_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ void UpdateWindowsKeyCodeAndKeyIdentifier(blink::WebKeyboardEvent* event,
if (id) {
base::strlcpy(event->keyIdentifier, id, sizeof(event->keyIdentifier) - 1);
} else {
base::snprintf(event->keyIdentifier,
sizeof(event->keyIdentifier),
"U+%04X",
base::ToUpperASCII(static_cast<int>(windows_key_code)));
base::snprintf(
event->keyIdentifier,
sizeof(event->keyIdentifier),
"U+%04X",
base::ToUpperASCII(static_cast<base::char16>(windows_key_code)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion content/child/appcache/web_application_cache_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void WebApplicationCacheHostImpl::willStartMainResourceRequest(

std::string method = request.httpMethod().utf8();
is_get_method_ = (method == kHttpGETMethod);
DCHECK(method == base::StringToUpperASCII(method));
DCHECK(method == base::ToUpperASCII(method));

const WebApplicationCacheHostImpl* spawning_host_impl =
static_cast<const WebApplicationCacheHostImpl*>(spawning_host);
Expand Down
2 changes: 1 addition & 1 deletion content/renderer/android/phone_number_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ PhoneNumberDetector::PhoneNumberDetector()
// Region should be empty or an ISO 3166-1 alpha-2 country code.
PhoneNumberDetector::PhoneNumberDetector(const std::string& region)
: region_code_(region.empty() ? RegionCode::GetUnknown()
: base::StringToUpperASCII(region)) {
: base::ToUpperASCII(region)) {
}

PhoneNumberDetector::~PhoneNumberDetector() {
Expand Down
4 changes: 2 additions & 2 deletions extensions/renderer/runtime_custom_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ void RuntimeCustomBindings::GetExtensionViews(
// all views for the current extension.
int browser_window_id = args[0]->Int32Value();

std::string view_type_string = *v8::String::Utf8Value(args[1]);
base::StringToUpperASCII(&view_type_string);
std::string view_type_string =
base::ToUpperASCII(*v8::String::Utf8Value(args[1]));
// |view_type| == VIEW_TYPE_INVALID means getting any type of
// views.
ViewType view_type = VIEW_TYPE_INVALID;
Expand Down
5 changes: 2 additions & 3 deletions media/base/mime_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ static bool ParseH264CodecID(const std::string& codec_id,
return false;
}

std::string profile = base::StringToUpperASCII(codec_id.substr(5, 4));
std::string profile = base::ToUpperASCII(codec_id.substr(5, 4));
if (IsValidH264BaselineProfile(profile)) {
*codec = MimeUtil::H264_BASELINE;
} else if (profile == "4D40") {
Expand All @@ -546,8 +546,7 @@ static bool ParseH264CodecID(const std::string& codec_id,
return true;
}

*is_ambiguous =
!IsValidH264Level(base::StringToUpperASCII(codec_id.substr(9)));
*is_ambiguous = !IsValidH264Level(base::ToUpperASCII(codec_id.substr(9)));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion net/http/http_auth_handler_ntlm_portable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static void LM_Hash(const base::string16& password, uint8* hash) {
// Convert password to OEM character set. We'll just use the native
// filesystem charset.
std::string passbuf = base::SysWideToNativeMB(base::UTF16ToWide(password));
base::StringToUpperASCII(&passbuf);
passbuf = base::ToUpperASCII(passbuf);
passbuf.resize(14, '\0');

uint8 k1[8], k2[8];
Expand Down
Loading

0 comments on commit c15100c

Please sign in to comment.