Skip to content

Instantly share code, notes, and snippets.

@kyokomi
Created January 22, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyokomi/b845d349756c6b98fff05f8e43d6072d to your computer and use it in GitHub Desktop.
Save kyokomi/b845d349756c6b98fff05f8e43d6072d to your computer and use it in GitHub Desktop.
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
}
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