OLD | NEW |
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 "base/i18n/rtl.h" | 5 #include "base/i18n/rtl.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 156 |
157 return LEFT_TO_RIGHT; | 157 return LEFT_TO_RIGHT; |
158 } | 158 } |
159 | 159 |
160 #if defined(WCHAR_T_IS_UTF32) | 160 #if defined(WCHAR_T_IS_UTF32) |
161 TextDirection GetFirstStrongCharacterDirection(const std::wstring& text) { | 161 TextDirection GetFirstStrongCharacterDirection(const std::wstring& text) { |
162 return GetFirstStrongCharacterDirection(WideToUTF16(text)); | 162 return GetFirstStrongCharacterDirection(WideToUTF16(text)); |
163 } | 163 } |
164 #endif | 164 #endif |
165 | 165 |
166 bool AdjustStringForLocaleDirection(const string16& text, | 166 bool AdjustStringForLocaleDirection(string16* text) { |
167 string16* localized_text) { | 167 if (!IsRTL() || text->empty()) |
168 if (!IsRTL() || text.empty()) | |
169 return false; | 168 return false; |
170 | 169 |
171 // Marking the string as LTR if the locale is RTL and the string does not | 170 // Marking the string as LTR if the locale is RTL and the string does not |
172 // contain strong RTL characters. Otherwise, mark the string as RTL. | 171 // contain strong RTL characters. Otherwise, mark the string as RTL. |
173 *localized_text = text; | 172 bool has_rtl_chars = StringContainsStrongRTLChars(*text); |
174 bool has_rtl_chars = StringContainsStrongRTLChars(text); | |
175 if (!has_rtl_chars) | 173 if (!has_rtl_chars) |
176 WrapStringWithLTRFormatting(localized_text); | 174 WrapStringWithLTRFormatting(text); |
177 else | 175 else |
178 WrapStringWithRTLFormatting(localized_text); | 176 WrapStringWithRTLFormatting(text); |
179 | 177 |
180 return true; | 178 return true; |
181 } | 179 } |
182 | 180 |
183 #if defined(WCHAR_T_IS_UTF32) | 181 #if defined(WCHAR_T_IS_UTF32) |
184 bool AdjustStringForLocaleDirection(const std::wstring& text, | 182 bool AdjustStringForLocaleDirection(std::wstring* text) { |
185 std::wstring* localized_text) { | 183 string16 temp = WideToUTF16(*text); |
186 string16 out; | 184 if (AdjustStringForLocaleDirection(&temp)) { |
187 if (AdjustStringForLocaleDirection(WideToUTF16(text), &out)) { | |
188 // We should only touch the output on success. | 185 // We should only touch the output on success. |
189 *localized_text = UTF16ToWide(out); | 186 *text = UTF16ToWide(temp); |
190 return true; | 187 return true; |
191 } | 188 } |
192 return false; | 189 return false; |
193 } | 190 } |
194 #endif | 191 #endif |
195 | 192 |
196 bool StringContainsStrongRTLChars(const string16& text) { | 193 bool StringContainsStrongRTLChars(const string16& text) { |
197 const UChar* string = text.c_str(); | 194 const UChar* string = text.c_str(); |
198 size_t length = text.length(); | 195 size_t length = text.length(); |
199 size_t position = 0; | 196 size_t position = 0; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 begin == kRightToLeftOverride) | 302 begin == kRightToLeftOverride) |
306 ++begin_index; | 303 ++begin_index; |
307 size_t end_index = text.length() - 1; | 304 size_t end_index = text.length() - 1; |
308 if (text[end_index] == kPopDirectionalFormatting) | 305 if (text[end_index] == kPopDirectionalFormatting) |
309 --end_index; | 306 --end_index; |
310 return text.substr(begin_index, end_index - begin_index + 1); | 307 return text.substr(begin_index, end_index - begin_index + 1); |
311 } | 308 } |
312 | 309 |
313 } // namespace i18n | 310 } // namespace i18n |
314 } // namespace base | 311 } // namespace base |
OLD | NEW |