-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
84 lines (71 loc) · 1.74 KB
/
token.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
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
package eventstream
// TokenType indicates the type of an event stream token returned by scanner.
type TokenType int
// Token types.
const (
EndToken TokenType = 0
CommentToken TokenType = 1
FieldToken TokenType = 2
)
// Token holds a reference to buffered line data read by Scanner. It is safe
// to pass tokens by value.
//
// A token is only valid until the next call to scanner.Scan, after which its
// underlying data may be overwritten.
type Token struct {
line []byte
colon int
}
// Type returns the type of the token.
func (t Token) Type() TokenType {
switch {
case len(t.line) == 0:
return EndToken
case t.colon == 0:
return CommentToken
default:
return FieldToken
}
}
// Comment returns the token as a comment.
func (t Token) Comment() Comment {
// Check for non-comment and empty comment
if t.colon != 0 || len(t.line) <= 1 {
return nil
}
return Comment(t.line[1:])
}
// Field interprets the token as a field and returns it.
func (t Token) Field() Field {
switch {
case t.colon == 0:
return nil // Comment
case t.colon < 0:
return Field(t.line) // Field without value
default:
return Field(t.line[0:t.colon]) // Field with value
}
}
// Value interprets the token as a field and returns its value, if present.
func (t Token) Value() Value {
// Check for comment (colon == 0) and field without value (colon == -1)
if t.colon <= 0 {
return nil
}
length := len(t.line)
// Don't include the colon delimeter
start := t.colon + 1
// Check for empty value (without space prefix)
if start >= length {
return nil
}
// Don't include the space prefix if present
if t.line[start] == ' ' {
start++
}
// Check for empty value (with space prefix)
if start >= length {
return nil
}
return Value(t.line[start:])
}