Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 // box so there is no issue with displaying zero-width bidi control characters | 208 // box so there is no issue with displaying zero-width bidi control characters |
| 209 // on any system. Thus no need for the !IsRTL() check here. | 209 // on any system. Thus no need for the !IsRTL() check here. |
| 210 if (text->empty()) | 210 if (text->empty()) |
| 211 return false; | 211 return false; |
| 212 | 212 |
| 213 bool ui_direction_is_rtl = IsRTL(); | 213 bool ui_direction_is_rtl = IsRTL(); |
| 214 | 214 |
| 215 bool has_rtl_chars = StringContainsStrongRTLChars(*text); | 215 bool has_rtl_chars = StringContainsStrongRTLChars(*text); |
| 216 if (!ui_direction_is_rtl && has_rtl_chars) { | 216 if (!ui_direction_is_rtl && has_rtl_chars) { |
| 217 WrapStringWithRTLFormatting(text); | 217 WrapStringWithRTLFormatting(text); |
| 218 text->insert(0, 1, kLeftToRightMark); | 218 text->insert(0U, 1U, kLeftToRightMark); |
| 219 text->push_back(kLeftToRightMark); | 219 text->push_back(kLeftToRightMark); |
| 220 } else if (ui_direction_is_rtl && has_rtl_chars) { | 220 } else if (ui_direction_is_rtl && has_rtl_chars) { |
| 221 WrapStringWithRTLFormatting(text); | 221 WrapStringWithRTLFormatting(text); |
| 222 text->insert(0, 1, kRightToLeftMark); | 222 text->insert(0U, 1U, kRightToLeftMark); |
| 223 text->push_back(kRightToLeftMark); | 223 text->push_back(kRightToLeftMark); |
| 224 } else if (ui_direction_is_rtl) { | 224 } else if (ui_direction_is_rtl) { |
| 225 WrapStringWithLTRFormatting(text); | 225 WrapStringWithLTRFormatting(text); |
| 226 text->insert(0, 1, kRightToLeftMark); | 226 text->insert(0U, 1U, kRightToLeftMark); |
| 227 text->push_back(kRightToLeftMark); | 227 text->push_back(kRightToLeftMark); |
| 228 } | 228 } |
| 229 | 229 |
| 230 return true; | 230 return true; |
| 231 } | 231 } |
| 232 | 232 |
| 233 #endif // !OS_WIN | 233 #endif // !OS_WIN |
| 234 | 234 |
| 235 bool StringContainsStrongRTLChars(const string16& text) { | 235 bool StringContainsStrongRTLChars(const string16& text) { |
| 236 const UChar* string = text.c_str(); | 236 const UChar* string = text.c_str(); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 251 } | 251 } |
| 252 | 252 |
| 253 return false; | 253 return false; |
| 254 } | 254 } |
| 255 | 255 |
| 256 void WrapStringWithLTRFormatting(string16* text) { | 256 void WrapStringWithLTRFormatting(string16* text) { |
| 257 if (text->empty()) | 257 if (text->empty()) |
| 258 return; | 258 return; |
| 259 | 259 |
| 260 // Inserting an LRE (Left-To-Right Embedding) mark as the first character. | 260 // Inserting an LRE (Left-To-Right Embedding) mark as the first character. |
| 261 text->insert(0, 1, kLeftToRightEmbeddingMark); | 261 text->insert(static_cast<std::wstring::size_type>(0), 1, |
|
brettw
2011/08/26 19:28:52
Why use static_cast here and 0U above? I'd use 0u
michaelbai
2011/08/26 22:01:50
Right, Done
| |
| 262 kLeftToRightEmbeddingMark); | |
| 262 | 263 |
| 263 // Inserting a PDF (Pop Directional Formatting) mark as the last character. | 264 // Inserting a PDF (Pop Directional Formatting) mark as the last character. |
| 264 text->push_back(kPopDirectionalFormatting); | 265 text->push_back(kPopDirectionalFormatting); |
| 265 } | 266 } |
| 266 | 267 |
| 267 void WrapStringWithRTLFormatting(string16* text) { | 268 void WrapStringWithRTLFormatting(string16* text) { |
| 268 if (text->empty()) | 269 if (text->empty()) |
| 269 return; | 270 return; |
| 270 | 271 |
| 271 // Inserting an RLE (Right-To-Left Embedding) mark as the first character. | 272 // Inserting an RLE (Right-To-Left Embedding) mark as the first character. |
| 272 text->insert(0, 1, kRightToLeftEmbeddingMark); | 273 text->insert(static_cast<std::wstring::size_type>(0), 1, |
| 274 kRightToLeftEmbeddingMark); | |
| 273 | 275 |
| 274 // Inserting a PDF (Pop Directional Formatting) mark as the last character. | 276 // Inserting a PDF (Pop Directional Formatting) mark as the last character. |
| 275 text->push_back(kPopDirectionalFormatting); | 277 text->push_back(kPopDirectionalFormatting); |
| 276 } | 278 } |
| 277 | 279 |
| 278 void WrapPathWithLTRFormatting(const FilePath& path, | 280 void WrapPathWithLTRFormatting(const FilePath& path, |
| 279 string16* rtl_safe_path) { | 281 string16* rtl_safe_path) { |
| 280 // Wrap the overall path with LRE-PDF pair which essentialy marks the | 282 // Wrap the overall path with LRE-PDF pair which essentialy marks the |
| 281 // string as a Left-To-Right string. | 283 // string as a Left-To-Right string. |
| 282 // Inserting an LRE (Left-To-Right Embedding) mark as the first character. | 284 // Inserting an LRE (Left-To-Right Embedding) mark as the first character. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 312 begin == kRightToLeftOverride) | 314 begin == kRightToLeftOverride) |
| 313 ++begin_index; | 315 ++begin_index; |
| 314 size_t end_index = text.length() - 1; | 316 size_t end_index = text.length() - 1; |
| 315 if (text[end_index] == kPopDirectionalFormatting) | 317 if (text[end_index] == kPopDirectionalFormatting) |
| 316 --end_index; | 318 --end_index; |
| 317 return text.substr(begin_index, end_index - begin_index + 1); | 319 return text.substr(begin_index, end_index - begin_index + 1); |
| 318 } | 320 } |
| 319 | 321 |
| 320 } // namespace i18n | 322 } // namespace i18n |
| 321 } // namespace base | 323 } // namespace base |
| OLD | NEW |