updated default tokenizer separators

This commit is contained in:
Gani Georgiev 2023-02-06 16:30:47 +02:00
parent 23dfa9c634
commit f475967a4a
2 changed files with 20 additions and 20 deletions

View File

@ -19,7 +19,7 @@ import (
const eof = rune(0) const eof = rune(0)
// DefaultSeparators is a list with the default token separator characters. // DefaultSeparators is a list with the default token separator characters.
var DefaultSeparators = []rune{',', ' ', '\t', '\n'} var DefaultSeparators = []rune{','}
// NewFromString creates new Tokenizer from the provided string. // NewFromString creates new Tokenizer from the provided string.
func NewFromString(str string) *Tokenizer { func NewFromString(str string) *Tokenizer {
@ -31,7 +31,7 @@ func NewFromBytes(b []byte) *Tokenizer {
return New(bytes.NewReader(b)) return New(bytes.NewReader(b))
} }
// New creates new Tokenizer from the provided reader. // New creates new Tokenizer from the provided reader with DefaultSeparators.
func New(r io.Reader) *Tokenizer { func New(r io.Reader) *Tokenizer {
return &Tokenizer{ return &Tokenizer{
r: bufio.NewReader(r), r: bufio.NewReader(r),

View File

@ -54,7 +54,7 @@ func TestFactories(t *testing.T) {
} }
func TestScan(t *testing.T) { func TestScan(t *testing.T) {
tk := NewFromString("abc 123.456 (abc)") tk := NewFromString("abc, 123.456, (abc)")
expectedTokens := []string{"abc", "123.456", "(abc)"} expectedTokens := []string{"abc", "123.456", "(abc)"}
@ -79,7 +79,7 @@ func TestScan(t *testing.T) {
} }
} }
func TestScanAllWithDefaultSeparators(t *testing.T) { func TestScanAll(t *testing.T) {
scenarios := []struct { scenarios := []struct {
name string name string
content string content string
@ -119,10 +119,24 @@ func TestScanAllWithDefaultSeparators(t *testing.T) {
}, },
{ {
"default separators", "default separators",
`a, b, c, d e, "a,b, c ", (123, 456)`,
DefaultSeparators,
false,
[]string{
"a",
"b",
"c",
"d e",
`"a,b, c "`,
`(123, 456)`,
},
},
{
"custom separators",
` a , 123.456, b, c d, ( ` a , 123.456, b, c d, (
test (a,b,c) " 123 " test (a,b,c) " 123 "
),"(abc d", "abc) d", "(abc) d \" " 'abc "'`, ),"(abc d", "abc) d", "(abc) d \" " 'abc "'`,
DefaultSeparators, []rune{',', ' ', '\t', '\n'},
false, false,
[]string{ []string{
"a", "a",
@ -137,20 +151,6 @@ func TestScanAllWithDefaultSeparators(t *testing.T) {
`'abc "'`, `'abc "'`,
}, },
}, },
{
"custom separators",
`a, b, c, d e, "a,b, c ", (123, 456)`,
[]rune{','},
false,
[]string{
"a",
"b",
"c",
"d e",
`"a,b, c "`,
`(123, 456)`,
},
},
} }
for _, s := range scenarios { for _, s := range scenarios {
@ -166,7 +166,7 @@ func TestScanAllWithDefaultSeparators(t *testing.T) {
} }
if len(tokens) != len(s.expectTokens) { if len(tokens) != len(s.expectTokens) {
t.Fatalf("[%s] Expected \n%v, \ngot \n%v", s.name, s.expectTokens, tokens) t.Fatalf("[%s] Expected \n%v (%d), \ngot \n%v (%d)", s.name, s.expectTokens, len(s.expectTokens), tokens, len(tokens))
} }
for _, tok := range tokens { for _, tok := range tokens {