forked from jlaffaye/ftp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.go
58 lines (49 loc) · 1.09 KB
/
scanner.go
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
package ftp
// A scanner for fields delimited by one or more whitespace characters
type scanner struct {
bytes []byte
position int
}
// newScanner creates a new scanner
func newScanner(str string) *scanner {
return &scanner{
bytes: []byte(str),
}
}
// NextFields returns the next `count` fields
func (s *scanner) NextFields(count int) []string {
fields := make([]string, 0, count)
for i := 0; i < count; i++ {
if field := s.Next(); field != "" {
fields = append(fields, field)
} else {
break
}
}
return fields
}
// Next returns the next field
func (s *scanner) Next() string {
sLen := len(s.bytes)
// skip trailing whitespace
for s.position < sLen {
if s.bytes[s.position] != ' ' {
break
}
s.position++
}
start := s.position
// skip non-whitespace
for s.position < sLen {
if s.bytes[s.position] == ' ' {
s.position++
return string(s.bytes[start : s.position-1])
}
s.position++
}
return string(s.bytes[start:s.position])
}
// Remaining returns the remaining string
func (s *scanner) Remaining() string {
return string(s.bytes[s.position:len(s.bytes)])
}