Skip to content

Commit 743856d

Browse files
refactor(parser): mirror upstream Params.resetZdm helper
Adds a single ResetZdm() method on Params that performs the common Reset() + AddParam(0) sequence used at every CSI/OSC/DCS/APC clear boundary. Behavior is unchanged; this only collapses the duplicated two-call pattern in the parser to match the upstream xterm.js performance refactor introduced after gitHead 09116882. Fixes #18 Co-authored-by: Ona <no-reply@ona.com>
1 parent f96d68e commit 743856d

3 files changed

Lines changed: 79 additions & 10 deletions

File tree

parser.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ func (p *EscapeSequenceParser) Reset() {
316316
p.oscParser.Reset()
317317
p.dcsParser.Reset()
318318
p.apcParser.Reset()
319-
p.params.Reset()
320-
p.params.AddParam(0) // ZDM
319+
p.params.ResetZdm()
321320
p.collect = 0
322321
p.precedingJoinState = 0
323322
}
@@ -441,8 +440,7 @@ func (p *EscapeSequenceParser) Parse(data []uint32, length int) {
441440
p.precedingJoinState = 0
442441

443442
case ParserActionClear:
444-
p.params.Reset()
445-
p.params.AddParam(0) // ZDM
443+
p.params.ResetZdm()
446444
p.collect = 0
447445

448446
case ParserActionDCSHook:
@@ -465,8 +463,7 @@ func (p *EscapeSequenceParser) Parse(data []uint32, length int) {
465463
if code == 0x1b {
466464
transition |= uint16(ParserStateEscape)
467465
}
468-
p.params.Reset()
469-
p.params.AddParam(0) // ZDM
466+
p.params.ResetZdm()
470467
p.collect = 0
471468
p.precedingJoinState = 0
472469

@@ -490,8 +487,7 @@ func (p *EscapeSequenceParser) Parse(data []uint32, length int) {
490487
if code == 0x1b {
491488
transition |= uint16(ParserStateEscape)
492489
}
493-
p.params.Reset()
494-
p.params.AddParam(0) // ZDM
490+
p.params.ResetZdm()
495491
p.collect = 0
496492
p.precedingJoinState = 0
497493

@@ -515,8 +511,7 @@ func (p *EscapeSequenceParser) Parse(data []uint32, length int) {
515511
if code == 0x1b {
516512
transition |= uint16(ParserStateEscape)
517513
}
518-
p.params.Reset()
519-
p.params.AddParam(0) // ZDM
514+
p.params.ResetZdm()
520515
p.collect = 0
521516
p.precedingJoinState = 0
522517
}

parser_params.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func (p *Params) Reset() {
6060
p.digitIsSub = false
6161
}
6262

63+
// ResetZdm resets the params and seeds a single zero-default param (ZDM).
64+
// Equivalent to Reset() followed by AddParam(0); kept as a single call to
65+
// mirror the upstream xterm.js Params.resetZdm helper.
66+
func (p *Params) ResetZdm() {
67+
p.Reset()
68+
p.AddParam(0)
69+
}
70+
6371
// AddParam appends a parameter value. Values < -1 panic. Values beyond
6472
// maxLength are silently ignored. Values exceeding maxParamValue are clamped.
6573
func (p *Params) AddParam(value int32) {

parser_params_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,69 @@ func TestParamsGetSubParamsAll(t *testing.T) {
392392
t.Errorf("(-want +got):\n%s", diff)
393393
}
394394
}
395+
396+
func TestParamsResetZdm(t *testing.T) {
397+
t.Parallel()
398+
type Expectation struct {
399+
Length int
400+
Values []int32
401+
}
402+
type TestCase struct {
403+
Name string
404+
Setup func(p *Params)
405+
Expected Expectation
406+
}
407+
tests := []TestCase{
408+
{
409+
Name: "fresh params seeded with zero default",
410+
Setup: func(p *Params) {},
411+
Expected: Expectation{Length: 1, Values: []int32{0}},
412+
},
413+
{
414+
Name: "previously populated params collapse to single zero",
415+
Setup: func(p *Params) {
416+
p.AddParam(5)
417+
p.AddSubParam(7)
418+
p.AddParam(9)
419+
},
420+
Expected: Expectation{Length: 1, Values: []int32{0}},
421+
},
422+
{
423+
Name: "matches Reset followed by AddParam(0)",
424+
Setup: func(p *Params) {
425+
p.AddParam(42)
426+
p.AddSubParam(1)
427+
p.AddSubParam(2)
428+
},
429+
Expected: Expectation{Length: 1, Values: []int32{0}},
430+
},
431+
}
432+
for _, tc := range tests {
433+
t.Run(tc.Name, func(t *testing.T) {
434+
t.Parallel()
435+
p := DefaultParams()
436+
tc.Setup(p)
437+
p.ResetZdm()
438+
got := Expectation{
439+
Length: p.Length,
440+
Values: append([]int32{}, p.Params[:p.Length]...),
441+
}
442+
if diff := cmp.Diff(tc.Expected, got); diff != "" {
443+
t.Errorf("(-want +got):\n%s", diff)
444+
}
445+
446+
// Confirm equivalence with Reset() + AddParam(0).
447+
ref := DefaultParams()
448+
tc.Setup(ref)
449+
ref.Reset()
450+
ref.AddParam(0)
451+
if diff := cmp.Diff(ref.Params[:ref.Length], p.Params[:p.Length]); diff != "" {
452+
t.Errorf("ResetZdm not equivalent to Reset+AddParam(0) (-ref +zdm):\n%s", diff)
453+
}
454+
if ref.Length != p.Length {
455+
t.Errorf("Length mismatch: ref=%d zdm=%d", ref.Length, p.Length)
456+
}
457+
})
458+
}
459+
}
460+

0 commit comments

Comments
 (0)