fix(rules): remove empty segments from splitPipe output

Refactored splitPipe function to use forEachPipePart helper, which filters
out empty segments instead of including them in the result. Updated test
expectation to match the new behavior where empty parts between pipes
are no longer included.
This commit is contained in:
yusing
2026-02-24 02:52:19 +08:00
parent 7b0d846576
commit 95ffd35585
2 changed files with 5 additions and 40 deletions

View File

@@ -570,45 +570,10 @@ func splitPipe(s string) []string {
return []string{}
}
result := make([]string, 0, 2)
quote := byte(0)
brackets := 0
start := 0
for i := 0; i < len(s); i++ {
switch s[i] {
case '\\':
// Skip escaped character.
if i+1 < len(s) {
i++
}
case '"', '\'', '`':
if quote == 0 && brackets == 0 {
quote = s[i]
} else if s[i] == quote {
quote = 0
}
case '(':
brackets++
case ')':
if brackets > 0 {
brackets--
}
case '|':
if quote == 0 && brackets == 0 {
result = append(result, strings.TrimSpace(s[start:i]))
start = i + 1
}
}
}
// drop trailing empty part.
if start < len(s) {
if part := strings.TrimSpace(s[start:]); part != "" {
result = append(result, part)
}
}
result := []string{}
forEachPipePart(s, func(part string) {
result = append(result, part)
})
return result
}

View File

@@ -76,7 +76,7 @@ func TestSplitPipe(t *testing.T) {
{
name: "empty_segments",
input: "rule1 || rule2 | | rule3",
want: []string{"rule1", "", "rule2", "", "rule3"},
want: []string{"rule1", "rule2", "rule3"},
},
}
for _, tt := range tests {