diff --git a/src/core/utils/StringProcessor.ts b/src/core/utils/StringProcessor.ts index 7c5ce83..8572bc6 100644 --- a/src/core/utils/StringProcessor.ts +++ b/src/core/utils/StringProcessor.ts @@ -3,14 +3,12 @@ import { FlowchartEdge, FlowchartNode } from "../../ir/ir"; export class StringProcessor { private static escapeCache = new Map(); private static readonly MAX_CACHE_SIZE = 1000; - - // Precompiled regex for better performance private static readonly escapeRegex = /"|\\|\n|<|>|`/g; private static readonly colonRegex = /:$/; private static readonly escapeMap: Record = { '"': "#quot;", "\\": "\\\\", - "\n": " ", + "\n": " ", "<": "#60;", ">": "#62;", "`": "#96;", @@ -22,24 +20,29 @@ export class StringProcessor { // Check cache first const cached = this.escapeCache.get(str); if (cached !== undefined) { - // Move to end for LRU behavior this.escapeCache.delete(str); this.escapeCache.set(str, cached); return cached; } - // Use LRU eviction instead of clearing entire cache + // LRU eviction if (this.escapeCache.size >= this.MAX_CACHE_SIZE) { const firstKey = this.escapeCache.keys().next().value; if (firstKey !== undefined) { this.escapeCache.delete(firstKey); } } + let processed = str; + + processed = processed.replace(/[\r\n\t]+/g, ' '); + processed = processed.replace(/\s+/g, ' '); + processed = processed.trim(); - let escaped = str.replace( + let escaped = processed.replace( this.escapeRegex, (match) => this.escapeMap[match] ); + escaped = escaped.replace(this.colonRegex, "").trim(); // Length limiting for readability @@ -56,7 +59,6 @@ export class StringProcessor { this.escapeCache.clear(); } } - export interface ProcessResult { nodes: FlowchartNode[]; edges: FlowchartEdge[]; @@ -68,4 +70,4 @@ export interface ProcessResult { export interface LoopContext { breakTargetId: string; continueTargetId: string; -} +} \ No newline at end of file