Skip to content

Commit e8d9fd9

Browse files
committed
2 parents e22e042 + 9f725a3 commit e8d9fd9

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/core/utils/StringProcessor.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import { FlowchartEdge, FlowchartNode } from "../../ir/ir";
33
export class StringProcessor {
44
private static escapeCache = new Map<string, string>();
55
private static readonly MAX_CACHE_SIZE = 1000;
6-
7-
// Precompiled regex for better performance
86
private static readonly escapeRegex = /"|\\|\n|<|>|`/g;
97
private static readonly colonRegex = /:$/;
108
private static readonly escapeMap: Record<string, string> = {
119
'"': "#quot;",
1210
"\\": "\\\\",
13-
"\n": " ",
11+
"\n": " ",
1412
"<": "#60;",
1513
">": "#62;",
1614
"`": "#96;",
@@ -22,24 +20,29 @@ export class StringProcessor {
2220
// Check cache first
2321
const cached = this.escapeCache.get(str);
2422
if (cached !== undefined) {
25-
// Move to end for LRU behavior
2623
this.escapeCache.delete(str);
2724
this.escapeCache.set(str, cached);
2825
return cached;
2926
}
3027

31-
// Use LRU eviction instead of clearing entire cache
28+
// LRU eviction
3229
if (this.escapeCache.size >= this.MAX_CACHE_SIZE) {
3330
const firstKey = this.escapeCache.keys().next().value;
3431
if (firstKey !== undefined) {
3532
this.escapeCache.delete(firstKey);
3633
}
3734
}
35+
let processed = str;
36+
37+
processed = processed.replace(/[\r\n\t]+/g, ' ');
38+
processed = processed.replace(/\s+/g, ' ');
39+
processed = processed.trim();
3840

39-
let escaped = str.replace(
41+
let escaped = processed.replace(
4042
this.escapeRegex,
4143
(match) => this.escapeMap[match]
4244
);
45+
4346
escaped = escaped.replace(this.colonRegex, "").trim();
4447

4548
// Length limiting for readability
@@ -56,7 +59,6 @@ export class StringProcessor {
5659
this.escapeCache.clear();
5760
}
5861
}
59-
6062
export interface ProcessResult {
6163
nodes: FlowchartNode[];
6264
edges: FlowchartEdge[];
@@ -68,4 +70,4 @@ export interface ProcessResult {
6870
export interface LoopContext {
6971
breakTargetId: string;
7072
continueTargetId: string;
71-
}
73+
}

0 commit comments

Comments
 (0)