Handle no-whitespace-after-a-number

This commit is contained in:
Tony Garnock-Jones 2022-01-12 02:02:11 +01:00
parent fb6a5caf38
commit 3cc875bce3
2 changed files with 11 additions and 4 deletions

View File

@ -148,6 +148,7 @@ export class ReaderState {
readDigit0(kind: IntOrFloat, acc: string, k: IntContinuation): Numeric {
while (true) {
if (this.atEnd()) break;
const ch = this.peek();
if (!(ch >= '0' && ch <= '9')) break;
this.advance();
@ -157,7 +158,7 @@ export class ReaderState {
}
readFracexp(kind: IntOrFloat, acc: string): Numeric {
if (this.peek() === '.') {
if (!this.atEnd() && this.peek() === '.') {
this.advance();
return this.readDigit1('float', acc + '.', (kind, acc) => this.readExp(kind, acc));
}
@ -165,7 +166,7 @@ export class ReaderState {
}
readExp(kind: IntOrFloat, acc: string): Numeric {
const ch = this.peek();
const ch = this.atEnd() ? '' : this.peek();
if (ch === 'e' || ch === 'E') {
this.advance();
return this.readSignAndExp(acc + ch);
@ -185,7 +186,7 @@ export class ReaderState {
finishNumber(kind: IntOrFloat, acc: string): Numeric {
const i = parseFloat(acc);
if (kind === 'int') return i;
const ch = this.peek();
const ch = this.atEnd() ? '' : this.peek();
if (ch === 'f' || ch === 'F') {
this.advance();
return Single(i);

View File

@ -1,4 +1,4 @@
import { Bytes, Decoder, genericEmbeddedType, encode, Reader } from '../src/index';
import { Bytes, Decoder, genericEmbeddedType, encode, Reader, Double } from '../src/index';
import './test-utils';
import * as fs from 'fs';
@ -28,4 +28,10 @@ describe('reading common test suite', () => {
}));
expect(bs).toEqual(new Uint8Array(samples_bin));
});
it('should be ok with no whitespace at the end of a numeric input', () => {
expect(new Reader('123').next()).toEqual(123);
expect(new Reader('123.0').next()).toEqual(Double(123.0));
expect(new Reader('123.00').next()).toEqual(Double(123.0));
});
});