Created
February 16, 2018 21:26
-
-
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)
This file contains 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 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