Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.