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

Unified Diff: base/utf_offset_string_conversions.cc

Issue 8747001: Reintroduce password support to NativeTextfieldViews (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: new implementation, linux only 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
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..a3b19558cb82bf9818011425b5e0215feed76dad 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,36 @@ bool ConvertUnicode(const SrcChar* src,
return success;
}
+} // namespace
+
+bool IsValidUtf16Index(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 from, size_t to) {
+ DCHECK_LE(from, s.length());
+ DCHECK_LE(to, s.length());
+ ptrdiff_t delta = 0;
+ while (from < to)
+ delta += IsValidUtf16Index(s, from++);
msw 2012/02/22 00:33:26 This adds a bool? Can you make it a ternary and ex
benrg 2012/02/24 19:07:44 Done.
+ while (to < from)
+ delta -= IsValidUtf16Index(s, to++);
+ 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 -= IsValidUtf16Index(s, pos++);
+ while (offset < 0 && pos > 0)
+ offset += IsValidUtf16Index(s, --pos);
+ // If offset != 0 then we ran off the edge of the string, which shouldn't
+ // happen but is handled anyway for safety.
+ DCHECK(offset == 0);
msw 2012/02/22 00:33:26 DCHECK_EQ
+ return pos;
+}
+
bool UTF8ToUTF16AndAdjustOffset(const char* src,
size_t src_len,
string16* output,

Powered by Google App Engine
This is Rietveld 408576698