Based on the work of others here I created my own version that passes the 
tests I needed to pass:

* strips outer quotes
* keeps inner quotes
* empty quotes produce empty string

https://play.golang.org/p/NzYUsZ-pm2v 

const NullStr = rune(0) 

 

// ParseArgs will parse a string that contains quoted strings the same as 
> bash does
> // (same as most other *nix shells do). This is secure in the sense that 
> it doesn't do any
> // executing or interpeting. However, it also doesn't do any escaping, so 
> you shouldn't pass
> // these strings to shells without escaping them.
> func ParseArgs(str string) ([]string, error) {
> var m []string
> var s string 

 

str = strings.TrimSpace(str) + " "
> lastQuote := NullStr
> isSpace := false 

 

for i, c := range str {
> switch {
> // If we're ending a quote, break out and skip this character
> case c == lastQuote:
> lastQuote = NullStr 

 

// If we're in a quote, count this character
> case lastQuote != NullStr:
> s += string(c) 

 

// If we encounter a quote, enter it and skip this character
> case unicode.In(c, unicode.Quotation_Mark):
> isSpace = false
> lastQuote = c 

 

// If it's a space, store the string
> case unicode.IsSpace(c):
> if 0 == i || isSpace {
> continue
> }
> isSpace = true
> m = append(m, s)
> s = "" 

 

default:
> isSpace = false
> s += string(c)
> }
> }
> if lastQuote != NullStr {
> return nil, fmt.Errorf("Quotes did not terminate")
> }
> return m, nil
> }


I may or may not keep this updated at 
https://git.coolaj86.com/coolaj86/git-scripts/src/branch/master/git-proxy

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/79d9733b-906b-4a28-af96-49257e48987a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to