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