OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // ICU integration functions. | 5 // ICU integration functions. |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 // TODO(jungshik): Look at info.errors to handle them case-by-case basis | 177 // TODO(jungshik): Look at info.errors to handle them case-by-case basis |
178 // if necessary. | 178 // if necessary. |
179 if (err != U_BUFFER_OVERFLOW_ERROR || info.errors != 0) | 179 if (err != U_BUFFER_OVERFLOW_ERROR || info.errors != 0) |
180 return false; // Unknown error, give up. | 180 return false; // Unknown error, give up. |
181 | 181 |
182 // Not enough room in our buffer, expand. | 182 // Not enough room in our buffer, expand. |
183 output->Resize(output_length); | 183 output->Resize(output_length); |
184 } | 184 } |
185 } | 185 } |
186 | 186 |
187 bool ReadUTFChar(const char* str, int* begin, int length, | |
188 unsigned* code_point_out) { | |
189 int code_point; // Avoids warning when U8_NEXT writes -1 to it. | |
190 U8_NEXT(str, *begin, length, code_point); | |
191 *code_point_out = static_cast<unsigned>(code_point); | |
192 | |
193 // The ICU macro above moves to the next char, we want to point to the last | |
194 // char consumed. | |
195 (*begin)--; | |
196 | |
197 // Validate the decoded value. | |
198 if (U_IS_UNICODE_CHAR(code_point)) | |
199 return true; | |
200 *code_point_out = kUnicodeReplacementCharacter; | |
201 return false; | |
202 } | |
203 | |
204 bool ReadUTFChar(const base::char16* str, int* begin, int length, | |
205 unsigned* code_point) { | |
206 if (U16_IS_SURROGATE(str[*begin])) { | |
207 if (!U16_IS_SURROGATE_LEAD(str[*begin]) || *begin + 1 >= length || | |
208 !U16_IS_TRAIL(str[*begin + 1])) { | |
209 // Invalid surrogate pair. | |
210 *code_point = kUnicodeReplacementCharacter; | |
211 return false; | |
212 } else { | |
213 // Valid surrogate pair. | |
214 *code_point = U16_GET_SUPPLEMENTARY(str[*begin], str[*begin + 1]); | |
215 (*begin)++; | |
216 } | |
217 } else { | |
218 // Not a surrogate, just one 16-bit word. | |
219 *code_point = str[*begin]; | |
220 } | |
221 | |
222 if (U_IS_UNICODE_CHAR(*code_point)) | |
223 return true; | |
224 | |
225 // Invalid code point. | |
226 *code_point = kUnicodeReplacementCharacter; | |
227 return false; | |
228 } | |
229 | |
230 } // namespace url | 187 } // namespace url |
OLD | NEW |