@kayahr/text-encoding
Version:
Text encoder and decoder
50 lines • 1.56 kB
JavaScript
/*
* Copyright (C) 2021 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*/
import { AbstractDecoder } from "../AbstractDecoder.js";
import { END_OF_BUFFER } from "../ByteBuffer.js";
import { FINISHED } from "../constants.js";
import { isASCII } from "../util.js";
/**
* Decoder for single byte encodings.
*/
export class SingleByteDecoder extends AbstractDecoder {
index;
/**
* Constructs new decoder for the given code points.
*
* @param codePoints - The code points of the encoding.
* @param fatal - True to throw an exception when a character can't be decoded. False to decode to a
* replacement character instead.
*/
constructor(codePoints, fatal) {
super(fatal);
this.index = codePoints;
}
/**
* Creates and returns a single byte decoder class for the given code points.
*
* @param codePoints - The code points of the encoding to decode.
* @returns The created single byte decoder class.
*/
static forCodePoints(codePoints) {
return class extends SingleByteDecoder {
constructor(fatal) {
super(codePoints, fatal);
}
};
}
/** @inheritdoc */
decode(buffer) {
const byte = buffer.read();
if (byte === END_OF_BUFFER) {
return FINISHED;
}
if (isASCII(byte)) {
return byte;
}
return this.index[byte - 0x80] ?? this.fail();
}
}
//# sourceMappingURL=SingleByteDecoder.js.map