Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2006, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2008, 2010 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 12 matching lines...) Expand all Loading... | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "config.h" | 26 #include "config.h" |
| 27 #include "wtf/text/TextCodecUTF16.h" | 27 #include "wtf/text/TextCodecUTF16.h" |
| 28 | 28 |
| 29 #include "wtf/PassOwnPtr.h" | 29 #include "wtf/PassOwnPtr.h" |
| 30 #include "wtf/text/CString.h" | 30 #include "wtf/text/CString.h" |
| 31 #include "wtf/text/StringBuffer.h" | 31 #include "wtf/text/StringBuffer.h" |
| 32 #include "wtf/text/WTFString.h" | 32 #include "wtf/text/WTFString.h" |
| 33 #include "wtf/unicode/CharacterNames.h" | |
| 33 | 34 |
| 34 using namespace std; | 35 using namespace std; |
| 35 | 36 |
| 36 namespace WTF { | 37 namespace WTF { |
| 37 | 38 |
| 38 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) | 39 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) |
| 39 { | 40 { |
| 40 registrar("UTF-16LE", "UTF-16LE"); | 41 registrar("UTF-16LE", "UTF-16LE"); |
| 41 registrar("UTF-16BE", "UTF-16BE"); | 42 registrar("UTF-16BE", "UTF-16BE"); |
| 42 | 43 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 59 { | 60 { |
| 60 return adoptPtr(new TextCodecUTF16(false)); | 61 return adoptPtr(new TextCodecUTF16(false)); |
| 61 } | 62 } |
| 62 | 63 |
| 63 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) | 64 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) |
| 64 { | 65 { |
| 65 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0); | 66 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0); |
| 66 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0); | 67 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0); |
| 67 } | 68 } |
| 68 | 69 |
| 69 String TextCodecUTF16::decode(const char* bytes, size_t length, bool, bool, bool &) | 70 String TextCodecUTF16::decode(const char* bytes, size_t length, bool flush, bool , bool& sawError) |
| 70 { | 71 { |
| 71 if (!length) | 72 if (!length) { |
| 72 return String(); | 73 if (!flush || !m_haveBufferedByte) |
| 74 return String(); | |
| 75 sawError = true; | |
| 76 String s; | |
| 77 s.append(Unicode::replacementCharacter); | |
|
abarth-chromium
2013/09/05 05:57:51
Never call String::append. It's a terrible, no go
| |
| 78 return s; | |
| 79 } | |
| 73 | 80 |
| 74 // FIXME: This should generate an error if there is an unpaired surrogate. | 81 // FIXME: This should generate an error if there is an unpaired surrogate. |
| 75 | 82 |
| 76 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes); | 83 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes); |
| 77 size_t numBytes = length + m_haveBufferedByte; | 84 size_t numBytes = length + m_haveBufferedByte; |
| 78 size_t numChars = numBytes / 2; | 85 size_t numCharsIn = numBytes / 2; |
| 86 size_t numCharsOut = ((numBytes & 1) && flush) ? numCharsIn + 1 : numCharsIn ; | |
| 79 | 87 |
| 80 StringBuffer<UChar> buffer(numChars); | 88 StringBuffer<UChar> buffer(numCharsOut); |
| 81 UChar* q = buffer.characters(); | 89 UChar* q = buffer.characters(); |
| 82 | 90 |
| 83 if (m_haveBufferedByte) { | 91 if (m_haveBufferedByte) { |
| 84 UChar c; | 92 UChar c; |
| 85 if (m_littleEndian) | 93 if (m_littleEndian) |
| 86 c = m_bufferedByte | (p[0] << 8); | 94 c = m_bufferedByte | (p[0] << 8); |
| 87 else | 95 else |
| 88 c = (m_bufferedByte << 8) | p[0]; | 96 c = (m_bufferedByte << 8) | p[0]; |
| 89 *q++ = c; | 97 *q++ = c; |
| 90 m_haveBufferedByte = false; | 98 m_haveBufferedByte = false; |
| 91 p += 1; | 99 p += 1; |
| 92 numChars -= 1; | 100 numCharsIn -= 1; |
| 93 } | 101 } |
| 94 | 102 |
| 95 if (m_littleEndian) { | 103 if (m_littleEndian) { |
| 96 for (size_t i = 0; i < numChars; ++i) { | 104 for (size_t i = 0; i < numCharsIn; ++i) { |
| 97 UChar c = p[0] | (p[1] << 8); | 105 UChar c = p[0] | (p[1] << 8); |
| 98 p += 2; | 106 p += 2; |
| 99 *q++ = c; | 107 *q++ = c; |
| 100 } | 108 } |
| 101 } else { | 109 } else { |
| 102 for (size_t i = 0; i < numChars; ++i) { | 110 for (size_t i = 0; i < numCharsIn; ++i) { |
| 103 UChar c = (p[0] << 8) | p[1]; | 111 UChar c = (p[0] << 8) | p[1]; |
| 104 p += 2; | 112 p += 2; |
| 105 *q++ = c; | 113 *q++ = c; |
| 106 } | 114 } |
| 107 } | 115 } |
| 108 | 116 |
| 109 if (numBytes & 1) { | 117 if (numBytes & 1) { |
| 110 ASSERT(!m_haveBufferedByte); | 118 ASSERT(!m_haveBufferedByte); |
| 111 m_haveBufferedByte = true; | 119 |
| 112 m_bufferedByte = p[0]; | 120 if (flush) { |
| 121 sawError = true; | |
| 122 *q++ = Unicode::replacementCharacter; | |
| 123 } else { | |
| 124 m_haveBufferedByte = true; | |
| 125 m_bufferedByte = p[0]; | |
| 126 } | |
| 113 } | 127 } |
| 114 | 128 |
| 115 buffer.shrink(q - buffer.characters()); | 129 buffer.shrink(q - buffer.characters()); |
| 116 | 130 |
| 117 return String::adopt(buffer); | 131 return String::adopt(buffer); |
| 118 } | 132 } |
| 119 | 133 |
| 120 CString TextCodecUTF16::encode(const UChar* characters, size_t length, Unencodab leHandling) | 134 CString TextCodecUTF16::encode(const UChar* characters, size_t length, Unencodab leHandling) |
| 121 { | 135 { |
| 122 // We need to be sure we can double the length without overflowing. | 136 // We need to be sure we can double the length without overflowing. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 for (size_t i = 0; i < length; ++i) { | 180 for (size_t i = 0; i < length; ++i) { |
| 167 bytes[i * 2] = 0; | 181 bytes[i * 2] = 0; |
| 168 bytes[i * 2 + 1] = characters[i]; | 182 bytes[i * 2 + 1] = characters[i]; |
| 169 } | 183 } |
| 170 } | 184 } |
| 171 | 185 |
| 172 return result; | 186 return result; |
| 173 } | 187 } |
| 174 | 188 |
| 175 } // namespace WTF | 189 } // namespace WTF |
| OLD | NEW |