-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.go
More file actions
95 lines (80 loc) · 2.82 KB
/
Copy pathscan.go
File metadata and controls
95 lines (80 loc) · 2.82 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2022, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"bufio"
"bytes"
"io"
"unicode"
"github.com/go-pogo/errors"
)
type Scanner struct {
scanner *bufio.Scanner
}
const panicNilReader = "env: io.Reader must not be nil"
// NewScanner returns a new [Scanner] which wraps a [bufio.Scanner] that reads
// from [io.Reader] r. The split function defaults to [ScanLines].
// Successive calls to the Scan method will (just like [bufio.Scanner]) step
// through the 'tokens' of the read bytes, skipping the bytes between the
// tokens.
func NewScanner(r io.Reader) *Scanner {
if r == nil {
panic(panicNilReader)
}
s := bufio.NewScanner(r)
s.Split(ScanLines)
return &Scanner{scanner: s}
}
// ScanLines is a [bufio.SplitFunc] that returns each line of text using
// [bufio.ScanLines]. Additionally, any leading or trailing whitespace is
// stripped from the token result. Lines that start with a #, after all leading
// whitespace is stripped, are treated as comments and result in an empty token
// result.
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
// bufio.ScanLines always returns a nil error
advance, token, _ = bufio.ScanLines(data, atEOF)
if len(token) == 0 {
return advance, token, err
}
if unicode.IsSpace(rune(token[0])) {
token = bytes.TrimLeftFunc(token, unicode.IsSpace)
if len(token) == 0 {
return advance, token, nil
}
}
if token[0] == '#' {
return advance, token[:0], nil
}
token = bytes.TrimRightFunc(token, unicode.IsSpace)
return advance, token, nil
}
// Err returns the first non-EOF error that was encountered by the [Scanner].
func (s *Scanner) Err() error { return errors.WithStack(s.scanner.Err()) }
// Bytes returns the most recent token generated by a call to Scan.
// The underlying array may point to data that will be overwritten
// by a subsequent call to Scan. It does no allocation.
func (s *Scanner) Bytes() []byte { return s.scanner.Bytes() }
// Text returns the most recent token generated by a call to Scan as a newly
// allocated string holding its bytes.
func (s *Scanner) Text() string { return s.scanner.Text() }
// NamedValue returns the parsed [NamedValue] from the most recent token
// generated by a call to Scan.
func (s *Scanner) NamedValue() (NamedValue, error) {
line := s.scanner.Text()
if line == "" {
return NamedValue{}, nil
}
return parse(line)
}
// Scan advances the scanner to the next token, which will then be available
// through the Bytes or Text method. The token is guaranteed to not be empty.
// See [bufio.Scanner] for additional details.
func (s *Scanner) Scan() bool {
ok := s.scanner.Scan()
// lookup next token when line is empty
for ok && len(s.scanner.Bytes()) == 0 {
ok = s.scanner.Scan()
}
return ok
}