2023-08-21 00:26:22 +01:00
|
|
|
package comma_list_scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2023-08-22 13:38:01 +01:00
|
|
|
"fmt"
|
2023-08-21 00:26:22 +01:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommaListScanner struct {
|
|
|
|
r *bufio.Scanner
|
|
|
|
text string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCommaListScanner(r io.Reader) *CommaListScanner {
|
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
if atEOF && len(data) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
2023-08-22 13:38:01 +01:00
|
|
|
println("data", fmt.Sprintf("%s", data))
|
|
|
|
println("index", bytes.IndexAny(data, " ,"))
|
|
|
|
if i := bytes.IndexAny(data, " ,"); i >= 0 {
|
|
|
|
// consume all spaces after the comma
|
|
|
|
j := i + 1
|
|
|
|
for j < len(data) && data[j] == ' ' {
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
return j, bytes.TrimSpace(data[0:i]), nil
|
2023-08-21 00:26:22 +01:00
|
|
|
}
|
|
|
|
// If we're at EOF, we have a final non-terminated line. Return it.
|
|
|
|
if atEOF {
|
|
|
|
return len(data), bytes.TrimSpace(data), nil
|
|
|
|
}
|
|
|
|
// Request more data.
|
|
|
|
return 0, nil, nil
|
|
|
|
})
|
|
|
|
return &CommaListScanner{r: s}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommaListScanner) Scan() bool {
|
|
|
|
if c.r.Scan() {
|
|
|
|
c.text = c.r.Text()
|
2023-08-22 13:38:01 +01:00
|
|
|
return true
|
2023-08-21 00:26:22 +01:00
|
|
|
}
|
|
|
|
c.err = c.r.Err()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommaListScanner) Text() string {
|
|
|
|
return c.text
|
|
|
|
}
|
2023-08-22 13:38:01 +01:00
|
|
|
|
|
|
|
func (c *CommaListScanner) Err() error {
|
|
|
|
return c.err
|
|
|
|
}
|