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

Unified Diff: components/search_engines/template_url_prepopulate_data.cc

Issue 2349213004: Clean up some code in template_url_prepopuplate_data.cc. (Closed)
Patch Set: Oops Created 4 years, 3 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/search_engines/template_url_prepopulate_data.cc
diff --git a/components/search_engines/template_url_prepopulate_data.cc b/components/search_engines/template_url_prepopulate_data.cc
index 076dcd62657d762db0fe83031310f29c73930105..1aac969e1e227a416214df3b4ac958480019970e 100644
--- a/components/search_engines/template_url_prepopulate_data.cc
+++ b/components/search_engines/template_url_prepopulate_data.cc
@@ -536,6 +536,12 @@ int CountryCharsToCountryIDWithUpdate(char c1, char c2) {
return CountryCharsToCountryID(c1, c2);
}
+int CountryStringToCountryID(const std::string& country) {
+ return (country.length() == 2)
+ ? CountryCharsToCountryIDWithUpdate(country[0], country[1])
+ : kCountryIDUnknown;
+}
+
#if defined(OS_WIN)
// For reference, a list of GeoIDs can be found at
@@ -1200,9 +1206,7 @@ SearchEngineType GetEngineType(const GURL& url) {
#if defined(OS_WIN)
int GetCurrentCountryID() {
- GEOID geo_id = GetUserGeoID(GEOCLASS_NATION);
-
- return GeoIDToCountryID(geo_id);
+ return GeoIDToCountryID(GetUserGeoID(GEOCLASS_NATION));
}
#elif defined(OS_MACOSX)
@@ -1225,41 +1229,30 @@ int GetCurrentCountryID() {
#elif defined(OS_ANDROID)
int GetCurrentCountryID() {
- const std::string& country_code = base::android::GetDefaultCountryCode();
- return (country_code.size() == 2) ?
- CountryCharsToCountryIDWithUpdate(country_code[0], country_code[1]) :
- kCountryIDUnknown;
+ return CountryStringToCountryID(base::android::GetDefaultCountryCode());
}
#elif defined(OS_POSIX)
int GetCurrentCountryID() {
const char* locale = setlocale(LC_MESSAGES, NULL);
-
if (!locale)
return kCountryIDUnknown;
// The format of a locale name is:
// language[_territory][.codeset][@modifier], where territory is an ISO 3166
// country code, which is what we want.
+
+ // First remove the language portion.
std::string locale_str(locale);
- size_t begin = locale_str.find('_');
- if (begin == std::string::npos || locale_str.size() - begin < 3)
+ size_t territory_delim = locale_str.find('_');
+ if (territory_delim == std::string::npos)
return kCountryIDUnknown;
+ locale_str.erase(0, territory_delim + 1);
- ++begin;
- size_t end = locale_str.find_first_of(".@", begin);
- if (end == std::string::npos)
- end = locale_str.size();
-
- // The territory part must contain exactly two characters.
- if (end - begin == 2) {
- return CountryCharsToCountryIDWithUpdate(
- base::ToUpperASCII(locale_str[begin]),
- base::ToUpperASCII(locale_str[begin + 1]));
- }
-
- return kCountryIDUnknown;
+ // Next remove any codeset/modifier portion and uppercase.
+ return CountryStringToCountryID(
+ base::ToUpperASCII(locale_str.substr(0, locale_str.find_first_of(".@"))));
}
#endif // OS_*
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698