 Chromium Code Reviews
 Chromium Code Reviews| Index: ui/gfx/text_utils.cc | 
| diff --git a/ui/gfx/text_utils.cc b/ui/gfx/text_utils.cc | 
| index a31ef3d3cc999cd844fb4042b17628b9db976bcf..d9ef200ac148d163f83700e78764eae99072a980 100644 | 
| --- a/ui/gfx/text_utils.cc | 
| +++ b/ui/gfx/text_utils.cc | 
| @@ -5,9 +5,32 @@ | 
| #include "ui/gfx/text_utils.h" | 
| #include "base/i18n/char_iterator.h" | 
| +#include "base/logging.h" | 
| +#include "third_party/icu/source/common/unicode/uchar.h" | 
| +#include "third_party/icu/source/common/unicode/utf16.h" | 
| namespace gfx { | 
| +namespace { | 
| + | 
| +// Returns true if the code point |c| is a combining mark character in Unicode. | 
| +bool CharIsMark(UChar32 c) { | 
| + int8_t char_type = u_charType(c); | 
| + return char_type == U_NON_SPACING_MARK || char_type == U_ENCLOSING_MARK || | 
| + char_type == U_COMBINING_SPACING_MARK; | 
| +} | 
| + | 
| +// Gets the code point of |str| at the given code unit position |index|. If | 
| +// |index| is a surrogate code unit, returns the whole code point (unless the | 
| +// code unit is unpaired, in which case it just returns the surrogate value). | 
| +UChar32 GetCodePointAt(const base::string16& str, size_t index) { | 
| + UChar32 c; | 
| + U16_GET(str.data(), 0, index, str.size(), c); | 
| + return c; | 
| +} | 
| + | 
| +} // namespace | 
| + | 
| base::string16 RemoveAcceleratorChar(const base::string16& s, | 
| base::char16 accelerator_char, | 
| int* accelerated_char_pos, | 
| @@ -46,4 +69,37 @@ base::string16 RemoveAcceleratorChar(const base::string16& s, | 
| return accelerator_removed; | 
| } | 
| +size_t FindValidBoundaryBefore(const base::string16& text, size_t index) { | 
| + size_t length = text.length(); | 
| + DCHECK_LE(index, length); | 
| + if (index == length) | 
| + return index; | 
| + | 
| + // If |index| straddles a combining character sequence, go back until we find | 
| + // a base character. | 
| + while (index > 0 && CharIsMark(GetCodePointAt(text, index))) | 
| + --index; | 
| + | 
| + // If |index| straddles a UTF-16 surrogate pair, go back. | 
| + U16_SET_CP_START(text.data(), 0, index); | 
| + return index; | 
| +} | 
| + | 
| +size_t FindValidBoundaryAfter(const base::string16& text, size_t index) { | 
| 
msw
2015/06/03 00:32:04
Why did you drop the checked_casts here? Is https:
 
xdai1
2015/06/06 00:05:19
Sorry my bad, I should not have done that. I saw F
 | 
| + size_t length = text.length(); | 
| + DCHECK_LE(index, length); | 
| + if (index == text.length()) | 
| + return index; | 
| + | 
| + // If |index| straddles a combining character sequence, go forward until we | 
| + // find a base character. | 
| + while (index < length && CharIsMark(GetCodePointAt(text, index))) { | 
| + ++index; | 
| + } | 
| 
msw
2015/06/03 00:32:04
nit: drop curly braces
 
xdai1
2015/06/06 00:05:19
After bring back the checked_casts, it still requi
 | 
| + | 
| + // If |index| straddles a UTF-16 surrogate pair, go forward. | 
| + U16_SET_CP_LIMIT(text.data(), 0, index, length); | 
| + return index; | 
| +} | 
| + | 
| } // namespace gfx |