Created
October 20, 2021 18:35
-
-
Save jclem/309cb1dfe843ea6096fd41e0f03b78da to your computer and use it in GitHub Desktop.
string-scanner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Scans over a string, character by character | |
*/ | |
export default class StringScanner { | |
pos = 0 | |
constructor(private readonly data: string) {} | |
/** | |
* Fetch the next character. | |
*/ | |
next(): string | undefined { | |
return this.data[this.pos++] | |
} | |
/** | |
* Peek at the next character. | |
*/ | |
peek(): string | undefined { | |
return this.data[this.pos + 1] | |
} | |
/** | |
* Rewind the position by 1. | |
*/ | |
rewind(): void { | |
const newPos = this.pos - 1 | |
this.pos = newPos < 0 ? 0 : newPos | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment