Simplified splitter to use a single scanner and respect comment lines

This commit is contained in:
Christopher Ramey 2020-08-12 18:10:54 -08:00
parent 1fcf9f1291
commit e8d45282e8
2 changed files with 51 additions and 70 deletions

115
main.go
View File

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"os" "os"
"bufio" "bufio"
"strings"
"unicode/utf8"
) )
func main(){ func main(){
@ -22,82 +20,65 @@ func main(){
} }
defer file.Close() defer file.Close()
lscan := bufio.NewScanner(file) scan := bufio.NewScanner(file)
lscan.Split(bufio.ScanLines) scan.Split(Split)
for lscan.Scan() { for scan.Scan() {
line := lscan.Text() word := scan.Text()
// Ignore comments
if len(line) < 1 || line[0] == '#' {
continue
}
wscan := bufio.NewScanner(strings.NewReader(line))
wscan.Split(ScanWords)
for wscan.Scan() {
word := wscan.Text()
fmt.Printf("[%s] ", word) fmt.Printf("[%s] ", word)
} }
}
fmt.Printf("\n") fmt.Printf("\n")
} }
func ScanWords(data []byte, atEOF bool) (int, []byte, error) { func Split(data []byte, atEOF bool) (int, []byte, error) {
start := 0 var ignoreline bool
quote := int32(0) var started bool
for start < len(data) { var startidx int
r, w := utf8.DecodeRune(data[start:]) var quote byte
if !isSpace(r) {
if isQuote(r) { for i := 0; i < len(data); i++ {
quote = r c := data[i]
start += w switch c {
case '\f', '\n', '\r':
if ignoreline {
return i + 1, nil, nil
} }
break fallthrough
}
start += w case ' ', '\t', '\v':
if started && quote == 0 {
return i + 1, data[startidx:i], nil
} }
for i := start; i < len(data); { case '\'', '"', '`':
r, w := utf8.DecodeRune(data[i:]) if started && quote == c {
return i + 1, data[startidx:i], nil
if (quote > 0 && quote == r) || (quote == 0 && isSpace(r)) {
return i + w, data[start:i], nil
} }
i += w if quote == 0 {
quote = c
} }
if atEOF && len(data) > start { case '#':
return len(data), data[start:], nil if !started {
ignoreline = true
} }
return start, nil, nil
} default:
if !ignoreline && !started {
func isQuote(r rune) bool { started = true
switch r { startidx = i
case '\u0022', '\u0027': }
return true }
} }
return false
} if atEOF {
if ignoreline {
func isSpace(r rune) bool { return len(data), nil, nil
if r <= '\u00FF' { }
switch r { if started {
case ' ', '\t', '\n', '\v', '\f', '\r': return len(data), data[startidx:], nil
return true }
case '\u0085', '\u00A0': }
return true
} return 0, nil, nil
return false
}
if '\u2000' <= r && r <= '\u200a' {
return true
}
switch r {
case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
return true
}
return false
} }

View File

@ -1,4 +1,4 @@
set alert set alert "Don't think about it"
monitor group monitor group
host www1.example.com address 10.11.135.101 host www1.example.com address 10.11.135.101