summaryrefslogtreecommitdiffstats
path: root/ui/gfx/utf16_indexing.cc
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-12 20:55:53 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-09-12 20:55:53 +0000
commitbde885b4e8cf11db7ba2af6c70a6580830a52e7a (patch)
tree7cb519bf772ed395b612ddabbe27d989ec44b958 /ui/gfx/utf16_indexing.cc
parent9561605bc712063ba4ccb315c0a5cf5b0e3b221e (diff)
downloadchromium_src-bde885b4e8cf11db7ba2af6c70a6580830a52e7a.zip
chromium_src-bde885b4e8cf11db7ba2af6c70a6580830a52e7a.tar.gz
chromium_src-bde885b4e8cf11db7ba2af6c70a6580830a52e7a.tar.bz2
Move a bunch of windows stuff from ui/base/win to ui/gfx/win
R=sky@chromium.org BUG=103304 Review URL: https://codereview.chromium.org/23769011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@222860 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/utf16_indexing.cc')
-rw-r--r--ui/gfx/utf16_indexing.cc55
1 files changed, 55 insertions, 0 deletions
diff --git a/ui/gfx/utf16_indexing.cc b/ui/gfx/utf16_indexing.cc
new file mode 100644
index 0000000..6af10c1
--- /dev/null
+++ b/ui/gfx/utf16_indexing.cc
@@ -0,0 +1,55 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/gfx/utf16_indexing.h"
+
+#include "base/logging.h"
+#include "base/third_party/icu/icu_utf.h"
+
+namespace gfx {
+
+bool IsValidCodePointIndex(const string16& s, size_t index) {
+ return index == 0 || index == s.length() ||
+ !(CBU16_IS_TRAIL(s[index]) && CBU16_IS_LEAD(s[index - 1]));
+}
+
+ptrdiff_t UTF16IndexToOffset(const string16& s, size_t base, size_t pos) {
+ // The indices point between UTF-16 words (range 0 to s.length() inclusive).
+ // In order to consistently handle indices that point to the middle of a
+ // surrogate pair, we count the first word in that surrogate pair and not
+ // the second. The test "s[i] is not the second half of a surrogate pair" is
+ // "IsValidCodePointIndex(s, i)".
+ DCHECK_LE(base, s.length());
+ DCHECK_LE(pos, s.length());
+ ptrdiff_t delta = 0;
+ while (base < pos)
+ delta += IsValidCodePointIndex(s, base++) ? 1 : 0;
+ while (pos < base)
+ delta -= IsValidCodePointIndex(s, pos++) ? 1 : 0;
+ return delta;
+}
+
+size_t UTF16OffsetToIndex(const string16& s, size_t base, ptrdiff_t offset) {
+ DCHECK_LE(base, s.length());
+ // As in UTF16IndexToOffset, we count the first half of a surrogate pair, not
+ // the second. When stepping from pos to pos+1 we check s[pos:pos+1] == s[pos]
+ // (Python syntax), hence pos++. When stepping from pos to pos-1 we check
+ // s[pos-1], hence --pos.
+ size_t pos = base;
+ while (offset > 0 && pos < s.length())
+ offset -= IsValidCodePointIndex(s, pos++) ? 1 : 0;
+ while (offset < 0 && pos > 0)
+ offset += IsValidCodePointIndex(s, --pos) ? 1 : 0;
+ // If offset != 0 then we ran off the edge of the string, which is a contract
+ // violation but is handled anyway (by clamping) in release for safety.
+ DCHECK_EQ(offset, 0);
+ // Since the second half of a surrogate pair has "length" zero, there is an
+ // ambiguity in the returned position. Resolve it by always returning a valid
+ // index.
+ if (!IsValidCodePointIndex(s, pos))
+ ++pos;
+ return pos;
+}
+
+} // namespace gfx