-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
26 lines (22 loc) · 1.12 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Package splitter - Go package for splitting strings (aware of enclosing braces and quotes)
/*
The problem with strings.Split is that it does not take into consideration that the string being split may
contain enclosing braces and/or quotes (where the separator should not be considered where it's inside braces or quotes)
Take for example a string representing a slice of comma separated strings...
str := `"aaa","bbb","this, for sanity, should not be split"`
running strings.Split on that...
str := `"aaa","bbb","this, for sanity, should not be split"`
parts := strings.Split(str, `,`)
println(len(parts))
would yield 5 - instead of the desired 3
With splitter, the result would be different...
str := `"aaa","bbb","this, for sanity, should not be split"`
mySplitter, _ := splitter.NewSplitter(',', splitter.DoubleQuotes)
parts, _ := mySplitter.Split(str)
println(len(parts))
which yields the desired 3!
Note: The varargs, after the first separator arg, are the desired 'enclosures' (e.g. quotes, brackets, etc.) to be taken
into consideration
While splitting, any enclosures specified are checked for balancing!
*/
package splitter