OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef UI_BASE_TEXT_UTF16_INDEXING_H_ |
| 6 #define UI_BASE_TEXT_UTF16_INDEXING_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/string16.h" |
| 10 #include "ui/base/ui_export.h" |
| 11 |
| 12 namespace ui { |
| 13 |
| 14 // Returns false if s[index-1] is a high surrogate and s[index] is a low |
| 15 // surrogate, true otherwise. |
| 16 UI_EXPORT bool IsValidCodePointIndex(const string16& s, size_t index); |
| 17 |
| 18 // |UTF16IndexToOffset| returns the number of code points between |base| and |
| 19 // |pos| in the given string. |UTF16OffsetToIndex| returns the index that is |
| 20 // |offset| code points away from the given |base| index. These functions are |
| 21 // named after glib's |g_utf8_pointer_to_offset| and |g_utf8_offset_to_pointer|, |
| 22 // which perform the same function for UTF-8. As in glib, it is an error to |
| 23 // pass an |offset| that walks off the edge of the string. |
| 24 // |
| 25 // These functions attempt to deal with invalid use of UTF-16 surrogates in a |
| 26 // way that makes as much sense as possible: unpaired surrogates are treated as |
| 27 // single characters, and if an argument index points to the middle of a valid |
| 28 // surrogate pair, it is treated as though it pointed to the end of that pair. |
| 29 // The index returned by |UTF16OffsetToIndex| never points to the middle of a |
| 30 // surrogate pair. |
| 31 // |
| 32 // The following identities hold: |
| 33 // If |s| contains no surrogate pairs, then |
| 34 // UTF16IndexToOffset(s, base, pos) == pos - base |
| 35 // UTF16OffsetToIndex(s, base, offset) == base + offset |
| 36 // If |pos| does not point to the middle of a surrogate pair, then |
| 37 // UTF16OffsetToIndex(s, base, UTF16IndexToOffset(s, base, pos)) == pos |
| 38 // Always, |
| 39 // UTF16IndexToOffset(s, base, UTF16OffsetToIndex(s, base, ofs)) == ofs |
| 40 // UTF16IndexToOffset(s, i, j) == -UTF16IndexToOffset(s, j, i) |
| 41 UI_EXPORT ptrdiff_t UTF16IndexToOffset(const string16& s, |
| 42 size_t base, |
| 43 size_t pos); |
| 44 UI_EXPORT size_t UTF16OffsetToIndex(const string16& s, |
| 45 size_t base, |
| 46 ptrdiff_t offset); |
| 47 |
| 48 } // namespace ui |
| 49 |
| 50 #endif // UI_BASE_TEXT_UTF16_INDEXING_H_ |
OLD | NEW |