OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 --m_partialSequenceSize; | 160 --m_partialSequenceSize; |
161 memmove(m_partialSequence, m_partialSequence + 1, m_partialSequenceSize); | 161 memmove(m_partialSequence, m_partialSequence + 1, m_partialSequenceSize); |
162 } | 162 } |
163 | 163 |
164 void TextCodecUTF8::handleError(UChar*& destination, bool stopOnError, bool& saw
Error) | 164 void TextCodecUTF8::handleError(UChar*& destination, bool stopOnError, bool& saw
Error) |
165 { | 165 { |
166 sawError = true; | 166 sawError = true; |
167 if (stopOnError) | 167 if (stopOnError) |
168 return; | 168 return; |
169 // Each error generates a replacement character and consumes one byte. | 169 // Each error generates a replacement character and consumes one byte. |
170 *destination++ = replacementCharacter; | 170 *destination++ = characterReplacement; |
171 consumePartialSequenceByte(); | 171 consumePartialSequenceByte(); |
172 } | 172 } |
173 | 173 |
174 template <> | 174 template <> |
175 bool TextCodecUTF8::handlePartialSequence<LChar>(LChar*& destination, const uint
8_t*& source, const uint8_t* end, bool flush, bool, bool&) | 175 bool TextCodecUTF8::handlePartialSequence<LChar>(LChar*& destination, const uint
8_t*& source, const uint8_t* end, bool flush, bool, bool&) |
176 { | 176 { |
177 ASSERT(m_partialSequenceSize); | 177 ASSERT(m_partialSequenceSize); |
178 do { | 178 do { |
179 if (isASCII(m_partialSequence[0])) { | 179 if (isASCII(m_partialSequence[0])) { |
180 *destination++ = m_partialSequence[0]; | 180 *destination++ = m_partialSequence[0]; |
181 consumePartialSequenceByte(); | 181 consumePartialSequenceByte(); |
182 continue; | 182 continue; |
183 } | 183 } |
184 int count = nonASCIISequenceLength(m_partialSequence[0]); | 184 int count = nonASCIISequenceLength(m_partialSequence[0]); |
185 if (!count) | 185 if (!count) |
186 return true; | 186 return true; |
187 | 187 |
188 if (count > m_partialSequenceSize) { | 188 if (count > m_partialSequenceSize) { |
189 if (count - m_partialSequenceSize > end - source) { | 189 if (count - m_partialSequenceSize > end - source) { |
190 if (!flush) { | 190 if (!flush) { |
191 // The new data is not enough to complete the sequence, so | 191 // The new data is not enough to complete the sequence, so |
192 // add it to the existing partial sequence. | 192 // add it to the existing partial sequence. |
193 memcpy(m_partialSequence + m_partialSequenceSize, source, en
d - source); | 193 memcpy(m_partialSequence + m_partialSequenceSize, source, en
d - source); |
194 m_partialSequenceSize += end - source; | 194 m_partialSequenceSize += end - source; |
195 return false; | 195 return false; |
196 } | 196 } |
197 // An incomplete partial sequence at the end is an error, but it
will create | 197 // An incomplete partial sequence at the end is an error, but it
will create |
198 // a 16 bit string due to the replacementCharacter. Let the 16 b
it path handle | 198 // a 16 bit string due to the characterReplacement. Let the 16 b
it path handle |
199 // the error. | 199 // the error. |
200 return true; | 200 return true; |
201 } | 201 } |
202 memcpy(m_partialSequence + m_partialSequenceSize, source, count - m_
partialSequenceSize); | 202 memcpy(m_partialSequence + m_partialSequenceSize, source, count - m_
partialSequenceSize); |
203 source += count - m_partialSequenceSize; | 203 source += count - m_partialSequenceSize; |
204 m_partialSequenceSize = count; | 204 m_partialSequenceSize = count; |
205 } | 205 } |
206 int character = decodeNonASCIISequence(m_partialSequence, count); | 206 int character = decodeNonASCIISequence(m_partialSequence, count); |
207 if (character & ~0xff) | 207 if (character & ~0xff) |
208 return true; | 208 return true; |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 source = end; | 404 source = end; |
405 break; | 405 break; |
406 } | 406 } |
407 character = decodeNonASCIISequence(source, count); | 407 character = decodeNonASCIISequence(source, count); |
408 } | 408 } |
409 if (character == nonCharacter) { | 409 if (character == nonCharacter) { |
410 sawError = true; | 410 sawError = true; |
411 if (stopOnError) | 411 if (stopOnError) |
412 break; | 412 break; |
413 // Each error generates a replacement character and consumes one
byte. | 413 // Each error generates a replacement character and consumes one
byte. |
414 *destination16++ = replacementCharacter; | 414 *destination16++ = characterReplacement; |
415 ++source; | 415 ++source; |
416 continue; | 416 continue; |
417 } | 417 } |
418 source += count; | 418 source += count; |
419 destination16 = appendCharacter(destination16, character); | 419 destination16 = appendCharacter(destination16, character); |
420 } | 420 } |
421 } while (flush && m_partialSequenceSize); | 421 } while (flush && m_partialSequenceSize); |
422 | 422 |
423 buffer16.shrink(destination16 - buffer16.characters()); | 423 buffer16.shrink(destination16 - buffer16.characters()); |
424 | 424 |
(...skipping 11 matching lines...) Expand all Loading... |
436 Vector<uint8_t> bytes(length * 3); | 436 Vector<uint8_t> bytes(length * 3); |
437 | 437 |
438 size_t i = 0; | 438 size_t i = 0; |
439 size_t bytesWritten = 0; | 439 size_t bytesWritten = 0; |
440 while (i < length) { | 440 while (i < length) { |
441 UChar32 character; | 441 UChar32 character; |
442 U16_NEXT(characters, i, length, character); | 442 U16_NEXT(characters, i, length, character); |
443 // U16_NEXT will simply emit a surrogate code point if an unmatched surr
ogate | 443 // U16_NEXT will simply emit a surrogate code point if an unmatched surr
ogate |
444 // is encountered; we must convert it to a U+FFFD (REPLACEMENT CHARACTER
) here. | 444 // is encountered; we must convert it to a U+FFFD (REPLACEMENT CHARACTER
) here. |
445 if (0xD800 <= character && character <= 0xDFFF) | 445 if (0xD800 <= character && character <= 0xDFFF) |
446 character = replacementCharacter; | 446 character = characterReplacement; |
447 U8_APPEND_UNSAFE(bytes.data(), bytesWritten, character); | 447 U8_APPEND_UNSAFE(bytes.data(), bytesWritten, character); |
448 } | 448 } |
449 | 449 |
450 return CString(reinterpret_cast<char*>(bytes.data()), bytesWritten); | 450 return CString(reinterpret_cast<char*>(bytes.data()), bytesWritten); |
451 } | 451 } |
452 | 452 |
453 CString TextCodecUTF8::encode(const UChar* characters, size_t length, Unencodabl
eHandling) | 453 CString TextCodecUTF8::encode(const UChar* characters, size_t length, Unencodabl
eHandling) |
454 { | 454 { |
455 return encodeCommon(characters, length); | 455 return encodeCommon(characters, length); |
456 } | 456 } |
457 | 457 |
458 CString TextCodecUTF8::encode(const LChar* characters, size_t length, Unencodabl
eHandling) | 458 CString TextCodecUTF8::encode(const LChar* characters, size_t length, Unencodabl
eHandling) |
459 { | 459 { |
460 return encodeCommon(characters, length); | 460 return encodeCommon(characters, length); |
461 } | 461 } |
462 | 462 |
463 } // namespace WTF | 463 } // namespace WTF |
OLD | NEW |