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

Side by Side Diff: app/l10n_util.cc

Issue 5816001: Minor changes to simplify IsValidLocaleSyntax logic. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "app/l10n_util.h" 5 #include "app/l10n_util.h"
6 6
7 #if defined(TOOLKIT_USES_GTK) 7 #if defined(TOOLKIT_USES_GTK)
8 #include <glib/gutils.h> 8 #include <glib/gutils.h>
9 #endif 9 #endif
10 10
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 } 500 }
501 501
502 bool IsValidLocaleSyntax(const std::string& locale) { 502 bool IsValidLocaleSyntax(const std::string& locale) {
503 // Check that the length is plausible. 503 // Check that the length is plausible.
504 if (locale.size() < 2 || locale.size() >= ULOC_FULLNAME_CAPACITY) 504 if (locale.size() < 2 || locale.size() >= ULOC_FULLNAME_CAPACITY)
505 return false; 505 return false;
506 506
507 // Strip off the part after an '@' sign, which might contain keywords, 507 // Strip off the part after an '@' sign, which might contain keywords,
508 // as in en_IE@currency=IEP or fr@collation=phonebook;calendar=islamic-civil. 508 // as in en_IE@currency=IEP or fr@collation=phonebook;calendar=islamic-civil.
509 // We don't validate that part much, just check that there's at least one 509 // We don't validate that part much, just check that there's at least one
510 // equals sign in a plausible place. 510 // equals sign in a plausible place. Normalize the prefix so that hyphens
511 std::string prefix = locale; 511 // are changed to underscores.
512 if (locale.find("@") != std::string::npos) { 512 std::string prefix = NormalizeLocale(locale);
513 size_t split_point = locale.find("@"); 513 size_t split_point = locale.find("@");
514 if (split_point != std::string::npos) {
514 std::string keywords = locale.substr(split_point + 1); 515 std::string keywords = locale.substr(split_point + 1);
515 prefix = locale.substr(0, split_point); 516 prefix = locale.substr(0, split_point);
516 517
517 size_t equals_loc = keywords.find("="); 518 size_t equals_loc = keywords.find("=");
518 if (equals_loc == std::string::npos || 519 if (equals_loc == std::string::npos ||
519 equals_loc < 1 || equals_loc > keywords.size() - 2) 520 equals_loc < 1 || equals_loc > keywords.size() - 2)
520 return false; 521 return false;
521 } 522 }
522 523
523 // Check that all characters before the at-sign are alphanumeric, hyphen, 524 // Check that all characters before the at-sign are alphanumeric or
524 // or underscore. 525 // underscore.
525 for (size_t i = 0; i < prefix.size(); i++) { 526 for (size_t i = 0; i < prefix.size(); i++) {
526 char ch = prefix[i]; 527 char ch = prefix[i];
527 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '-' && ch != '_') 528 if (!IsAsciiAlpha(ch) && !IsAsciiDigit(ch) && ch != '_')
528 return false; 529 return false;
529 } 530 }
530 531
531 // Check that the initial token (before the first hyphen/underscore) 532 // Check that the initial token (before the first hyphen/underscore)
532 // is 1 - 3 alphabetical characters (a language tag). 533 // is 1 - 3 alphabetical characters (a language tag).
533 for (size_t i = 0; i < prefix.size(); i++) { 534 for (size_t i = 0; i < prefix.size(); i++) {
534 char ch = prefix[i]; 535 char ch = prefix[i];
535 if (ch == '-' || ch == '_') { 536 if (ch == '_') {
536 if (i < 1 || i > 3) 537 if (i < 1 || i > 3)
537 return false; 538 return false;
538 break; 539 break;
539 } 540 }
540 if (!IsAsciiAlpha(ch)) 541 if (!IsAsciiAlpha(ch))
541 return false; 542 return false;
542 } 543 }
543 544
544 // Check that the all tokens after the initial token are 1 - 8 characters. 545 // Check that the all tokens after the initial token are 1 - 8 characters.
545 // (Tokenize/StringTokenizer don't work here, they collapse multiple 546 // (Tokenize/StringTokenizer don't work here, they collapse multiple
546 // delimiters into one.) 547 // delimiters into one.)
547 int token_len = 0; 548 int token_len = 0;
548 int token_index = 0; 549 int token_index = 0;
549 for (size_t i = 0; i < prefix.size(); i++) { 550 for (size_t i = 0; i < prefix.size(); i++) {
550 char ch = prefix[i]; 551 char ch = prefix[i];
551 if (ch == '-' || ch == '_') { 552 if (ch == '_') {
Nebojša Ćirić 2010/12/13 19:43:48 You don't use ch anywhere else. Remove it and use
552 if (token_index > 0 && (token_len < 1 || token_len > 8)) { 553 if (token_index > 0 && (token_len < 1 || token_len > 8)) {
553 return false; 554 return false;
554 } 555 }
555 token_index++; 556 token_index++;
556 token_len = 0; 557 token_len = 0;
557 } else { 558 } else {
558 token_len++; 559 token_len++;
559 } 560 }
560 } 561 }
561 if (token_index == 0 && (token_len < 1 || token_len > 3)) { 562 if (token_index == 0 && (token_len < 1 || token_len > 3)) {
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
959 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { 960 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) {
960 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) 961 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale))
961 // TODO(jungshik) : Put them at the of the list with language codes 962 // TODO(jungshik) : Put them at the of the list with language codes
962 // enclosed by brackets instead of skipping. 963 // enclosed by brackets instead of skipping.
963 continue; 964 continue;
964 locale_codes->push_back(kAcceptLanguageList[i]); 965 locale_codes->push_back(kAcceptLanguageList[i]);
965 } 966 }
966 } 967 }
967 968
968 } // namespace l10n_util 969 } // namespace l10n_util
OLDNEW
« 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