Created
January 22, 2017 14:42
-
-
Save kyokomi/b845d349756c6b98fff05f8e43d6072d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var quotationsReplacer = strings.NewReplacer("'", "", `"`, "") | |
func quotationOrSpaceFields(s string) []string { | |
lastQuote := rune(0) | |
f := func(c rune) bool { | |
switch { | |
case c == lastQuote: | |
lastQuote = rune(0) | |
return false | |
case lastQuote != rune(0): | |
return false | |
case unicode.In(c, unicode.Quotation_Mark): | |
lastQuote = c | |
return false | |
default: | |
return unicode.IsSpace(c) | |
} | |
} | |
args := strings.FieldsFunc(s, f) | |
for i := range args { | |
args[i] = quotationsReplacer.Replace(args[i]) | |
} | |
return args | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Test_quotationOrSpaceFields(t *testing.T) { | |
as := assert.New(t) | |
type testCase struct { | |
s string | |
args []string | |
} | |
testCases := []testCase{ | |
{ | |
s: `#random 'Create Task.*'`, | |
args: []string{"#random", "Create Task.*"}, | |
}, | |
{ | |
s: `#random "Create Task.*"`, | |
args: []string{"#random", "Create Task.*"}, | |
}, | |
} | |
for i, ts := range testCases { | |
as.Equal(quotationOrSpaceFields(ts.s), ts.args, "testCase [%d]", i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment