Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: ui/gfx/utf16_indexing.cc

Issue 100303003: Move more uses of string16 to specify base:: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/utf16_indexing.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/gfx/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 gfx { 10 namespace gfx {
11 11
12 bool IsValidCodePointIndex(const string16& s, size_t index) { 12 bool IsValidCodePointIndex(const base::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 base::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
21 // the second. The test "s[i] is not the second half of a surrogate pair" is 21 // the second. The test "s[i] is not the second half of a surrogate pair" is
22 // "IsValidCodePointIndex(s, i)". 22 // "IsValidCodePointIndex(s, i)".
23 DCHECK_LE(base, s.length()); 23 DCHECK_LE(base, s.length());
24 DCHECK_LE(pos, s.length()); 24 DCHECK_LE(pos, s.length());
25 ptrdiff_t delta = 0; 25 ptrdiff_t delta = 0;
26 while (base < pos) 26 while (base < pos)
27 delta += IsValidCodePointIndex(s, base++) ? 1 : 0; 27 delta += IsValidCodePointIndex(s, base++) ? 1 : 0;
28 while (pos < base) 28 while (pos < base)
29 delta -= IsValidCodePointIndex(s, pos++) ? 1 : 0; 29 delta -= IsValidCodePointIndex(s, pos++) ? 1 : 0;
30 return delta; 30 return delta;
31 } 31 }
32 32
33 size_t UTF16OffsetToIndex(const string16& s, size_t base, ptrdiff_t offset) { 33 size_t UTF16OffsetToIndex(const base::string16& s,
34 size_t base,
35 ptrdiff_t offset) {
34 DCHECK_LE(base, s.length()); 36 DCHECK_LE(base, s.length());
35 // As in UTF16IndexToOffset, we count the first half of a surrogate pair, not 37 // As in UTF16IndexToOffset, we count the first half of a surrogate pair, not
36 // the second. When stepping from pos to pos+1 we check s[pos:pos+1] == s[pos] 38 // the second. When stepping from pos to pos+1 we check s[pos:pos+1] == s[pos]
37 // (Python syntax), hence pos++. When stepping from pos to pos-1 we check 39 // (Python syntax), hence pos++. When stepping from pos to pos-1 we check
38 // s[pos-1], hence --pos. 40 // s[pos-1], hence --pos.
39 size_t pos = base; 41 size_t pos = base;
40 while (offset > 0 && pos < s.length()) 42 while (offset > 0 && pos < s.length())
41 offset -= IsValidCodePointIndex(s, pos++) ? 1 : 0; 43 offset -= IsValidCodePointIndex(s, pos++) ? 1 : 0;
42 while (offset < 0 && pos > 0) 44 while (offset < 0 && pos > 0)
43 offset += IsValidCodePointIndex(s, --pos) ? 1 : 0; 45 offset += IsValidCodePointIndex(s, --pos) ? 1 : 0;
44 // If offset != 0 then we ran off the edge of the string, which is a contract 46 // If offset != 0 then we ran off the edge of the string, which is a contract
45 // violation but is handled anyway (by clamping) in release for safety. 47 // violation but is handled anyway (by clamping) in release for safety.
46 DCHECK_EQ(offset, 0); 48 DCHECK_EQ(offset, 0);
47 // Since the second half of a surrogate pair has "length" zero, there is an 49 // 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 50 // ambiguity in the returned position. Resolve it by always returning a valid
49 // index. 51 // index.
50 if (!IsValidCodePointIndex(s, pos)) 52 if (!IsValidCodePointIndex(s, pos))
51 ++pos; 53 ++pos;
52 return pos; 54 return pos;
53 } 55 }
54 56
55 } // namespace gfx 57 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/utf16_indexing.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698