lotus/postfix-config/comma-list-scanner/comma-list-scanner.go

58 lines
1.1 KiB
Go
Raw Normal View History

2023-08-21 00:26:22 +01:00
package comma_list_scanner
import (
"bufio"
"bytes"
"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
}
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()
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
}
func (c *CommaListScanner) Err() error {
return c.err
}