60 lines
1.7 KiB
Plaintext
60 lines
1.7 KiB
Plaintext
|
|
// Minimal Pattern P5b (Escape Handling) Test Fixture
|
||
|
|
// Purpose: Verify JoinIR Canonicalizer recognition of escape sequence patterns
|
||
|
|
//
|
||
|
|
// Pattern: loop(i < n) with conditional increment on escape character
|
||
|
|
// Carriers: i (position), out (accumulator)
|
||
|
|
// Exit: break on quote character
|
||
|
|
//
|
||
|
|
// This pattern is common in string parsing:
|
||
|
|
// - JSON string readers
|
||
|
|
// - CSV parsers
|
||
|
|
// - Template engines
|
||
|
|
// - Escape sequence handlers
|
||
|
|
|
||
|
|
static box Main {
|
||
|
|
console: ConsoleBox
|
||
|
|
|
||
|
|
main() {
|
||
|
|
me.console = new ConsoleBox()
|
||
|
|
|
||
|
|
// Test data: string with escape sequence
|
||
|
|
// Original: "hello\" world"
|
||
|
|
// After parsing: hello" world
|
||
|
|
local s = "hello\\\" world"
|
||
|
|
local n = s.length()
|
||
|
|
local i = 0
|
||
|
|
local out = ""
|
||
|
|
|
||
|
|
// Pattern P5b: Escape sequence handling loop
|
||
|
|
// - Header: loop(i < n)
|
||
|
|
// - Escape check: if ch == "\\" { i = i + 1 }
|
||
|
|
// - Process: out = out + ch
|
||
|
|
// - Update: i = i + 1
|
||
|
|
loop(i < n) {
|
||
|
|
local ch = s.substring(i, i + 1)
|
||
|
|
|
||
|
|
// Break on quote (string boundary)
|
||
|
|
if ch == "\"" {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handle escape sequence: skip the escape char itself
|
||
|
|
if ch == "\\" {
|
||
|
|
i = i + 1 // Skip escape character (i increments by +2 total with final i++)
|
||
|
|
if i < n {
|
||
|
|
ch = s.substring(i, i + 1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Accumulate processed character
|
||
|
|
out = out + ch
|
||
|
|
i = i + 1 // Standard increment
|
||
|
|
}
|
||
|
|
|
||
|
|
// Expected output: hello" world (escape removed)
|
||
|
|
me.console.log(out)
|
||
|
|
|
||
|
|
return "OK"
|
||
|
|
}
|
||
|
|
}
|