OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/utf_offset_string_conversions.h" | 5 #include "base/utf_offset_string_conversions.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
11 #include "base/utf_string_conversion_utils.h" | 11 #include "base/utf_string_conversion_utils.h" |
12 | 12 |
13 using base::PrepareForUTF16Or32Output; | 13 using base::PrepareForUTF16Or32Output; |
14 using base::PrepareForUTF8Output; | |
14 using base::ReadUnicodeCharacter; | 15 using base::ReadUnicodeCharacter; |
15 using base::WriteUnicodeCharacter; | 16 using base::WriteUnicodeCharacter; |
16 | 17 |
17 // Converts the given source Unicode character type to the given destination | 18 // Converts the given source Unicode character type to the given destination |
18 // Unicode character type as a STL string. The given input buffer and size | 19 // Unicode character type as a STL string. The given input buffer and size |
19 // determine the source, and the given output STL string will be replaced by | 20 // determine the source, and the given output STL string will be replaced by |
20 // the result. | 21 // the result. |
21 bool ConvertUnicode(const char* src, | 22 template<typename SrcChar, typename DestStdString> |
23 bool ConvertUnicode(const SrcChar* src, | |
22 size_t src_len, | 24 size_t src_len, |
23 string16* output, | 25 DestStdString* output, |
24 std::vector<size_t>* offsets_for_adjustment) { | 26 std::vector<size_t>* offsets_for_adjustment) { |
25 if (offsets_for_adjustment) { | 27 if (offsets_for_adjustment) { |
26 std::for_each(offsets_for_adjustment->begin(), | 28 std::for_each(offsets_for_adjustment->begin(), |
27 offsets_for_adjustment->end(), | 29 offsets_for_adjustment->end(), |
28 LimitOffset<string16>(src_len)); | 30 LimitOffset<DestStdString>(src_len)); |
29 } | 31 } |
30 | 32 |
31 // ICU requires 32-bit numbers. | 33 // ICU requires 32-bit numbers. |
32 bool success = true; | 34 bool success = true; |
33 OffsetAdjuster offset_adjuster(offsets_for_adjustment); | 35 OffsetAdjuster offset_adjuster(offsets_for_adjustment); |
34 int32 src_len32 = static_cast<int32>(src_len); | 36 int32 src_len32 = static_cast<int32>(src_len); |
35 for (int32 i = 0; i < src_len32; i++) { | 37 for (int32 i = 0; i < src_len32; i++) { |
36 uint32 code_point; | 38 uint32 code_point; |
37 size_t original_i = i; | 39 size_t original_i = i; |
38 size_t chars_written = 0; | 40 size_t chars_written = 0; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 | 93 |
92 string16 UTF8ToUTF16AndAdjustOffsets( | 94 string16 UTF8ToUTF16AndAdjustOffsets( |
93 const base::StringPiece& utf8, | 95 const base::StringPiece& utf8, |
94 std::vector<size_t>* offsets_for_adjustment) { | 96 std::vector<size_t>* offsets_for_adjustment) { |
95 string16 result; | 97 string16 result; |
96 UTF8ToUTF16AndAdjustOffsets(utf8.data(), utf8.length(), &result, | 98 UTF8ToUTF16AndAdjustOffsets(utf8.data(), utf8.length(), &result, |
97 offsets_for_adjustment); | 99 offsets_for_adjustment); |
98 return result; | 100 return result; |
99 } | 101 } |
100 | 102 |
103 std::string UTF16ToUTF8AndAdjustOffset( | |
104 const base::StringPiece16& utf16, | |
105 size_t* offset_for_adjustment) { | |
106 std::vector<size_t> offsets; | |
107 if (offset_for_adjustment) | |
108 offsets.push_back(*offset_for_adjustment); | |
109 std::string result; | |
brettw
2011/09/07 19:28:07
The 8->16 version above just calls the vector vers
kinaba
2011/09/07 19:54:47
Done.
| |
110 PrepareForUTF8Output(utf16.data(), utf16.length(), &result); | |
111 ConvertUnicode(utf16.data(), utf16.length(), &result, &offsets); | |
112 if (offset_for_adjustment) | |
113 *offset_for_adjustment = offsets[0]; | |
114 return result; | |
115 } | |
116 | |
117 std::string UTF16ToUTF8AndAdjustOffsets( | |
118 const base::StringPiece16& utf16, | |
119 std::vector<size_t>* offsets_for_adjustment) { | |
120 std::string result; | |
121 PrepareForUTF8Output(utf16.data(), utf16.length(), &result); | |
122 ConvertUnicode(utf16.data(), utf16.length(), &result, offsets_for_adjustment); | |
123 return result; | |
124 } | |
125 | |
brettw
2011/09/07 19:28:07
Only one blank line needed.
kinaba
2011/09/07 19:54:47
Done.
| |
126 | |
101 OffsetAdjuster::Adjustment::Adjustment(size_t original_offset, | 127 OffsetAdjuster::Adjustment::Adjustment(size_t original_offset, |
102 size_t original_length, | 128 size_t original_length, |
103 size_t output_length) | 129 size_t output_length) |
104 : original_offset(original_offset), | 130 : original_offset(original_offset), |
105 original_length(original_length), | 131 original_length(original_length), |
106 output_length(output_length) { | 132 output_length(output_length) { |
107 } | 133 } |
108 | 134 |
109 OffsetAdjuster::OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment) | 135 OffsetAdjuster::OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment) |
110 : offsets_for_adjustment_(offsets_for_adjustment) { | 136 : offsets_for_adjustment_(offsets_for_adjustment) { |
(...skipping 24 matching lines...) Expand all Loading... | |
135 if (*offset <= i->original_offset) | 161 if (*offset <= i->original_offset) |
136 break; | 162 break; |
137 if (*offset < (i->original_offset + i->original_length)) { | 163 if (*offset < (i->original_offset + i->original_length)) { |
138 *offset = string16::npos; | 164 *offset = string16::npos; |
139 return; | 165 return; |
140 } | 166 } |
141 adjustment += (i->original_length - i->output_length); | 167 adjustment += (i->original_length - i->output_length); |
142 } | 168 } |
143 *offset -= adjustment; | 169 *offset -= adjustment; |
144 } | 170 } |
OLD | NEW |