From 0efa67908c1c28ef6f24bc85df019ef4e233a477 Mon Sep 17 00:00:00 2001 From: Copilot Date: Thu, 19 Feb 2026 19:04:34 +0000 Subject: [PATCH] refactor: remove duplicate CSI helper functions from parser package The refactoring in #16892 moved the ANSI stripping implementation to pkg/stringutil/ansi.go and created a thin wrapper in pkg/parser/ansi_strip.go. However, the private helper functions isFinalCSIChar and isCSIParameterChar were accidentally left in pkg/parser/ansi_strip.go as duplicates. These helpers are already defined in pkg/stringutil/ansi.go (their canonical location) and are only used internally by stringutil.StripANSI. The parser package never uses them directly - it delegates to stringutil.StripANSI via the wrapper. Remove the duplicate helper functions and their associated tests from the parser package to eliminate the code duplication. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pkg/parser/ansi_strip.go | 13 ----- pkg/parser/frontmatter_utils_test.go | 79 ---------------------------- 2 files changed, 92 deletions(-) diff --git a/pkg/parser/ansi_strip.go b/pkg/parser/ansi_strip.go index ad905765621..a8d911ca9fa 100644 --- a/pkg/parser/ansi_strip.go +++ b/pkg/parser/ansi_strip.go @@ -10,16 +10,3 @@ import ( func StripANSI(s string) string { return stringutil.StripANSI(s) } - -// isFinalCSIChar checks if a character is a valid CSI final character -// Final characters are in range 0x40-0x7E (@-~) -func isFinalCSIChar(b byte) bool { - return b >= 0x40 && b <= 0x7E -} - -// isCSIParameterChar checks if a character is a valid CSI parameter or intermediate character -// Parameter characters are in range 0x30-0x3F (0-?) -// Intermediate characters are in range 0x20-0x2F (space-/) -func isCSIParameterChar(b byte) bool { - return (b >= 0x20 && b <= 0x2F) || (b >= 0x30 && b <= 0x3F) -} diff --git a/pkg/parser/frontmatter_utils_test.go b/pkg/parser/frontmatter_utils_test.go index 2cd814a4edb..2a18f9c1880 100644 --- a/pkg/parser/frontmatter_utils_test.go +++ b/pkg/parser/frontmatter_utils_test.go @@ -563,85 +563,6 @@ func TestStripANSI(t *testing.T) { } } -// Test isCSIParameterChar function -func TestIsCSIParameterChar(t *testing.T) { - tests := []struct { - name string - char byte - expected bool - }{ - // Valid parameter characters (0x30-0x3F, 0-?) - {name: "0 (0x30)", char: '0', expected: true}, - {name: "9 (0x39)", char: '9', expected: true}, - {name: "; (0x3B)", char: ';', expected: true}, - {name: "? (0x3F)", char: '?', expected: true}, - - // Valid intermediate characters (0x20-0x2F, space-/) - {name: "space (0x20)", char: ' ', expected: true}, - {name: "! (0x21)", char: '!', expected: true}, - {name: "/ (0x2F)", char: '/', expected: true}, - - // Invalid characters (below 0x20) - {name: "tab (0x09)", char: '\t', expected: false}, - {name: "newline (0x0A)", char: '\n', expected: false}, - {name: "null (0x00)", char: 0x00, expected: false}, - - // Invalid characters (above 0x3F) - {name: "@ (0x40)", char: '@', expected: false}, - {name: "A (0x41)", char: 'A', expected: false}, - {name: "m (0x6D)", char: 'm', expected: false}, - {name: "~ (0x7E)", char: '~', expected: false}, - {name: "DEL (0x7F)", char: 0x7F, expected: false}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := isCSIParameterChar(tt.char) - if result != tt.expected { - t.Errorf("isCSIParameterChar(%q/0x%02X) = %v, want %v", tt.char, tt.char, result, tt.expected) - } - }) - } -} - -// Test isFinalCSIChar function -func TestIsFinalCSIChar(t *testing.T) { - tests := []struct { - name string - char byte - expected bool - }{ - // Valid final characters (0x40-0x7E, @-~) - {name: "@ (0x40)", char: '@', expected: true}, - {name: "A (0x41)", char: 'A', expected: true}, - {name: "Z (0x5A)", char: 'Z', expected: true}, - {name: "a (0x61)", char: 'a', expected: true}, - {name: "m (0x6D)", char: 'm', expected: true}, // Common color final char - {name: "~ (0x7E)", char: '~', expected: true}, - - // Invalid characters (below 0x40) - {name: "space (0x20)", char: ' ', expected: false}, - {name: "0 (0x30)", char: '0', expected: false}, - {name: "9 (0x39)", char: '9', expected: false}, - {name: "; (0x3B)", char: ';', expected: false}, - {name: "? (0x3F)", char: '?', expected: false}, - - // Invalid characters (above 0x7E) - {name: "DEL (0x7F)", char: 0x7F, expected: false}, - {name: "high byte (0x80)", char: 0x80, expected: false}, - {name: "high byte (0xFF)", char: 0xFF, expected: false}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := isFinalCSIChar(tt.char) - if result != tt.expected { - t.Errorf("isFinalCSIChar(%q/0x%02X) = %v, want %v", tt.char, tt.char, result, tt.expected) - } - }) - } -} - // Benchmark StripANSI function for performance func BenchmarkStripANSI(b *testing.B) { testCases := []struct {