OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/base/text/utf16_indexing.h" | 5 #include "ui/gfx/utf16_indexing.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/third_party/icu/icu_utf.h" | 8 #include "base/third_party/icu/icu_utf.h" |
9 | 9 |
10 namespace ui { | 10 namespace gfx { |
11 | 11 |
12 bool IsValidCodePointIndex(const string16& s, size_t index) { | 12 bool IsValidCodePointIndex(const string16& s, size_t index) { |
13 return index == 0 || index == s.length() || | 13 return index == 0 || index == s.length() || |
14 !(CBU16_IS_TRAIL(s[index]) && CBU16_IS_LEAD(s[index - 1])); | 14 !(CBU16_IS_TRAIL(s[index]) && CBU16_IS_LEAD(s[index - 1])); |
15 } | 15 } |
16 | 16 |
17 ptrdiff_t UTF16IndexToOffset(const string16& s, size_t base, size_t pos) { | 17 ptrdiff_t UTF16IndexToOffset(const string16& s, size_t base, size_t pos) { |
18 // The indices point between UTF-16 words (range 0 to s.length() inclusive). | 18 // The indices point between UTF-16 words (range 0 to s.length() inclusive). |
19 // In order to consistently handle indices that point to the middle of a | 19 // In order to consistently handle indices that point to the middle of a |
20 // surrogate pair, we count the first word in that surrogate pair and not | 20 // surrogate pair, we count the first word in that surrogate pair and not |
(...skipping 24 matching lines...) Expand all Loading... |
45 // violation but is handled anyway (by clamping) in release for safety. | 45 // violation but is handled anyway (by clamping) in release for safety. |
46 DCHECK_EQ(offset, 0); | 46 DCHECK_EQ(offset, 0); |
47 // Since the second half of a surrogate pair has "length" zero, there is an | 47 // Since the second half of a surrogate pair has "length" zero, there is an |
48 // ambiguity in the returned position. Resolve it by always returning a valid | 48 // ambiguity in the returned position. Resolve it by always returning a valid |
49 // index. | 49 // index. |
50 if (!IsValidCodePointIndex(s, pos)) | 50 if (!IsValidCodePointIndex(s, pos)) |
51 ++pos; | 51 ++pos; |
52 return pos; | 52 return pos; |
53 } | 53 } |
54 | 54 |
55 } // namespace ui | 55 } // namespace gfx |
OLD | NEW |