-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot.go
235 lines (203 loc) · 4.4 KB
/
dot.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package objectid
/*
DotNotation contains an ordered sequence of NumberForm instances.
*/
type DotNotation []NumberForm
/*
String is a stringer method that returns the dotNotation
form of the receiver (e.g.: "1.3.6.1").
*/
func (d DotNotation) String() (s string) {
if !d.IsZero() {
var x []string
for i := 0; i < len(d); i++ {
x = append(x, d[i].String())
}
s = join(x, `.`)
}
return
}
/*
Root returns the root node (0) NumberForm instance.
*/
func (d DotNotation) Root() NumberForm {
x, _ := d.Index(0)
return x
}
func (d DotNotation) Len() int {
return len(d)
}
/*
Leaf returns the leaf-node (-1) NumberForm instance.
*/
func (d DotNotation) Leaf() NumberForm {
x, _ := d.Index(-1)
return x
}
/*
Parent returns the leaf-node's parent (-2) NumberForm instance.
*/
func (d DotNotation) Parent() NumberForm {
x, _ := d.Index(-2)
return x
}
/*
IsZero returns a boolean indicative of whether the receiver
is unset.
*/
func (d *DotNotation) IsZero() (is bool) {
if d != nil {
is = d.Len() == 0
}
return
}
/*
NewDotNotation returns an instance of *DotNotation alongside a boolean
value indicative of success.
*/
func NewDotNotation(id string) (d *DotNotation, err error) {
if len(id) == 0 {
err = errorf("input value is zero-length")
return
}
ids := split(id, `.`)
var t *DotNotation = new(DotNotation)
for i := 0; i < len(ids) && err == nil; i++ {
var a NumberForm
a, err = NewNumberForm(ids[i])
*t = append(*t, a)
}
if err == nil {
d = new(DotNotation)
*d = *t
}
return
}
/*
IntSlice returns slices of integer values and an error. The integer values are based
upon the contents of the receiver. Note that if any single arc number overflows int,
a zero slice is returned.
Successful output can be cast as an instance of asn1.ObjectIdentifier, if desired.
*/
func (d DotNotation) IntSlice() (slice []int, err error) {
if d.IsZero() {
return
}
var t []int
for i := 0; i < len(d); i++ {
var n int
if n, err = atoi(d[i].String()); err != nil {
return
}
t = append(t, n)
}
if len(t) > 0 {
slice = t[:]
}
return
}
/*
Index returns the Nth index from the receiver, alongside a boolean
value indicative of success. This method supports the use of negative
indices.
*/
func (d DotNotation) Index(idx int) (a NumberForm, ok bool) {
if L := len(d); L > 0 {
if idx < 0 {
a = d[0]
if x := L + idx; x >= 0 {
a = d[x]
}
} else if idx > L {
a = d[L-1]
} else {
a = d[idx]
}
ok = true
}
return
}
/*
Ancestry returns slices of DotNotation values ordered from leaf
node (first) to root node (last).
Empty slices of DotNotation are returned if the dotNotation value
within the receiver is less than two (2) NumberForm values in length.
*/
func (d DotNotation) Ancestry() (anc []DotNotation) {
if d.Len() > 0 {
for i := d.Len(); i > 0; i-- {
anc = append(anc, d[:i])
}
}
return
}
/*
AncestorOf returns a boolean value indicative of whether the receiver
is an ancestor of the input value, which can be string or DotNotation.
*/
func (d DotNotation) AncestorOf(dot any) (is bool) {
if !d.IsZero() {
var D *DotNotation
switch tv := dot.(type) {
case string:
D, _ = NewDotNotation(tv)
case *DotNotation:
if tv != nil {
D = tv
}
case DotNotation:
if tv.Len() >= 0 {
D = &tv
}
}
if D.Len() > d.Len() {
is = d.matchDotNot(D)
}
}
return
}
func (d DotNotation) matchDotNot(dot *DotNotation) bool {
L := d.Len()
ct := 0
for i := 0; i < L; i++ {
x, _ := d.Index(i)
if y, ok := dot.Index(i); ok {
if x.Equal(y) {
ct++
}
}
}
return ct == L
}
/*
NewSubordinate returns a new instance of DotNotation based upon the
contents of the receiver as well as the input NumberForm subordinate
value. This creates a fully-qualified child DotNotation value of the
receiver.
*/
func (d DotNotation) NewSubordinate(nf any) (dot *DotNotation) {
if d.Len() > 0 {
// Prepare the new leaf numberForm,
// or die trying.
if a, err := NewNumberForm(nf); err == nil {
D := make(DotNotation, d.Len()+1, d.Len()+1)
for i := 0; i < d.Len(); i++ {
D[i] = d[i]
}
D[D.Len()-1] = a
dot = &D
}
}
return
}
/*
Valid returns a boolean value indicative of the following:
• Receiver's length is greater than or equal to one (1) slice member, and ...
• The first slice in the receiver contains a decimal value that is less than three (3)
*/
func (d DotNotation) Valid() (is bool) {
if !d.IsZero() {
is = d.Root().Lt(3)
}
return
}