-
Notifications
You must be signed in to change notification settings - Fork 4
/
syntax_subscript_slice_negative_step.go
53 lines (45 loc) · 1.15 KB
/
syntax_subscript_slice_negative_step.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
package jsonpath
type syntaxSliceNegativeStepSubscript struct {
*syntaxBasicSubscript
start *syntaxIndexSubscript
end *syntaxIndexSubscript
step *syntaxIndexSubscript
}
func (s *syntaxSliceNegativeStepSubscript) getIndexes(srcLength int) []int {
loopStart := s.getLoopStart(srcLength)
loopEnd := s.getLoopEnd(srcLength)
index, result := 0, make([]int, srcLength)
if s.step.number < 0 {
for i := loopStart; i > loopEnd; i += s.step.number {
result[index] = i
index++
}
}
return result[:index]
}
func (s *syntaxSliceNegativeStepSubscript) getLoopStart(srcLength int) int {
loopStart := s.start.number
if s.start.isOmitted {
loopStart = srcLength - 1
}
return s.getNormalizedValue(loopStart, srcLength)
}
func (s *syntaxSliceNegativeStepSubscript) getLoopEnd(srcLength int) int {
loopEnd := s.end.number
if s.end.isOmitted {
loopEnd = -srcLength - 1
}
return s.getNormalizedValue(loopEnd, srcLength)
}
func (s *syntaxSliceNegativeStepSubscript) getNormalizedValue(value int, srcLength int) int {
if value < 0 {
value += srcLength
if value < -1 {
value = -1
}
}
if value > srcLength-1 {
value = srcLength - 1
}
return value
}