1
0

use string sections to reduce result accumulator size

This commit is contained in:
2024-05-19 01:44:41 +02:00
parent 21bd39426a
commit 6e43c1ceb3
2 changed files with 37 additions and 9 deletions

View File

@@ -77,10 +77,12 @@ class Substitutor {
} }
replace(): string { replace(): string {
const result: string[] = []; const result: (string | StringSection)[] = [];
let escaping = false; let escaping = false;
let hasSuffix = false; let hasSuffix = false;
let sectionOffset = -1;
let sectionLength = 0;
while (this.#pos < this.#input.length) { while (this.#pos < this.#input.length) {
const c = this.#input.charAt(this.#pos); const c = this.#input.charAt(this.#pos);
@@ -90,7 +92,21 @@ class Substitutor {
escaping = false; escaping = false;
} else if (c === this.#substitutor.escapeChar) { } else if (c === this.#substitutor.escapeChar) {
escaping = true; escaping = true;
if (sectionOffset >= 0) {
result.push(this.#input.subSection(sectionOffset, sectionLength));
sectionOffset = -1;
sectionLength = 0;
}
} else if (this.tryAdvance(this.#substitutor.pattern.prefix)) { } else if (this.tryAdvance(this.#substitutor.pattern.prefix)) {
if (sectionOffset >= 0) {
result.push(this.#input.subSection(sectionOffset, sectionLength));
sectionOffset = -1;
sectionLength = 0;
}
const innerSubstitutor = new Substitutor(this.#substitutor, this.#input.subSection(this.#pos), true); const innerSubstitutor = new Substitutor(this.#substitutor, this.#input.subSection(this.#pos), true);
const variable = innerSubstitutor.replace(); const variable = innerSubstitutor.replace();
@@ -111,22 +127,26 @@ class Substitutor {
this.#pos += innerSubstitutor.#pos; this.#pos += innerSubstitutor.#pos;
continue; continue;
} else if (this.tryAdvance(this.#substitutor.pattern.suffix)) { } else if (this.#inner && this.tryAdvance(this.#substitutor.pattern.suffix)) {
if (this.#inner) {
hasSuffix = true; hasSuffix = true;
break; break;
} else {
if (sectionOffset < 0) {
sectionOffset = this.#pos;
} }
throw new PlaceholderException(this.#input.getSection(), this.#pos, "Missing prefix"); sectionLength++;
} else {
result.push(c);
} }
this.#pos++; this.#pos++;
} }
if (sectionOffset >= 0) {
result.push(this.#input.subSection(sectionOffset, sectionLength));
}
if (this.#inner && !hasSuffix) { if (this.#inner && !hasSuffix) {
throw new PlaceholderException(this.#input.getSection(), this.#pos, "Missing suffix"); throw new PlaceholderException(this.#input.getSection(), this.#pos, "Missing variable end delimiter");
} }
return result.join(""); return result.join("");

View File

@@ -44,4 +44,12 @@ export class StringSection {
public substring(beginIndex: number = 0, endIndex: number = this.#length - beginIndex) { public substring(beginIndex: number = 0, endIndex: number = this.#length - beginIndex) {
return this.#parent.substring(this.#offset + beginIndex, this.#offset + endIndex); return this.#parent.substring(this.#offset + beginIndex, this.#offset + endIndex);
} }
public toJSON() {
return this.getSection();
}
public toString() {
return this.getSection();
}
} }