All files / app/src string-buffer.ts

100% Statements 31/31
100% Branches 6/6
100% Functions 10/10
100% Lines 30/30

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 631x           1x       36x 35x     1x 1x     1x 16x     1x 553250x 553250x 553250x     1x 83x     1x 158157x               1x 8x 4x     4x 4x 4x 4x 4x     1x 32x     1x 8x 4x   1x  
import AbstractCharBuffer from './abstract-char-buffer';
 
/**
 * {@link AbstractCharBuffer} implementation using a single {@link String}.
 */
 
export default class StringBuffer extends AbstractCharBuffer {
	private _buffer: string;
 
	public constructor(initCapacity: number) {
		super(initCapacity);
		this._buffer = '';
	}
 
	public static get isSupported(): boolean {
		return true;
	}
 
	public static fromString(string: string, transform?: (value: number, index: number, string: string) => number, thisArg?: any): StringBuffer {
		return super._fromString(new this(string.length), string, transform, thisArg);
	}
 
	public append(charCode: number): this {
		this._buffer += String.fromCharCode(charCode);
		this._length = this._buffer.length;
		return this;
	}
 
	public charAt(offset: number): string {
		return this._buffer.charAt(offset);
	}
 
	public charCodeAt(offset: number): number {
		return this._buffer.charCodeAt(offset);
	}
 
	/**
	 * Write a charCode to the buffer using {@link String#fromCharCode} and {@link String#concat +}.
	 * @param charCode
	 * @param offset
	 */
	public write(charCode: number, offset?: number): this {
		if (typeof offset === 'undefined' || offset === this._length) {
			return this.append(charCode);
		}
 
		const pre = this._buffer.slice(0, offset);
		const post = this._buffer.slice(offset + 1);
		this._buffer = pre + String.fromCharCode(charCode) + post;
		this._length = this._buffer.length;
		return this;
	}
 
	public toString(): string {
		return this._buffer;
	}
 
	protected setLength(newLength: number): void {
		super.setLength(newLength);
		this._buffer = this._buffer.slice(0, this.length);
	}
}