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

Unified Diff: base/utf_offset_string_conversions.cc

Issue 9467017: Password support for NativeTextfieldViews, in the model (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/utf_offset_string_conversions.h ('k') | base/utf_offset_string_conversions_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/utf_offset_string_conversions.cc
diff --git a/base/utf_offset_string_conversions.cc b/base/utf_offset_string_conversions.cc
index 8405303fd9d95bd5fb8fe3ef0a0bcdb70102cd80..3e468b554ab29a89114f756c5ed8a4666fb40a04 100644
--- a/base/utf_offset_string_conversions.cc
+++ b/base/utf_offset_string_conversions.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// 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.
@@ -6,8 +6,10 @@
#include <algorithm>
+#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_piece.h"
+#include "base/third_party/icu/icu_utf.h"
#include "base/utf_string_conversion_utils.h"
using base::PrepareForUTF16Or32Output;
@@ -15,6 +17,8 @@ using base::PrepareForUTF8Output;
using base::ReadUnicodeCharacter;
using base::WriteUnicodeCharacter;
+namespace {
+
// Converts the given source Unicode character type to the given destination
// Unicode character type as a STL string. The given input buffer and size
// determine the source, and the given output STL string will be replaced by
@@ -56,6 +60,41 @@ bool ConvertUnicode(const SrcChar* src,
return success;
}
+} // namespace
+
+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) {
+ 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 pos, ptrdiff_t offset) {
+ DCHECK_LE(pos, s.length());
+ 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 shouldn't
+ // happen but is handled anyway 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;
+}
+
bool UTF8ToUTF16AndAdjustOffset(const char* src,
size_t src_len,
string16* output,
« no previous file with comments | « base/utf_offset_string_conversions.h ('k') | base/utf_offset_string_conversions_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698