Chromium Code Reviews| 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) { |
|
msw
2012/03/01 22:48:54
s/pos/base/ to match decl and for clarity.
benrg
2012/03/02 00:06:08
Done.
|
| + DCHECK_LE(pos, s.length()); |
|
msw
2012/03/01 22:48:54
Should you DCHECK_LE(pos + offset, s.length())?
Ma
benrg
2012/03/02 00:06:08
It covers those cases.
|
| + 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. |
|
msw
2012/03/01 22:48:54
It seems like this should happen with garbage inpu
benrg
2012/03/02 00:06:08
Rewritten.
|
| + 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, |