| 16 | * @returns {StringIterator} |
| 17 | */ |
| 18 | export class StringIterator { |
| 19 | private curIndex: number; |
| 20 | private newLines: number; |
| 21 | private str: String |
| 22 | |
| 23 | constructor(str: string) { |
| 24 | this.curIndex = 0; |
| 25 | this.str = str |
| 26 | this.newLines = str.split('\n').length - 1; |
| 27 | } |
| 28 | remaining = () => this.str.length - this.curIndex; |
| 29 | |
| 30 | getnewLines = () => this.newLines; |
| 31 | |
| 32 | assertRemaining = (n: number) => { |
| 33 | assert(n <= this.remaining(), `!(${n} <= ${this.remaining()})`); |
| 34 | } |
| 35 | |
| 36 | take = (n: number) => { |
| 37 | this.assertRemaining(n); |
| 38 | const s = this.str.substring(this.curIndex, this.curIndex+n); |
| 39 | this.newLines -= s.split('\n').length - 1; |
| 40 | this.curIndex += n; |
| 41 | return s; |
| 42 | } |
| 43 | |
| 44 | peek = (n: number) => { |
| 45 | this.assertRemaining(n); |
| 46 | return this.str.substring(this.curIndex, this.curIndex+n); |
| 47 | } |
| 48 | |
| 49 | skip = (n: number) => { |
| 50 | this.assertRemaining(n); |
| 51 | this.curIndex += n; |
| 52 | } |
| 53 | |
| 54 | } |