Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

72 lines
1.4 KiB
Go

package indexer
// shlexSplit tokenizes a POSIX-shell-style command line into arguments,
// honoring single quotes (fully literal), double quotes (with `\"` / `\\`
// escapes), and bare backslash escapes; unquoted whitespace separates tokens.
// It is intentionally minimal — enough to recover `-I"a b/inc"` and
// `-isystem /x` from a compile_commands.json `command` string when the
// structured `arguments` array is absent.
func shlexSplit(s string) []string {
const (
none = iota
single
double
)
var out []string
var cur []rune
inTok := false
quote := none
rs := []rune(s)
flush := func() {
if inTok {
out = append(out, string(cur))
cur = cur[:0]
inTok = false
}
}
for i := 0; i < len(rs); i++ {
c := rs[i]
switch quote {
case single:
if c == '\'' {
quote = none
} else {
cur = append(cur, c)
}
continue
case double:
switch {
case c == '\\' && i+1 < len(rs) && (rs[i+1] == '"' || rs[i+1] == '\\'):
i++
cur = append(cur, rs[i])
case c == '"':
quote = none
default:
cur = append(cur, c)
}
continue
}
switch c {
case ' ', '\t', '\n', '\r':
flush()
case '\'':
quote = single
inTok = true
case '"':
quote = double
inTok = true
case '\\':
if i+1 < len(rs) {
i++
cur = append(cur, rs[i])
}
inTok = true
default:
cur = append(cur, c)
inTok = true
}
}
flush()
return out
}