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

Side by Side Diff: base/i18n/rtl.cc

Issue 1281343003: Optimize RTL check in ICU to avoid mmap access. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/i18n/rtl.h" 5 #include "base/i18n/rtl.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
13 #include "third_party/icu/source/common/unicode/locid.h" 12 #include "third_party/icu/source/common/unicode/locid.h"
14 #include "third_party/icu/source/common/unicode/uchar.h" 13 #include "third_party/icu/source/common/unicode/uchar.h"
15 #include "third_party/icu/source/common/unicode/uscript.h" 14 #include "third_party/icu/source/common/unicode/uscript.h"
16 #include "third_party/icu/source/i18n/unicode/coll.h" 15 #include "third_party/icu/source/i18n/unicode/coll.h"
17 16
18 namespace { 17 namespace {
19 18
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 g_icu_text_direction = UNKNOWN_DIRECTION; 118 g_icu_text_direction = UNKNOWN_DIRECTION;
120 } 119 }
121 120
122 bool IsRTL() { 121 bool IsRTL() {
123 return ICUIsRTL(); 122 return ICUIsRTL();
124 } 123 }
125 124
126 bool ICUIsRTL() { 125 bool ICUIsRTL() {
127 if (g_icu_text_direction == UNKNOWN_DIRECTION) { 126 if (g_icu_text_direction == UNKNOWN_DIRECTION) {
128 const icu::Locale& locale = icu::Locale::getDefault(); 127 const icu::Locale& locale = icu::Locale::getDefault();
129 g_icu_text_direction = GetTextDirectionForLocale(locale.getName()); 128 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName());
130 } 129 }
131 return g_icu_text_direction == RIGHT_TO_LEFT; 130 return g_icu_text_direction == RIGHT_TO_LEFT;
132 } 131 }
133 132
133 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) {
134 static const char* kRTLLocales[] = {
jungshik at Google 2015/08/11 19:23:13 Please, add a note that this list has to be update
danduong 2015/08/11 20:01:10 Done.
135 "ar",
136 "fa",
137 "he",
138 "iw",
jungshik at Google 2015/08/11 19:23:13 'iw' is a legacy name for 'he' and this API should
danduong 2015/08/11 20:01:10 Acknowledged. I'd rather leave this here to be def
139 "ur"
140 };
141 for (const char* rtl_locale : kRTLLocales) {
142 if (StartsWith(locale_name, rtl_locale, CompareCase::SENSITIVE))
143 return RIGHT_TO_LEFT;
144 }
145 return LEFT_TO_RIGHT;
146 }
147
134 TextDirection GetTextDirectionForLocale(const char* locale_name) { 148 TextDirection GetTextDirectionForLocale(const char* locale_name) {
135 const std::string group_name =
136 FieldTrialList::FindFullName("LightSpeed");
137 // StartsWith allows flexibility for this experiment to apply to multiple
138 // group names. To start, this will apply to AvoidMMapOnStartup.
139 if (StartsWith(group_name, "AvoidMMap", CompareCase::SENSITIVE)) {
140 static const char kEnglishLocale[] = "en_";
141 if (StartsWith(locale_name, kEnglishLocale, CompareCase::SENSITIVE))
142 return LEFT_TO_RIGHT;
143 }
144 UErrorCode status = U_ZERO_ERROR; 149 UErrorCode status = U_ZERO_ERROR;
145 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status); 150 ULayoutType layout_dir = uloc_getCharacterOrientation(locale_name, &status);
146 DCHECK(U_SUCCESS(status)); 151 DCHECK(U_SUCCESS(status));
147 // Treat anything other than RTL as LTR. 152 // Treat anything other than RTL as LTR.
148 return (layout_dir != ULOC_LAYOUT_RTL) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT; 153 return (layout_dir != ULOC_LAYOUT_RTL) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT;
149 } 154 }
150 155
151 TextDirection GetFirstStrongCharacterDirection(const string16& text) { 156 TextDirection GetFirstStrongCharacterDirection(const string16& text) {
152 const UChar* string = text.c_str(); 157 const UChar* string = text.c_str();
153 size_t length = text.length(); 158 size_t length = text.length();
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 begin == kRightToLeftOverride) 400 begin == kRightToLeftOverride)
396 ++begin_index; 401 ++begin_index;
397 size_t end_index = text.length() - 1; 402 size_t end_index = text.length() - 1;
398 if (text[end_index] == kPopDirectionalFormatting) 403 if (text[end_index] == kPopDirectionalFormatting)
399 --end_index; 404 --end_index;
400 return text.substr(begin_index, end_index - begin_index + 1); 405 return text.substr(begin_index, end_index - begin_index + 1);
401 } 406 }
402 407
403 } // namespace i18n 408 } // namespace i18n
404 } // namespace base 409 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698