| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2008 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 17 matching lines...) Expand all Loading... |
| 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/TextCodecASCIIFastPath.h" | 32 #include "wtf/text/TextCodecASCIIFastPath.h" |
| 33 #include "wtf/text/WTFString.h" | 33 #include "wtf/text/WTFString.h" |
| 34 | 34 |
| 35 namespace WTF { | 35 namespace WTF { |
| 36 | 36 |
| 37 static const UChar table[256] = { | 37 static const UChar table[256] = { |
| 38 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, // 00-07 | 38 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, // 00-07 |
| 39 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, // 08-0F | 39 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, // 08-0F |
| 40 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, // 10-17 | 40 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, // 10-17 |
| 41 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, // 18-1F | 41 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, // 18-1F |
| 42 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, // 20-27 | 42 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, // 20-27 |
| 43 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, // 28-2F | 43 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, // 28-2F |
| 44 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, // 30-37 | 44 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, // 30-37 |
| 45 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, // 38-3F | 45 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, // 38-3F |
| 46 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, // 40-47 | 46 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, // 40-47 |
| 47 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, // 48-4F | 47 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, // 48-4F |
| 48 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, // 50-57 | 48 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, // 50-57 |
| 49 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, // 58-5F | 49 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, // 58-5F |
| 50 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, // 60-67 | 50 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, // 60-67 |
| 51 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, // 68-6F | 51 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, // 68-6F |
| 52 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, // 70-77 | 52 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, // 70-77 |
| 53 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, // 78-7F | 53 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, // 78-7F |
| 54 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, // 80-87 | 54 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, // 80-87 |
| 55 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F, // 88-8F | 55 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F, // 88-8F |
| 56 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, // 90-97 | 56 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, // 90-97 |
| 57 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178, // 98-9F | 57 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178, // 98-9F |
| 58 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, // A0-A7 | 58 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, // A0-A7 |
| 59 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, // A8-AF | 59 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, // A8-AF |
| 60 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, // B0-B7 | 60 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, // B0-B7 |
| 61 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, // B8-BF | 61 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, // B8-BF |
| 62 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, // C0-C7 | 62 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, // C0-C7 |
| 63 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, // C8-CF | 63 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, // C8-CF |
| 64 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, // D0-D7 | 64 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, // D0-D7 |
| 65 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, // D8-DF | 65 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, // D8-DF |
| 66 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, // E0-E7 | 66 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, // E0-E7 |
| 67 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, // E8-EF | 67 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, // E8-EF |
| 68 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, // F0-F7 | 68 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, // F0-F7 |
| 69 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF // F8-FF | 69 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF // F8-FF |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 void TextCodecLatin1::registerEncodingNames(EncodingNameRegistrar registrar) | 72 void TextCodecLatin1::registerEncodingNames(EncodingNameRegistrar registrar) { |
| 73 { | 73 // Taken from the alias table at https://encoding.spec.whatwg.org/ |
| 74 // Taken from the alias table at https://encoding.spec.whatwg.org/ | 74 registrar("windows-1252", "windows-1252"); |
| 75 registrar("windows-1252", "windows-1252"); | 75 registrar("ANSI_X3.4-1968", "windows-1252"); |
| 76 registrar("ANSI_X3.4-1968", "windows-1252"); | 76 registrar("ASCII", "windows-1252"); |
| 77 registrar("ASCII", "windows-1252"); | 77 registrar("cp1252", "windows-1252"); |
| 78 registrar("cp1252", "windows-1252"); | 78 registrar("cp819", "windows-1252"); |
| 79 registrar("cp819", "windows-1252"); | 79 registrar("csISOLatin1", "windows-1252"); |
| 80 registrar("csISOLatin1", "windows-1252"); | 80 registrar("IBM819", "windows-1252"); |
| 81 registrar("IBM819", "windows-1252"); | 81 registrar("ISO-8859-1", "windows-1252"); |
| 82 registrar("ISO-8859-1", "windows-1252"); | 82 registrar("iso-ir-100", "windows-1252"); |
| 83 registrar("iso-ir-100", "windows-1252"); | 83 registrar("iso8859-1", "windows-1252"); |
| 84 registrar("iso8859-1", "windows-1252"); | 84 registrar("iso88591", "windows-1252"); |
| 85 registrar("iso88591", "windows-1252"); | 85 registrar("iso_8859-1", "windows-1252"); |
| 86 registrar("iso_8859-1", "windows-1252"); | 86 registrar("iso_8859-1:1987", "windows-1252"); |
| 87 registrar("iso_8859-1:1987", "windows-1252"); | 87 registrar("l1", "windows-1252"); |
| 88 registrar("l1", "windows-1252"); | 88 registrar("latin1", "windows-1252"); |
| 89 registrar("latin1", "windows-1252"); | 89 registrar("US-ASCII", "windows-1252"); |
| 90 registrar("US-ASCII", "windows-1252"); | 90 registrar("x-cp1252", "windows-1252"); |
| 91 registrar("x-cp1252", "windows-1252"); | 91 } |
| 92 } | 92 |
| 93 | 93 static PassOwnPtr<TextCodec> newStreamingTextDecoderWindowsLatin1(const TextEnco
ding&, const void*) { |
| 94 static PassOwnPtr<TextCodec> newStreamingTextDecoderWindowsLatin1(const TextEnco
ding&, const void*) | 94 return adoptPtr(new TextCodecLatin1); |
| 95 { | 95 } |
| 96 return adoptPtr(new TextCodecLatin1); | 96 |
| 97 } | 97 void TextCodecLatin1::registerCodecs(TextCodecRegistrar registrar) { |
| 98 | 98 registrar("windows-1252", newStreamingTextDecoderWindowsLatin1, 0); |
| 99 void TextCodecLatin1::registerCodecs(TextCodecRegistrar registrar) | 99 |
| 100 { | 100 // ASCII and Latin-1 both decode as Windows Latin-1 although they retain uniqu
e identities. |
| 101 registrar("windows-1252", newStreamingTextDecoderWindowsLatin1, 0); | 101 registrar("ISO-8859-1", newStreamingTextDecoderWindowsLatin1, 0); |
| 102 | 102 registrar("US-ASCII", newStreamingTextDecoderWindowsLatin1, 0); |
| 103 // ASCII and Latin-1 both decode as Windows Latin-1 although they retain uni
que identities. | 103 } |
| 104 registrar("ISO-8859-1", newStreamingTextDecoderWindowsLatin1, 0); | 104 |
| 105 registrar("US-ASCII", newStreamingTextDecoderWindowsLatin1, 0); | 105 String TextCodecLatin1::decode(const char* bytes, size_t length, FlushBehavior,
bool, bool&) { |
| 106 } | 106 LChar* characters; |
| 107 | 107 if (!length) |
| 108 String TextCodecLatin1::decode(const char* bytes, size_t length, FlushBehavior,
bool, bool&) | 108 return emptyString(); |
| 109 { | 109 String result = String::createUninitialized(length, characters); |
| 110 LChar* characters; | 110 |
| 111 if (!length) | 111 const uint8_t* source = reinterpret_cast<const uint8_t*>(bytes); |
| 112 return emptyString(); | 112 const uint8_t* end = reinterpret_cast<const uint8_t*>(bytes + length); |
| 113 String result = String::createUninitialized(length, characters); | 113 const uint8_t* alignedEnd = alignToMachineWord(end); |
| 114 | 114 LChar* destination = characters; |
| 115 const uint8_t* source = reinterpret_cast<const uint8_t*>(bytes); | 115 |
| 116 const uint8_t* end = reinterpret_cast<const uint8_t*>(bytes + length); | 116 while (source < end) { |
| 117 const uint8_t* alignedEnd = alignToMachineWord(end); | 117 if (isASCII(*source)) { |
| 118 LChar* destination = characters; | 118 // Fast path for ASCII. Most Latin-1 text will be ASCII. |
| 119 | 119 if (isAlignedToMachineWord(source)) { |
| 120 while (source < end) { | 120 while (source < alignedEnd) { |
| 121 if (isASCII(*source)) { | 121 MachineWord chunk = *reinterpret_cast_ptr<const MachineWord*>(source); |
| 122 // Fast path for ASCII. Most Latin-1 text will be ASCII. | 122 |
| 123 if (isAlignedToMachineWord(source)) { | 123 if (!isAllASCII<LChar>(chunk)) |
| 124 while (source < alignedEnd) { | 124 goto useLookupTable; |
| 125 MachineWord chunk = *reinterpret_cast_ptr<const MachineWord*
>(source); | 125 |
| 126 | 126 copyASCIIMachineWord(destination, source); |
| 127 if (!isAllASCII<LChar>(chunk)) | 127 source += sizeof(MachineWord); |
| 128 goto useLookupTable; | 128 destination += sizeof(MachineWord); |
| 129 | |
| 130 copyASCIIMachineWord(destination, source); | |
| 131 source += sizeof(MachineWord); | |
| 132 destination += sizeof(MachineWord); | |
| 133 } | |
| 134 | |
| 135 if (source == end) | |
| 136 break; | |
| 137 } | |
| 138 *destination = *source; | |
| 139 } else { | |
| 140 useLookupTable: | |
| 141 if (table[*source] > 0xff) | |
| 142 goto upConvertTo16Bit; | |
| 143 | |
| 144 *destination = static_cast<LChar>(table[*source]); | |
| 145 } | 129 } |
| 146 | 130 |
| 147 ++source; | 131 if (source == end) |
| 148 ++destination; | 132 break; |
| 149 } | 133 } |
| 150 | 134 *destination = *source; |
| 151 return result; | 135 } else { |
| 136 useLookupTable: |
| 137 if (table[*source] > 0xff) |
| 138 goto upConvertTo16Bit; |
| 139 |
| 140 *destination = static_cast<LChar>(table[*source]); |
| 141 } |
| 142 |
| 143 ++source; |
| 144 ++destination; |
| 145 } |
| 146 |
| 147 return result; |
| 152 | 148 |
| 153 upConvertTo16Bit: | 149 upConvertTo16Bit: |
| 154 UChar* characters16; | 150 UChar* characters16; |
| 155 String result16 = String::createUninitialized(length, characters16); | 151 String result16 = String::createUninitialized(length, characters16); |
| 156 | 152 |
| 157 UChar* destination16 = characters16; | 153 UChar* destination16 = characters16; |
| 158 | 154 |
| 159 // Zero extend and copy already processed 8 bit data | 155 // Zero extend and copy already processed 8 bit data |
| 160 LChar* ptr8 = characters; | 156 LChar* ptr8 = characters; |
| 161 LChar* endPtr8 = destination; | 157 LChar* endPtr8 = destination; |
| 162 | 158 |
| 163 while (ptr8 < endPtr8) | 159 while (ptr8 < endPtr8) |
| 164 *destination16++ = *ptr8++; | 160 *destination16++ = *ptr8++; |
| 165 | 161 |
| 166 // Handle the character that triggered the 16 bit path | 162 // Handle the character that triggered the 16 bit path |
| 167 *destination16 = table[*source]; | 163 *destination16 = table[*source]; |
| 164 ++source; |
| 165 ++destination16; |
| 166 |
| 167 while (source < end) { |
| 168 if (isASCII(*source)) { |
| 169 // Fast path for ASCII. Most Latin-1 text will be ASCII. |
| 170 if (isAlignedToMachineWord(source)) { |
| 171 while (source < alignedEnd) { |
| 172 MachineWord chunk = *reinterpret_cast_ptr<const MachineWord*>(source); |
| 173 |
| 174 if (!isAllASCII<LChar>(chunk)) |
| 175 goto useLookupTable16; |
| 176 |
| 177 copyASCIIMachineWord(destination16, source); |
| 178 source += sizeof(MachineWord); |
| 179 destination16 += sizeof(MachineWord); |
| 180 } |
| 181 |
| 182 if (source == end) |
| 183 break; |
| 184 } |
| 185 *destination16 = *source; |
| 186 } else { |
| 187 useLookupTable16: |
| 188 *destination16 = table[*source]; |
| 189 } |
| 190 |
| 168 ++source; | 191 ++source; |
| 169 ++destination16; | 192 ++destination16; |
| 170 | 193 } |
| 171 while (source < end) { | 194 |
| 172 if (isASCII(*source)) { | 195 return result16; |
| 173 // Fast path for ASCII. Most Latin-1 text will be ASCII. | 196 } |
| 174 if (isAlignedToMachineWord(source)) { | 197 |
| 175 while (source < alignedEnd) { | 198 template <typename CharType> |
| 176 MachineWord chunk = *reinterpret_cast_ptr<const MachineWord*
>(source); | 199 static CString encodeComplexWindowsLatin1(const CharType* characters, size_t len
gth, UnencodableHandling handling) { |
| 177 | 200 Vector<char> result(length); |
| 178 if (!isAllASCII<LChar>(chunk)) | 201 char* bytes = result.data(); |
| 179 goto useLookupTable16; | 202 |
| 180 | 203 size_t resultLength = 0; |
| 181 copyASCIIMachineWord(destination16, source); | 204 for (size_t i = 0; i < length;) { |
| 182 source += sizeof(MachineWord); | 205 UChar32 c; |
| 183 destination16 += sizeof(MachineWord); | 206 U16_NEXT(characters, i, length, c); |
| 184 } | 207 unsigned char b = static_cast<unsigned char>(c); |
| 185 | 208 // Do an efficient check to detect characters other than 00-7F and A0-FF. |
| 186 if (source == end) | 209 if (b != c || (c & 0xE0) == 0x80) { |
| 187 break; | 210 // Look for a way to encode this with Windows Latin-1. |
| 188 } | 211 for (b = 0x80; b < 0xA0; ++b) { |
| 189 *destination16 = *source; | 212 if (table[b] == c) |
| 190 } else { | 213 goto gotByte; |
| 191 useLookupTable16: | 214 } |
| 192 *destination16 = table[*source]; | 215 // No way to encode this character with Windows Latin-1. |
| 193 } | 216 UnencodableReplacementArray replacement; |
| 194 | 217 int replacementLength = TextCodec::getUnencodableReplacement(c, handling,
replacement); |
| 195 ++source; | 218 result.grow(resultLength + replacementLength + length - i); |
| 196 ++destination16; | 219 bytes = result.data(); |
| 197 } | 220 memcpy(bytes + resultLength, replacement, replacementLength); |
| 198 | 221 resultLength += replacementLength; |
| 199 return result16; | 222 continue; |
| 200 } | 223 } |
| 201 | 224 gotByte: |
| 202 template<typename CharType> | 225 bytes[resultLength++] = b; |
| 203 static CString encodeComplexWindowsLatin1(const CharType* characters, size_t len
gth, UnencodableHandling handling) | 226 } |
| 204 { | 227 |
| 205 Vector<char> result(length); | 228 return CString(bytes, resultLength); |
| 206 char* bytes = result.data(); | 229 } |
| 207 | 230 |
| 208 size_t resultLength = 0; | 231 template <typename CharType> |
| 209 for (size_t i = 0; i < length; ) { | 232 CString TextCodecLatin1::encodeCommon(const CharType* characters, size_t length,
UnencodableHandling handling) { |
| 210 UChar32 c; | 233 { |
| 211 U16_NEXT(characters, i, length, c); | 234 char* bytes; |
| 212 unsigned char b = static_cast<unsigned char>(c); | 235 CString string = CString::newUninitialized(length, bytes); |
| 213 // Do an efficient check to detect characters other than 00-7F and A0-FF
. | 236 |
| 214 if (b != c || (c & 0xE0) == 0x80) { | 237 // Convert the string a fast way and simultaneously do an efficient check to
see if it's all ASCII. |
| 215 // Look for a way to encode this with Windows Latin-1. | 238 UChar ored = 0; |
| 216 for (b = 0x80; b < 0xA0; ++b) { | 239 for (size_t i = 0; i < length; ++i) { |
| 217 if (table[b] == c) | 240 UChar c = characters[i]; |
| 218 goto gotByte; | 241 bytes[i] = static_cast<char>(c); |
| 219 } | 242 ored |= c; |
| 220 // No way to encode this character with Windows Latin-1. | 243 } |
| 221 UnencodableReplacementArray replacement; | 244 |
| 222 int replacementLength = TextCodec::getUnencodableReplacement(c, hand
ling, replacement); | 245 if (!(ored & 0xFF80)) |
| 223 result.grow(resultLength + replacementLength + length - i); | 246 return string; |
| 224 bytes = result.data(); | 247 } |
| 225 memcpy(bytes + resultLength, replacement, replacementLength); | 248 |
| 226 resultLength += replacementLength; | 249 // If it wasn't all ASCII, call the function that handles more-complex cases. |
| 227 continue; | 250 return encodeComplexWindowsLatin1(characters, length, handling); |
| 228 } | 251 } |
| 229 gotByte: | 252 |
| 230 bytes[resultLength++] = b; | 253 CString TextCodecLatin1::encode(const UChar* characters, size_t length, Unencoda
bleHandling handling) { |
| 231 } | 254 return encodeCommon(characters, length, handling); |
| 232 | 255 } |
| 233 return CString(bytes, resultLength); | 256 |
| 234 } | 257 CString TextCodecLatin1::encode(const LChar* characters, size_t length, Unencoda
bleHandling handling) { |
| 235 | 258 return encodeCommon(characters, length, handling); |
| 236 template<typename CharType> | 259 } |
| 237 CString TextCodecLatin1::encodeCommon(const CharType* characters, size_t length,
UnencodableHandling handling) | 260 |
| 238 { | 261 } // namespace WTF |
| 239 { | |
| 240 char* bytes; | |
| 241 CString string = CString::newUninitialized(length, bytes); | |
| 242 | |
| 243 // Convert the string a fast way and simultaneously do an efficient chec
k to see if it's all ASCII. | |
| 244 UChar ored = 0; | |
| 245 for (size_t i = 0; i < length; ++i) { | |
| 246 UChar c = characters[i]; | |
| 247 bytes[i] = static_cast<char>(c); | |
| 248 ored |= c; | |
| 249 } | |
| 250 | |
| 251 if (!(ored & 0xFF80)) | |
| 252 return string; | |
| 253 } | |
| 254 | |
| 255 // If it wasn't all ASCII, call the function that handles more-complex cases
. | |
| 256 return encodeComplexWindowsLatin1(characters, length, handling); | |
| 257 } | |
| 258 | |
| 259 CString TextCodecLatin1::encode(const UChar* characters, size_t length, Unencoda
bleHandling handling) | |
| 260 { | |
| 261 return encodeCommon(characters, length, handling); | |
| 262 } | |
| 263 | |
| 264 CString TextCodecLatin1::encode(const LChar* characters, size_t length, Unencoda
bleHandling handling) | |
| 265 { | |
| 266 return encodeCommon(characters, length, handling); | |
| 267 } | |
| 268 | |
| 269 } // namespace WTF | |
| OLD | NEW |