-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparams.go
105 lines (90 loc) · 2.25 KB
/
params.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 vte
import (
"fmt"
"strings"
)
// maxParams is the maximum number of parameters allowed.
const maxParams = 32
// params is a parameters for VTE.
type params struct {
// Number of subparameters in each parameter
//
// For each entry in the `params` slice, this stores the length of the param as number of
// subparams at the same index as the param in the `params` slice.
//
// At the subparam positions the length will always be `0`.
subparams [maxParams]uint8
// All parameters and subparameters
params [maxParams]uint16
// Number of subparams in the current parameter.
current_subparams uint8
len int
}
// Len returns the number of parameters.
func (p *params) Len() int {
return p.len
}
// IsEmpty returns true if there are no parameters.
func (p *params) IsEmpty() bool {
return p.len == 0
}
// IsFull returns true if there are no more parameters can be added.
func (p *params) IsFull() bool {
return p.len == maxParams
}
// Clear clears all parameters.
func (p *params) Clear() {
p.current_subparams = 0
p.len = 0
}
// Push pushes a parameter.
func (p *params) Push(param uint16) {
p.subparams[p.len-int(p.current_subparams)] = p.current_subparams + 1
p.params[p.len] = param
p.current_subparams = 0
p.len++
}
// Extend extends the last parameter.
func (p *params) Extend(param uint16) {
p.subparams[p.len-int(p.current_subparams)] = p.current_subparams + 1
p.params[p.len] = param
p.current_subparams++
p.len++
}
// Range iterates over all parameters.
func (p *params) Range(f func(param []uint16)) {
for i := 0; i < p.len; {
numSubparams := p.subparams[i]
param := p.params[i : i+int(numSubparams)]
i += int(numSubparams)
f(param)
}
}
// Params returns all parameters and their subparameters as a slice.
func (p *params) Params() []uint16 {
var params []uint16
p.Range(func(param []uint16) {
params = append(params, param...)
})
return params
}
// String returns a string representation of the parameters.
func (p *params) String() string {
var b strings.Builder
b.WriteString("[")
i := 0
p.Range(func(param []uint16) {
if i > 0 {
b.WriteRune(';')
}
for j, subparam := range param {
if j > 0 {
b.WriteRune(':')
}
b.WriteString(fmt.Sprint(subparam))
}
i++
})
b.WriteString("]")
return b.String()
}