Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/core/utils/StringProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { FlowchartEdge, FlowchartNode } from "../../ir/ir";
export class StringProcessor {
private static escapeCache = new Map<string, string>();
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<string, string> = {
'"': "#quot;",
"\\": "\\\\",
"\n": " ",
"\n": " ",
"<": "#60;",
">": "#62;",
"`": "#96;",
Expand All @@ -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
Expand All @@ -56,7 +59,6 @@ export class StringProcessor {
this.escapeCache.clear();
}
}

export interface ProcessResult {
nodes: FlowchartNode[];
edges: FlowchartEdge[];
Expand All @@ -68,4 +70,4 @@ export interface ProcessResult {
export interface LoopContext {
breakTargetId: string;
continueTargetId: string;
}
}