(encoding, fatal)
| 106 | const decodersFatal = new SafeMap(); |
| 107 | |
| 108 | function createSinglebyteDecoder(encoding, fatal) { |
| 109 | const id = encoding === 'iso-8859-8-i' ? 'iso-8859-8' : encoding; |
| 110 | const decoders = fatal ? decodersFatal : decodersLoose; |
| 111 | const cached = decoders.get(id); |
| 112 | if (cached) return cached; |
| 113 | |
| 114 | const map = getEncoding(id); |
| 115 | const incomplete = TypedArrayPrototypeIncludes(map, r); |
| 116 | |
| 117 | // Expects type-checked Buffer input |
| 118 | const decoder = (buf) => { |
| 119 | if (buf.byteLength === 0) return ''; |
| 120 | if (isAscii(buf)) return buf.latin1Slice(); // .latin1Slice is faster than .asciiSlice |
| 121 | const o = new Uint16Array(buf.length); |
| 122 | TypedArrayPrototypeSet(o, buf); // Copy to modify in-place, also those are 16-bit now |
| 123 | |
| 124 | let i = 0; |
| 125 | for (const end7 = o.length - 7; i < end7; i += 8) { |
| 126 | o[i] = map[o[i]]; |
| 127 | o[i + 1] = map[o[i + 1]]; |
| 128 | o[i + 2] = map[o[i + 2]]; |
| 129 | o[i + 3] = map[o[i + 3]]; |
| 130 | o[i + 4] = map[o[i + 4]]; |
| 131 | o[i + 5] = map[o[i + 5]]; |
| 132 | o[i + 6] = map[o[i + 6]]; |
| 133 | o[i + 7] = map[o[i + 7]]; |
| 134 | } |
| 135 | |
| 136 | for (const end = o.length; i < end; i++) o[i] = map[o[i]]; |
| 137 | |
| 138 | const b = new FastBuffer(o.buffer, o.byteOffset, o.byteLength); |
| 139 | if (isBigEndian) b.swap16(); |
| 140 | const string = b.ucs2Slice(); |
| 141 | if (fatal && incomplete && StringPrototypeIncludes(string, '\uFFFD')) { |
| 142 | throw new ERR_ENCODING_INVALID_ENCODED_DATA(encoding, undefined); |
| 143 | } |
| 144 | return string; |
| 145 | }; |
| 146 | |
| 147 | decoders.set(id, decoder); |
| 148 | return decoder; |
| 149 | } |
| 150 | |
| 151 | module.exports = { |
| 152 | isSinglebyteEncoding, |
no test coverage detected
searching dependent graphs…