| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "ui/gfx/text_utils.h" | 5 #include "ui/gfx/text_utils.h" |
| 6 | 6 |
| 7 #include "base/i18n/char_iterator.h" | 7 #include "base/i18n/char_iterator.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/numerics/safe_conversions.h" |
| 10 #include "third_party/icu/source/common/unicode/uchar.h" |
| 11 #include "third_party/icu/source/common/unicode/utf16.h" |
| 8 | 12 |
| 9 namespace gfx { | 13 namespace gfx { |
| 10 | 14 |
| 15 namespace { |
| 16 |
| 17 // Returns true if the code point |c| is a combining mark character in Unicode. |
| 18 bool CharIsMark(UChar32 c) { |
| 19 int8_t char_type = u_charType(c); |
| 20 return char_type == U_NON_SPACING_MARK || char_type == U_ENCLOSING_MARK || |
| 21 char_type == U_COMBINING_SPACING_MARK; |
| 22 } |
| 23 |
| 24 // Gets the code point of |str| at the given code unit position |index|. If |
| 25 // |index| is a surrogate code unit, returns the whole code point (unless the |
| 26 // code unit is unpaired, in which case it just returns the surrogate value). |
| 27 UChar32 GetCodePointAt(const base::string16& str, size_t index) { |
| 28 UChar32 c; |
| 29 U16_GET(str.data(), 0, index, str.size(), c); |
| 30 return c; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 11 base::string16 RemoveAcceleratorChar(const base::string16& s, | 35 base::string16 RemoveAcceleratorChar(const base::string16& s, |
| 12 base::char16 accelerator_char, | 36 base::char16 accelerator_char, |
| 13 int* accelerated_char_pos, | 37 int* accelerated_char_pos, |
| 14 int* accelerated_char_span) { | 38 int* accelerated_char_span) { |
| 15 bool escaped = false; | 39 bool escaped = false; |
| 16 ptrdiff_t last_char_pos = -1; | 40 ptrdiff_t last_char_pos = -1; |
| 17 int last_char_span = 0; | 41 int last_char_span = 0; |
| 18 base::i18n::UTF16CharIterator chars(&s); | 42 base::i18n::UTF16CharIterator chars(&s); |
| 19 base::string16 accelerator_removed; | 43 base::string16 accelerator_removed; |
| 20 | 44 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 39 } | 63 } |
| 40 | 64 |
| 41 if (accelerated_char_pos) | 65 if (accelerated_char_pos) |
| 42 *accelerated_char_pos = last_char_pos; | 66 *accelerated_char_pos = last_char_pos; |
| 43 if (accelerated_char_span) | 67 if (accelerated_char_span) |
| 44 *accelerated_char_span = last_char_span; | 68 *accelerated_char_span = last_char_span; |
| 45 | 69 |
| 46 return accelerator_removed; | 70 return accelerator_removed; |
| 47 } | 71 } |
| 48 | 72 |
| 73 size_t FindValidBoundaryBefore(const base::string16& text, size_t index) { |
| 74 size_t length = text.length(); |
| 75 DCHECK_LE(index, length); |
| 76 if (index == length) |
| 77 return index; |
| 78 |
| 79 // If |index| straddles a combining character sequence, go back until we find |
| 80 // a base character. |
| 81 while (index > 0 && CharIsMark(GetCodePointAt(text, index))) |
| 82 --index; |
| 83 |
| 84 // If |index| straddles a UTF-16 surrogate pair, go back. |
| 85 U16_SET_CP_START(text.data(), 0, index); |
| 86 return index; |
| 87 } |
| 88 |
| 89 size_t FindValidBoundaryAfter(const base::string16& text, size_t index) { |
| 90 DCHECK_LE(index, text.length()); |
| 91 if (index == text.length()) |
| 92 return index; |
| 93 |
| 94 int32_t text_index = base::checked_cast<int32_t>(index); |
| 95 int32_t text_length = base::checked_cast<int32_t>(text.length()); |
| 96 |
| 97 // If |index| straddles a combining character sequence, go forward until we |
| 98 // find a base character. |
| 99 while (text_index < text_length && |
| 100 CharIsMark(GetCodePointAt(text, text_index))) { |
| 101 ++text_index; |
| 102 } |
| 103 |
| 104 // If |index| straddles a UTF-16 surrogate pair, go forward. |
| 105 U16_SET_CP_LIMIT(text.data(), 0, text_index, text_length); |
| 106 return static_cast<size_t>(text_index); |
| 107 } |
| 108 |
| 49 } // namespace gfx | 109 } // namespace gfx |
| OLD | NEW |