Skip to content

Commit

Permalink
remove CursorProxy
Browse files Browse the repository at this point in the history
it doesn't improve performance
  • Loading branch information
scoiatael authored and pastelmind committed Aug 18, 2022
1 parent 3b757d8 commit b8d4db7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
45 changes: 5 additions & 40 deletions packages/psd/src/engineData/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Section 7.2 - Lexical Conventions

import {
Cursor,
InvalidEngineDataBoolean,
InvalidEngineDataNumber,
InvalidEngineDataTextBOM,
Expand Down Expand Up @@ -63,50 +64,14 @@ const Delimiters = {

const DelimiterCharacters = new Set(Object.values(Delimiters));

class CursorProxy {
public position = 0;
constructor(private cursor: Uint8Array) {}

peek() {
return this.cursor[this.position];
}
pass(n: number) {
this.position += n;
}
one() {
const val = this.peek();
this.position += 1;
return val;
}
unpass() {
this.pass(-1);
}
get length() {
return this.cursor.length;
}
clone() {
const val = new CursorProxy(this.cursor);
val.position = this.position;
return val;
}
take(n: number) {
const val = this.cursor.subarray(this.position, this.position + n);
this.position += n;
return val;
}
iter() {
return this.cursor.subarray(this.position);
}
}

const STRING_TOKEN_JT = [] as boolean[];
for (let i = 0; i < 256; i += 1) {
STRING_TOKEN_JT[i] =
WhitespaceCharacters.has(i) || DelimiterCharacters.has(i);
}

const STRING_DECODER = new TextDecoder("utf-8");
function stringToken(cursor: CursorProxy): string {
function stringToken(cursor: Cursor): string {
const startsAt = cursor.position;
let endsAt = cursor.position;
for (const i of cursor.iter()) {
Expand All @@ -120,10 +85,10 @@ function stringToken(cursor: CursorProxy): string {
}

export class Lexer {
cursor: CursorProxy;
cursor: Cursor;

constructor(cursor: Uint8Array) {
this.cursor = new CursorProxy(cursor);
this.cursor = Cursor.from(cursor);
}

tokens(): Token[] {
Expand Down Expand Up @@ -174,7 +139,7 @@ export class Lexer {
}
// only two types left: number or boolean
// we need to return val first since it starts value
this.cursor.unpass();
this.cursor.unpass(1);
if (BooleanStartCharacters.has(val)) {
value.push({type: TokenType.Boolean, value: this.boolean()});
} else {
Expand Down
23 changes: 23 additions & 0 deletions packages/psd/src/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ const INCREASE: {[type in ReadType]: number} = {
* being parsed.
*/
export class Cursor {
static from(bytes: Uint8Array) {
return new Cursor(
new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
);
}

constructor(private dataView: DataView, public position = 0) {}

/**
Expand Down Expand Up @@ -145,6 +151,13 @@ export class Cursor {
);
}

iter(): Uint8Array {
return new Uint8Array(
this.dataView.buffer,
this.dataView.byteOffset + this.position
);
}

/**
* Creates a `Uint8Array` that covers the underlying `ArrayBuffer` of this
* cursor, starting at the current cursor position and spanning a
Expand All @@ -166,6 +179,16 @@ export class Cursor {
return this.dataView.getUint8(this.position);
}

/**
* Returns subsequent byte
*/
one(): number {
// dataView throws RangeError if position is outside bounds
const val = this.dataView.getUint8(this.position);
this.position += 1;
return val;
}

/**
* Reads a number at the current cursor position, using the given {@link type}
* (big endian).
Expand Down

0 comments on commit b8d4db7

Please sign in to comment.