Skip to content

Instantly share code, notes, and snippets.

@matthewmueller
Created February 16, 2018 21:26
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 matthewmueller/5393a1243108dfde20b5b5a5c3d4aee1 to your computer and use it in GitHub Desktop.
Save matthewmueller/5393a1243108dfde20b5b5a5c3d4aee1 to your computer and use it in GitHub Desktop.
Nice little function to split by slashes while ignoring slashes inside quotes (e.g. /Invoices/"Invoice (9/1)"/invoice.txt)
func splitBySlash(s string) []string {
lastQuote := rune(0)
slash := rune(filepath.Separator)
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 c == slash
}
}
return strings.FieldsFunc(s, f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment