All files / src/abap/1_lexer lexer_buffer.ts

100% Statements 48/48
100% Branches 11/11
100% Functions 6/6
100% Lines 48/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11212x 11212x 11212x 11212x 11212x 1x 1x 1173024x 359005x 359005x 359005x 1173024x 1173024x 1x 1x 361722x 361722x 1x 1x 830574x 830574x 1x 1x 359005x 359005x 359005x 359005x 1x 1x 2894x 2894x 18295x 5855x 5855x 18295x 2894x 2894x 1x  
// The buffer holds a reference to the raw input and tracks the [start, end)
// offset range of the current token, so the token string can be sliced out with
// a single substring() instead of being concatenated one character at a time.
export class LexerBuffer {
  private readonly raw: string;
  private start: number;
  private end: number;   // exclusive
  private empty: boolean;
 
  public constructor(raw: string) {
    this.raw = raw;
    this.start = 0;
    this.end = 0;
    this.empty = true;
  }
 
  public add(offset: number): void {
    if (this.empty === true) {
      this.start = offset < 0 ? 0 : offset;
      this.empty = false;
    }
    this.end = offset + 1;
  }
 
  public get(): string {
    return this.raw.substring(this.start, this.end);
  }
 
  public length(): number {
    return this.end - this.start;
  }
 
  public clear(): void {
    this.start = 0;
    this.end = 0;
    this.empty = true;
  }
 
  public countIsEven(char: number): boolean {
    let count = 0;
    for (let i = this.start; i < this.end; i += 1) {
      if (this.raw.charCodeAt(i) === char) {
        count += 1;
      }
    }
    return count % 2 === 0;
  }
}