commit
10558becb7
|
@ -12,7 +12,7 @@ jobs:
|
|||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [10.x, 12.x, 14.x]
|
||||
node-version: [12.x, 14.x, 16x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
|
|
@ -7,10 +7,10 @@ export interface Recogniser {
|
|||
}
|
||||
|
||||
export interface Context {
|
||||
fByteStats: number[];
|
||||
fC1Bytes: boolean;
|
||||
fRawInput: Uint8Array;
|
||||
fRawLength: number;
|
||||
fInputBytes: Uint8Array;
|
||||
fInputLen: number;
|
||||
byteStats: number[];
|
||||
c1Bytes: boolean;
|
||||
rawInput: Uint8Array;
|
||||
rawLen: number;
|
||||
inputBytes: Uint8Array;
|
||||
inputLen: number;
|
||||
}
|
||||
|
|
|
@ -36,8 +36,8 @@ class ISO_2022 implements Recogniser {
|
|||
let quality;
|
||||
|
||||
// TODO: refactor me
|
||||
const text = det.fInputBytes;
|
||||
const textLen = det.fInputLen;
|
||||
const text = det.inputBytes;
|
||||
const textLen = det.inputLen;
|
||||
|
||||
scanInput: for (i = 0; i < textLen; i++) {
|
||||
if (text[i] == 0x1b) {
|
||||
|
|
|
@ -64,11 +64,11 @@ class IteratedChar {
|
|||
}
|
||||
|
||||
nextByte(det: Context) {
|
||||
if (this.nextIndex >= det.fRawLength) {
|
||||
if (this.nextIndex >= det.rawLen) {
|
||||
this.done = true;
|
||||
return -1;
|
||||
}
|
||||
const byteValue = det.fRawInput[this.nextIndex++] & 0x00ff;
|
||||
const byteValue = det.rawInput[this.nextIndex++] & 0x00ff;
|
||||
return byteValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,9 +58,9 @@ class NGramParser {
|
|||
}
|
||||
|
||||
nextByte(det: Context) {
|
||||
if (this.byteIndex >= det.fInputLen) return -1;
|
||||
if (this.byteIndex >= det.inputLen) return -1;
|
||||
|
||||
return det.fInputBytes[this.byteIndex++] & 0xff;
|
||||
return det.inputBytes[this.byteIndex++] & 0xff;
|
||||
}
|
||||
|
||||
parse(det: Context, spaceCh: number) {
|
||||
|
@ -1080,7 +1080,7 @@ export class ISO_8859_1 extends sbcs {
|
|||
}
|
||||
|
||||
name(input: Context): string {
|
||||
return input && input.fC1Bytes ? 'windows-1252' : 'ISO-8859-1';
|
||||
return input && input.c1Bytes ? 'windows-1252' : 'ISO-8859-1';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1616,7 +1616,7 @@ export class ISO_8859_2 extends sbcs {
|
|||
}
|
||||
|
||||
name(det: Context): string {
|
||||
return det && det.fC1Bytes ? 'windows-1250' : 'ISO-8859-2';
|
||||
return det && det.c1Bytes ? 'windows-1250' : 'ISO-8859-2';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2632,7 +2632,7 @@ export class ISO_8859_7 extends sbcs {
|
|||
}
|
||||
|
||||
name(det: Context): string {
|
||||
return det && det.fC1Bytes ? 'windows-1253' : 'ISO-8859-7';
|
||||
return det && det.c1Bytes ? 'windows-1253' : 'ISO-8859-7';
|
||||
}
|
||||
|
||||
language() {
|
||||
|
@ -3040,7 +3040,7 @@ export class ISO_8859_8 extends sbcs {
|
|||
}
|
||||
|
||||
name(det: Context): string {
|
||||
return det && det.fC1Bytes ? 'windows-1255' : 'ISO-8859-8';
|
||||
return det && det.c1Bytes ? 'windows-1255' : 'ISO-8859-8';
|
||||
}
|
||||
|
||||
language() {
|
||||
|
@ -3380,7 +3380,7 @@ export class ISO_8859_9 extends sbcs {
|
|||
}
|
||||
|
||||
name(det: Context): string {
|
||||
return det && det.fC1Bytes ? 'windows-1254' : 'ISO-8859-9';
|
||||
return det && det.c1Bytes ? 'windows-1254' : 'ISO-8859-9';
|
||||
}
|
||||
|
||||
language() {
|
||||
|
@ -4425,7 +4425,7 @@ module.exports.ISO_8859_7 = function() {
|
|||
this.name = function(det) {
|
||||
if (typeof det == 'undefined')
|
||||
return 'ISO-8859-7';
|
||||
return det.fC1Bytes ? 'windows-1253' : 'ISO-8859-7';
|
||||
return det.c1Bytes ? 'windows-1253' : 'ISO-8859-7';
|
||||
};
|
||||
|
||||
language() {
|
||||
|
|
|
@ -11,7 +11,7 @@ export class UTF_16BE implements Recogniser {
|
|||
}
|
||||
|
||||
match(det: Context): Match | null {
|
||||
const input = det.fRawInput;
|
||||
const input = det.rawInput;
|
||||
|
||||
if (
|
||||
input.length >= 2 &&
|
||||
|
@ -31,7 +31,7 @@ export class UTF_16LE implements Recogniser {
|
|||
return 'UTF-16LE';
|
||||
}
|
||||
match(det: Context): Match | null {
|
||||
const input = det.fRawInput;
|
||||
const input = det.rawInput;
|
||||
|
||||
if (
|
||||
input.length >= 2 &&
|
||||
|
@ -69,8 +69,8 @@ class UTF_32 implements Recogniser, WithGetChar {
|
|||
numInvalid = 0,
|
||||
hasBOM = false,
|
||||
confidence = 0;
|
||||
const limit = (det.fRawLength / 4) * 4;
|
||||
const input = det.fRawInput;
|
||||
const limit = (det.rawLen / 4) * 4;
|
||||
const input = det.rawInput;
|
||||
|
||||
if (limit == 0) {
|
||||
return null;
|
||||
|
|
|
@ -12,10 +12,10 @@ export default class Utf8 implements Recogniser {
|
|||
numInvalid = 0,
|
||||
trailBytes = 0,
|
||||
confidence;
|
||||
const input = det.fRawInput;
|
||||
const input = det.rawInput;
|
||||
|
||||
if (
|
||||
det.fRawLength >= 3 &&
|
||||
det.rawLen >= 3 &&
|
||||
(input[0] & 0xff) == 0xef &&
|
||||
(input[1] & 0xff) == 0xbb &&
|
||||
(input[2] & 0xff) == 0xbf
|
||||
|
@ -24,7 +24,7 @@ export default class Utf8 implements Recogniser {
|
|||
}
|
||||
|
||||
// Scan for multi-byte sequences
|
||||
for (let i = 0; i < det.fRawLength; i++) {
|
||||
for (let i = 0; i < det.rawLen; i++) {
|
||||
const b = input[i];
|
||||
if ((b & 0x80) == 0) continue; // ASCII
|
||||
|
||||
|
@ -44,7 +44,7 @@ export default class Utf8 implements Recogniser {
|
|||
// Verify that we've got the right number of trail bytes in the sequence
|
||||
for (;;) {
|
||||
i++;
|
||||
if (i >= det.fRawLength) break;
|
||||
if (i >= det.rawLen) break;
|
||||
|
||||
if ((input[i] & 0xc0) != 0x080) {
|
||||
numInvalid++;
|
||||
|
|
24
src/index.ts
24
src/index.ts
|
@ -50,26 +50,26 @@ export const detect = (buffer: Uint8Array): string | null => {
|
|||
|
||||
export const analyse = (buffer: Uint8Array): Match[] => {
|
||||
// Tally up the byte occurrence statistics.
|
||||
const fByteStats = [];
|
||||
for (let i = 0; i < 256; i++) fByteStats[i] = 0;
|
||||
const byteStats = [];
|
||||
for (let i = 0; i < 256; i++) byteStats[i] = 0;
|
||||
|
||||
for (let i = buffer.length - 1; i >= 0; i--) fByteStats[buffer[i] & 0x00ff]++;
|
||||
for (let i = buffer.length - 1; i >= 0; i--) byteStats[buffer[i] & 0x00ff]++;
|
||||
|
||||
let fC1Bytes = false;
|
||||
let c1Bytes = false;
|
||||
for (let i = 0x80; i <= 0x9f; i += 1) {
|
||||
if (fByteStats[i] !== 0) {
|
||||
fC1Bytes = true;
|
||||
if (byteStats[i] !== 0) {
|
||||
c1Bytes = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const context: Context = {
|
||||
fByteStats,
|
||||
fC1Bytes,
|
||||
fRawInput: buffer,
|
||||
fRawLength: buffer.length,
|
||||
fInputBytes: buffer,
|
||||
fInputLen: buffer.length,
|
||||
byteStats,
|
||||
c1Bytes,
|
||||
rawInput: buffer,
|
||||
rawLen: buffer.length,
|
||||
inputBytes: buffer,
|
||||
inputLen: buffer.length,
|
||||
};
|
||||
|
||||
const matches = recognisers
|
||||
|
|
Loading…
Reference in New Issue