-
Notifications
You must be signed in to change notification settings - Fork 0
/
delimited_address_strings.go
235 lines (208 loc) · 6.15 KB
/
delimited_address_strings.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 goip
import "strings"
const SegmentValueDelimiter = ','
type DelimitedAddressString string
// CountDelimitedAddresses will count the possible combinations,
// given a string with comma delimiters separating segment elements.
// It is a counterpart to ParseDelimitedSegments,
// indicating the number of iterated elements from ParseDelimitedSegments.
//
// For example, given "1,2.3.4,5.6" this method will return 4 for the possible combinations:
// "1.3.4.6", "1.3.5.6", "2.3.4.6" and "2.3.5.6".
func (str DelimitedAddressString) CountDelimitedAddresses() int {
segDelimitedCount := 0
result := 1
strlen := len(str)
for i := 0; i < strlen; i++ {
c := str[i]
if isDelimitedBoundary(c) {
if segDelimitedCount > 0 {
result *= segDelimitedCount + 1
segDelimitedCount = 0
}
} else if c == SegmentValueDelimiter {
segDelimitedCount++
}
}
if segDelimitedCount > 0 {
result *= segDelimitedCount + 1
}
return result
}
// ParseDelimitedSegments will provide an iterator to iterate through the possible combinations,
// given a string with comma delimiters to denote segment elements,
//
// For example, given "1,2.3.4,5.6" this will iterate through "1.3.4.6", "1.3.5.6", "2.3.4.6" and "2.3.5.6"
//
// Another example: "1-2,3.4.5.6" will iterate through "1-2.4.5.6" and "1-3.4.5.6".
//
// This method will not validate strings.
// Each string produced can be validated using an instance of [IPAddressString].
// Use CountDelimitedAddresses for the count of elements in the iterator.
func (str DelimitedAddressString) ParseDelimitedSegments() Iterator[string] {
var parts [][]string
var delimitedList []string
var lastSegmentStartIndex, lastPartIndex, lastDelimiterIndex int
anyDelimited := false
strlen := len(str)
s := string(str)
for i := 0; i < strlen; i++ {
c := str[i]
if isDelimitedBoundary(c) { // end of segment or range boundary
if len(delimitedList) > 0 {
if parts == nil {
parts = make([][]string, 0, IPv6SegmentCount)
}
parts, _ = addParts(s, parts, lastSegmentStartIndex, lastPartIndex, lastDelimiterIndex, delimitedList, i)
lastPartIndex = i
delimitedList = delimitedList[:0]
}
lastDelimiterIndex = i + 1
lastSegmentStartIndex = lastDelimiterIndex
} else if c == SegmentValueDelimiter {
anyDelimited = true
if delimitedList == nil {
delimitedList = make([]string, 0, 4)
}
sub := str[lastDelimiterIndex:i]
delimitedList = append(delimitedList, string(sub))
lastDelimiterIndex = i + 1
}
}
if anyDelimited {
if len(delimitedList) > 0 {
if parts == nil {
parts = make([][]string, 0, IPv6SegmentCount)
}
parts, _ = addParts(s, parts, lastSegmentStartIndex, lastPartIndex, lastDelimiterIndex, delimitedList, len(str))
} else {
parts = append(parts, []string{s[lastPartIndex:]})
}
return newDelimitedStringsIterator(parts)
}
return newSingleStrIterator(s)
}
// ParseDelimitedIPAddrSegments will provide an iterator to iterate through the possible combinations,
// given a string with comma delimiters to denote segment elements.
func (str DelimitedAddressString) ParseDelimitedIPAddrSegments() Iterator[*IPAddressString] {
return ipAddressStringIterator{str.ParseDelimitedSegments()}
}
type delimitedStringsIterator struct {
parts [][]string
done bool
variations []Iterator[string]
nextSet []string
}
func (it *delimitedStringsIterator) updateVariations(start int) {
variationLen := len(it.variations)
variations := it.variations
parts := it.parts
nextSet := it.nextSet
for i := start; i < variationLen; i++ {
strSlice := parts[i]
if len(strSlice) > 1 {
variations[i] = newStrSliceIterator(strSlice)
} else {
variations[i] = newSingleStrIterator(strSlice[0])
}
nextSet[i] = variations[i].Next()
}
}
func (it *delimitedStringsIterator) HasNext() bool {
return !it.done
}
func (it *delimitedStringsIterator) Next() (res string) {
if !it.done {
result := strings.Builder{}
nextSet := it.nextSet
nextSetLen := len(nextSet)
for i := 0; i < nextSetLen; i++ {
result.WriteString(nextSet[i])
}
it.increment()
res = result.String()
}
return
}
func (it *delimitedStringsIterator) increment() {
variations := it.variations
variationsLen := len(variations)
nextSet := it.nextSet
for j := variationsLen - 1; j >= 0; j-- {
if variations[j].HasNext() {
nextSet[j] = variations[j].Next()
it.updateVariations(j + 1)
return
}
}
it.done = true
}
type stringIterator struct {
strs []string
}
func (it *stringIterator) HasNext() bool {
return len(it.strs) > 0
}
func (it *stringIterator) Next() (res string) {
if it.HasNext() {
strs := it.strs
res = strs[0]
it.strs = strs[1:]
}
return
}
type singleStringIterator struct {
str string
done bool
}
func (it *singleStringIterator) HasNext() bool {
return !it.done
}
func (it *singleStringIterator) Next() (res string) {
if it.HasNext() {
it.done = true
res = it.str
}
return
}
type ipAddressStringIterator struct {
Iterator[string]
}
func (iter ipAddressStringIterator) Next() *IPAddressString {
if !iter.HasNext() {
return nil
}
return NewIPAddressString(iter.Iterator.Next())
}
func isDelimitedBoundary(c byte) bool {
return c == IPv4SegmentSeparator ||
c == IPv6SegmentSeparator ||
c == RangeSeparator ||
c == MacDashedSegmentRangeSeparator
}
func addParts(str string, parts [][]string, lastSegmentStartIndex, lastPartIndex,
lastDelimiterIndex int, delimitedList []string, i int) (newParts [][]string, newDelimitedList []string) {
sub := str[lastDelimiterIndex:i]
delimitedList = append(delimitedList, sub)
if lastPartIndex != lastSegmentStartIndex {
parts = append(parts, []string{str[lastPartIndex:lastSegmentStartIndex]})
}
parts = append(parts, delimitedList)
return parts, delimitedList
}
func newStrSliceIterator(strs []string) Iterator[string] {
return &stringIterator{strs: strs}
}
func newSingleStrIterator(str string) Iterator[string] {
return &singleStringIterator{str: str}
}
func newDelimitedStringsIterator(parts [][]string) Iterator[string] {
partCount := len(parts)
it := &delimitedStringsIterator{
parts: parts,
variations: make([]Iterator[string], partCount),
nextSet: make([]string, partCount),
}
it.updateVariations(0)
return it
}