(cmdline string)
| 7 | ) |
| 8 | |
| 9 | func Tokenize(cmdline string) []string { |
| 10 | var tokens []string |
| 11 | var token strings.Builder |
| 12 | inQuotes := false |
| 13 | runes := []rune(cmdline) |
| 14 | length := len(runes) |
| 15 | |
| 16 | for i := 0; i < length; { |
| 17 | c := runes[i] |
| 18 | |
| 19 | if c == ' ' && !inQuotes { |
| 20 | if token.Len() > 0 { |
| 21 | tokens = append(tokens, token.String()) |
| 22 | token.Reset() |
| 23 | } |
| 24 | i++ |
| 25 | continue |
| 26 | } |
| 27 | |
| 28 | if c == '"' { |
| 29 | inQuotes = !inQuotes |
| 30 | i++ |
| 31 | continue |
| 32 | } |
| 33 | |
| 34 | if c == '\\' { |
| 35 | numBS := 0 |
| 36 | for i < length && runes[i] == '\\' { |
| 37 | numBS++ |
| 38 | i++ |
| 39 | } |
| 40 | if i < length && runes[i] == '"' { |
| 41 | for j := 0; j < numBS/2; j++ { |
| 42 | token.WriteRune('\\') |
| 43 | } |
| 44 | if numBS%2 == 0 { |
| 45 | inQuotes = !inQuotes |
| 46 | } else { |
| 47 | token.WriteRune('"') |
| 48 | } |
| 49 | i++ |
| 50 | } else { |
| 51 | for j := 0; j < numBS; j++ { |
| 52 | token.WriteRune('\\') |
| 53 | } |
| 54 | } |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | token.WriteRune(c) |
| 59 | i++ |
| 60 | } |
| 61 | |
| 62 | if token.Len() > 0 { |
| 63 | tokens = append(tokens, token.String()) |
| 64 | } |
| 65 | |
| 66 | return tokens |
no test coverage detected