-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_utils.go
105 lines (92 loc) · 2.4 KB
/
token_utils.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package anitogo
import "github.com/juju/errors"
// newTokenManager returns a new tokenManager instance
func newTokenManager() *tokenManager {
return new(tokenManager)
}
// Empty returns whether the token is in its default state
func (t token) Empty() bool {
return t == token{}
}
// Empty returns whether the tokenManager has no tokens in it
func (tm tokenManager) Empty() bool {
return len(tm.tokens) <= 0
}
// GetIndex returns the index of a token
func (t tokens) GetIndex(tkn token) (int, error) {
for i, tk := range t {
if *tk == tkn {
return i, errors.Trace(nil)
}
}
return -1, errors.Trace(ErrorTokenNotFound)
}
// Distance returns the distance between the indexes of two tokens
func (t tokens) Distance(tokenStart, tokenEnd token) (int, error) {
var startIndex int
var endIndex int
var err error
// if no start token provided, start at beginning
if tokenStart.Empty() {
startIndex = 0
} else {
// else, starting at tokenStart's index
startIndex, err = t.GetIndex(tokenStart)
if err != nil {
return -1, errors.Trace(err)
}
}
// if no end token provided, finish at end
if tokenEnd.Empty() {
endIndex = len(t)
} else {
// else, finish at tokenEnd's index
endIndex, err = t.GetIndex(tokenEnd)
if err != nil {
return -1, errors.Trace(err)
}
}
return endIndex - startIndex, errors.Trace(nil)
}
// FindInTokens finds a token in a tokens slice containing the specified flags
func (t tokens) FindInTokens(flags tokenFlags) (*token, error) {
for _, tkn := range t {
if tkn.HasFlags(flags) {
return tkn, errors.Trace(nil)
}
}
return &token{}, errors.Trace(ErrorMatchingTokenNotFound)
}
// ReverseOrder reverses the order of a tokens slice
func (t *tokens) ReverseOrder() {
lastIndex := len(*t) - 1
var tkns = *t
for i := 0; i < len(*t)/2; i++ {
tkns[i], tkns[lastIndex-i] = tkns[lastIndex-i], tkns[i]
}
t = &tkns
}
func (t tokens) FindPrevious(tkn token, flags tokenFlags) (*token, error) {
if tkn.Empty() {
t.ReverseOrder()
} else {
tokenIndex, err := t.GetIndex(tkn)
if err != nil {
return &token{}, errors.Trace(err)
}
t = t[tokenIndex-1:]
t.ReverseOrder()
}
return t.FindInTokens(flags)
}
func (t tokens) FindNext(tkn token, flags tokenFlags) (*token, error) {
if tkn.Empty() {
tokenIndex, err := t.GetIndex(tkn)
if err != nil {
return &token{}, errors.Trace(err)
}
t = t[tokenIndex+1:]
t.ReverseOrder()
}
return t.FindInTokens(flags)
}