OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ | 5 #ifndef BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |
6 #define BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ | 6 #define BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
15 | 15 |
| 16 // Returns false if s[index-1] is a high surrogate and s[index] is a low |
| 17 // surrogate, true otherwise. |
| 18 BASE_EXPORT bool IsValidCodePointIndex(const string16& s, size_t index); |
| 19 |
| 20 // |Utf16IndexToOffset| returns the number of code points between |base| and |
| 21 // |pos| in the given string. |Utf16OffsetToIndex| returns the index that is |
| 22 // |offset| code points away from the given |base| index. These functions are |
| 23 // named after glib's |g_utf8_pointer_to_offset| and |g_utf8_offset_to_pointer|, |
| 24 // which perform the same function for UTF-8. |
| 25 // Unpaired surrogates in the string are counted as BMP characters. If an |
| 26 // argument index points to the middle of a surrogate pair, it is treated as |
| 27 // though it pointed to the end of that pair (in other words, the first word of |
| 28 // any surrogate pair is the one that is counted). The index returned by |
| 29 // |Utf16OffsetToIndex| never points to the middle of a surrogate pair. |
| 30 // The following identities hold: |
| 31 // If |s| contains no surrogate pairs, then |
| 32 // Utf16IndexToOffset(s, base, pos) == pos - base |
| 33 // Utf16OffsetToIndex(s, base, offset) == base + offset |
| 34 // If |pos| does not point to the middle of a surrogate pair, then |
| 35 // Utf16OffsetToIndex(s, base, Utf16IndexToOffset(s, base, pos)) == pos |
| 36 // Always, |
| 37 // Utf16IndexToOffset(s, base, Utf16OffsetToIndex(s, base, ofs)) == ofs |
| 38 // Utf16IndexToOffset(s, i, j) == -Utf16IndexToOffset(s, j, i) |
| 39 BASE_EXPORT ptrdiff_t Utf16IndexToOffset(const string16& s, |
| 40 size_t base, |
| 41 size_t pos); |
| 42 BASE_EXPORT size_t Utf16OffsetToIndex(const string16& s, |
| 43 size_t base, |
| 44 ptrdiff_t offset); |
| 45 |
16 // Like the conversions in utf_string_conversions.h, but also takes one or more | 46 // Like the conversions in utf_string_conversions.h, but also takes one or more |
17 // offsets (|offset[s]_for_adjustment|) into the source strings, each offset | 47 // offsets (|offset[s]_for_adjustment|) into the source strings, each offset |
18 // will be adjusted to point at the same logical place in the result strings. | 48 // will be adjusted to point at the same logical place in the result strings. |
19 // If this isn't possible because an offset points past the end of the source | 49 // If this isn't possible because an offset points past the end of the source |
20 // strings or into the middle of a multibyte sequence, the offending offset will | 50 // strings or into the middle of a multibyte sequence, the offending offset will |
21 // be set to string16::npos. |offset[s]_for_adjustment| may be NULL. | 51 // be set to string16::npos. |offset[s]_for_adjustment| may be NULL. |
22 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src, | 52 BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src, |
23 size_t src_len, | 53 size_t src_len, |
24 string16* output, | 54 string16* output, |
25 size_t* offset_for_adjustment); | 55 size_t* offset_for_adjustment); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 void Add(const Adjustment& adjustment); | 111 void Add(const Adjustment& adjustment); |
82 | 112 |
83 private: | 113 private: |
84 void AdjustOffset(std::vector<size_t>::iterator offset); | 114 void AdjustOffset(std::vector<size_t>::iterator offset); |
85 | 115 |
86 std::vector<size_t>* offsets_for_adjustment_; | 116 std::vector<size_t>* offsets_for_adjustment_; |
87 std::vector<Adjustment> adjustments_; | 117 std::vector<Adjustment> adjustments_; |
88 }; | 118 }; |
89 | 119 |
90 #endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ | 120 #endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_ |
OLD | NEW |