-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework_wrappers.go
782 lines (715 loc) · 40.7 KB
/
framework_wrappers.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package goip
import (
"github.com/pchchv/goip/address_error"
"github.com/pchchv/goip/address_string"
)
var _, _ ExtendedSegmentSeries = WrappedAddress{}, WrappedAddressSection{}
// ExtendedSegmentSeries wraps either an Address or AddressSection.
// ExtendedSegmentSeries can be used to write code that works with both addresses and address sections,
// going further than AddressSegmentSeries to offer additional methods with the series types in their signature.
type ExtendedSegmentSeries interface {
AddressSegmentSeries
// Unwrap returns the wrapped address or address section as an interface, AddressSegmentSeries.
Unwrap() AddressSegmentSeries
// Equal returns whether the given address series is equal to this address series.
// Two address series are equal if they represent the same set of series.
// Both must be equal addresses or both must be equal sections.
Equal(ExtendedSegmentSeries) bool
// Contains returns whether this is same type and version as the given address series and whether it contains all values in the given series.
//
// Series must also have the same number of segments to be comparable, otherwise false is returned.
Contains(ExtendedSegmentSeries) bool
// GetSection returns the backing section for this series, comprising all segments.
GetSection() *AddressSection
// GetTrailingSection returns an ending subsection of the full address section.
GetTrailingSection(index int) *AddressSection
// GetSubSection returns a subsection of the full address section.
GetSubSection(index, endIndex int) *AddressSection
// GetSegment returns the segment at the given index.
// The first segment is at index 0.
// GetSegment will panic given a negative index or an index matching or larger than the segment count.
GetSegment(index int) *AddressSegment
// GetSegments returns a slice with the address segments. The returned slice is not backed by the same array as this section.
GetSegments() []*AddressSegment
// CopySegments copies the existing segments into the given slice,
// as much as can be fit into the slice, returning the number of segments copied.
CopySegments(segs []*AddressSegment) (count int)
// CopySubSegments copies the existing segments from the given start index until but not including the segment at the given end index,
// into the given slice, as much as can be fit into the slice, returning the number of segments copied.
CopySubSegments(start, end int, segs []*AddressSegment) (count int)
// IsIP returns true if this series originated as an IPv4 or IPv6 series, or a zero-length IP series. If so, use ToIP to convert back to the IP-specific type.
IsIP() bool
// IsIPv4 returns true if this series originated as an IPv4 series. If so, use ToIPv4 to convert back to the IPv4-specific type.
IsIPv4() bool
// IsIPv6 returns true if this series originated as an IPv6 series. If so, use ToIPv6 to convert back to the IPv6-specific type.
IsIPv6() bool
// IsMAC returns true if this series originated as a MAC series. If so, use ToMAC to convert back to the MAC-specific type.
IsMAC() bool
// ToIP converts to an IPAddressSegmentSeries if this series originated as IPv4 or IPv6, or an implicitly zero-valued IP.
// If not, ToIP returns nil.
ToIP() IPAddressSegmentSeries
// ToIPv4 converts to an IPv4AddressSegmentSeries if this series originated as an IPv4 series.
// If not, ToIPv4 returns nil.
//
// ToIPv4 implementations can be called with a nil receiver, enabling you to chain this method with methods that might return a nil pointer.
ToIPv4() IPv4AddressSegmentSeries
// ToIPv6 converts to an IPv4AddressSegmentSeries if this series originated as an IPv6 series.
// If not, ToIPv6 returns nil.
//
// ToIPv6 implementations can be called with a nil receiver, enabling you to chain this method with methods that might return a nil pointer.
ToIPv6() IPv6AddressSegmentSeries
// ToMAC converts to a MACAddressSegmentSeries if this series originated as a MAC series.
// If not, ToMAC returns nil.
//
// ToMAC implementations can be called with a nil receiver, enabling you to chain this method with methods that might return a nil pointer.
ToMAC() MACAddressSegmentSeries
// ToBlock creates a new series block by changing the segment at the given index to have the given lower and upper value,
// and changing the following segments to be full-range.
ToBlock(segmentIndex int, lower, upper SegInt) ExtendedSegmentSeries
// ToPrefixBlock returns the series with the same prefix as this series while the remaining bits span all values.
// The series will be the block of all series with the same prefix.
//
// If this series has no prefix, this series is returned.
ToPrefixBlock() ExtendedSegmentSeries
// ToPrefixBlockLen returns the series with the same prefix of the given length as this series while the remaining bits span all values.
// The returned series will be the block of all series with the same prefix.
ToPrefixBlockLen(prefLen BitCount) ExtendedSegmentSeries
// Increment returns the item that is the given increment upwards into the range,
// with the increment of 0 returning the first in the range.
//
// If the increment i matches or exceeds the range count c, then i - c + 1
// is added to the upper item of the range.
// An increment matching the count gives you the item just above the highest in the range.
//
// If the increment is negative, it is added to the lowest of the range.
// To get the item just below the lowest of the range, use the increment -1.
//
// If this represents just a single value, the item is simply incremented by the given increment, positive or negative.
//
// If this item represents multiple values, a positive increment i is equivalent i + 1 values from the iterator and beyond.
// For instance, a increment of 0 is the first value from the iterator, an increment of 1 is the second value from the iterator, and so on.
// An increment of a negative value added to the count is equivalent to the same number of iterator values preceding the last value of the iterator.
// For instance, an increment of count - 1 is the last value from the iterator, an increment of count - 2 is the second last value, and so on.
//
// On overflow or underflow, Increment returns nil.
Increment(int64) ExtendedSegmentSeries
// IncrementBoundary returns the item that is the given increment from the range boundaries of this item.
//
// If the given increment is positive, adds the value to the highest (GetUpper) in the range to produce a new item.
// If the given increment is negative, adds the value to the lowest (GetLower) in the range to produce a new item.
// If the increment is zero, returns this.
//
// If this represents just a single value, this item is simply incremented by the given increment value, positive or negative.
//
// On overflow or underflow, IncrementBoundary returns nil.
IncrementBoundary(int64) ExtendedSegmentSeries
// GetLower returns the series in the range with the lowest numeric value,
// which will be the same series if it represents a single value.
GetLower() ExtendedSegmentSeries
// GetUpper returns the series in the range with the highest numeric value,
// which will be the same series if it represents a single value.
GetUpper() ExtendedSegmentSeries
// AssignPrefixForSingleBlock returns the equivalent prefix block that matches exactly the range of values in this series.
// The returned block will have an assigned prefix length indicating the prefix length for the block.
//
// There may be no such series - it is required that the range of values match the range of a prefix block.
// If there is no such series, then nil is returned.
AssignPrefixForSingleBlock() ExtendedSegmentSeries
// AssignMinPrefixForBlock returns an equivalent series, assigned the smallest prefix length possible,
// such that the prefix block for that prefix length is in this series.
//
// In other words, this method assigns a prefix length to this series matching the largest prefix block in this series.
AssignMinPrefixForBlock() ExtendedSegmentSeries
// Iterator provides an iterator to iterate through the individual series of this series.
//
// When iterating, the prefix length is preserved. Remove it using WithoutPrefixLen prior to iterating if you wish to drop it from all individual series.
//
// Call IsMultiple to determine if this instance represents multiple series, or GetCount for the count.
Iterator() Iterator[ExtendedSegmentSeries]
// PrefixIterator provides an iterator to iterate through the individual prefixes of this series,
// each iterated element spanning the range of values for its prefix.
//
// It is similar to the prefix block iterator, except for possibly the first and last iterated elements, which might not be prefix blocks,
// instead constraining themselves to values from this series.
//
// If the series has no prefix length, then this is equivalent to Iterator.
PrefixIterator() Iterator[ExtendedSegmentSeries]
// PrefixBlockIterator provides an iterator to iterate through the individual prefix blocks, one for each prefix of this series.
// Each iterated series will be a prefix block with the same prefix length as this series.
//
// If this series has no prefix length, then this is equivalent to Iterator.
PrefixBlockIterator() Iterator[ExtendedSegmentSeries]
// AdjustPrefixLen increases or decreases the prefix length by the given increment.
//
// A prefix length will not be adjusted lower than zero or beyond the bit length of the series.
//
// If this series has no prefix length, then the prefix length will be set to the adjustment if positive,
// or it will be set to the adjustment added to the bit count if negative.
AdjustPrefixLen(BitCount) ExtendedSegmentSeries
// AdjustPrefixLenZeroed increases or decreases the prefix length by the given increment while zeroing out the bits that have moved into or outside the prefix.
//
// A prefix length will not be adjusted lower than zero or beyond the bit length of the series.
//
// If this series has no prefix length, then the prefix length will be set to the adjustment if positive,
// or it will be set to the adjustment added to the bit count if negative.
//
// When prefix length is increased, the bits moved within the prefix become zero.
// When a prefix length is decreased, the bits moved outside the prefix become zero.
AdjustPrefixLenZeroed(BitCount) (ExtendedSegmentSeries, address_error.IncompatibleAddressError)
// SetPrefixLen sets the prefix length.
//
// A prefix length will not be set to a value lower than zero or beyond the bit length of the series.
// The provided prefix length will be adjusted to these boundaries if necessary.
SetPrefixLen(BitCount) ExtendedSegmentSeries
// SetPrefixLenZeroed sets the prefix length.
//
// A prefix length will not be set to a value lower than zero or beyond the bit length of the series.
// The provided prefix length will be adjusted to these boundaries if necessary.
//
// If this series has a prefix length, and the prefix length is increased when setting the new prefix length, the bits moved within the prefix become zero.
// If this series has a prefix length, and the prefix length is decreased when setting the new prefix length, the bits moved outside the prefix become zero.
//
// In other words, bits that move from one side of the prefix length to the other (bits moved into the prefix or outside the prefix) are zeroed.
//
// If the result cannot be zeroed because zeroing out bits results in a non-contiguous segment, an error is returned.
SetPrefixLenZeroed(BitCount) (ExtendedSegmentSeries, address_error.IncompatibleAddressError)
// WithoutPrefixLen provides the same address series but with no prefix length. The values remain unchanged.
WithoutPrefixLen() ExtendedSegmentSeries
// ReverseBytes returns a new segment series with the bytes reversed. Any prefix length is dropped.
//
// If each segment is more than 1 byte long, and the bytes within a single segment cannot be reversed because the segment represents a range,
// and reversing the segment values results in a range that is not contiguous, then this returns an error.
//
// In practice this means that to be reversible, a range must include all values except possibly the largest and/or smallest, which reverse to themselves.
ReverseBytes() (ExtendedSegmentSeries, address_error.IncompatibleAddressError)
// ReverseBits returns a new segment series with the bits reversed. Any prefix length is dropped.
//
// If the bits within a single segment cannot be reversed because the segment represents a range,
// and reversing the segment values results in a range that is not contiguous, this returns an error.
//
// In practice this means that to be reversible, a range must include all values except possibly the largest and/or smallest, which reverse to themselves.
ReverseBits(perByte bool) (ExtendedSegmentSeries, address_error.IncompatibleAddressError)
// ReverseSegments returns a new series with the segments reversed.
ReverseSegments() ExtendedSegmentSeries
// ToCustomString creates a customized string from this series according to the given string option parameters.
ToCustomString(stringOptions address_string.StringOptions) string
}
// WrappedAddress is the implementation of ExtendedSegmentSeries for addresses.
type WrappedAddress struct {
*Address
}
// GetSection returns the backing section for this series, comprising all segments.
func (addr WrappedAddress) GetSection() *AddressSection {
return addr.Address.GetSection()
}
// Equal returns whether the given address series is equal to this address series.
// Two address series are equal if they represent the same set of series.
// Both must be equal addresses.
func (addr WrappedAddress) Equal(other ExtendedSegmentSeries) bool {
a, ok := other.Unwrap().(AddressType)
return ok && addr.Address.Equal(a)
}
// Unwrap returns the wrapped address as an interface, AddressSegmentSeries.
func (addr WrappedAddress) Unwrap() AddressSegmentSeries {
res := addr.Address
if res == nil {
return nil
}
return res
}
// ToIPv4 converts to an IPv4AddressSegmentSeries if this series originated as an IPv4 series.
// If not, ToIPv4 returns nil.
//
// ToIPv4 implementations can be called with a nil receiver,
// enabling you to chain this method with methods that might return a nil pointer.
func (addr WrappedAddress) ToIPv4() IPv4AddressSegmentSeries {
return addr.Address.ToIPv4()
}
// ToIPv6 converts to an IPv4AddressSegmentSeries if this series originated as an IPv6 series.
// If not, ToIPv6 returns nil.
//
// ToIPv6 implementations can be called with a nil receiver,
// enabling you to chain this method with methods that might return a nil pointer.
func (addr WrappedAddress) ToIPv6() IPv6AddressSegmentSeries {
return addr.Address.ToIPv6()
}
// ToIP converts to an IP address if this originated as IPv4 or IPv6, or an implicitly zero-valued IP.
// If not, ToIP returns nil.
func (addr WrappedAddress) ToIP() IPAddressSegmentSeries {
return addr.Address.ToIP()
}
// ToMAC converts to a MACAddressSegmentSeries if this series originated as a MAC series.
// If not, ToMAC returns nil.
//
// ToMAC implementations can be called with a nil receiver,
// enabling you to chain this method with methods that might return a nil pointer.
func (addr WrappedAddress) ToMAC() MACAddressSegmentSeries {
return addr.Address.ToMAC()
}
// AdjustPrefixLen increases or decreases the prefix length by the given increment.
//
// A prefix length will not be adjusted lower than zero or beyond the bit length of the series.
//
// If this series has no prefix length, then the prefix length will be set to the adjustment if positive,
// or it will be set to the adjustment added to the bit count if negative.
func (addr WrappedAddress) AdjustPrefixLen(prefixLen BitCount) ExtendedSegmentSeries {
return wrapAddress(addr.Address.AdjustPrefixLen(prefixLen))
}
// AdjustPrefixLenZeroed increases or decreases the prefix length by the given increment while zeroing out the bits that have moved into or outside the prefix.
//
// A prefix length will not be adjusted lower than zero or beyond the bit length of the series.
//
// If this series has no prefix length, then the prefix length will be set to the adjustment if positive,
// or it will be set to the adjustment added to the bit count if negative.
//
// When prefix length is increased, the bits moved within the prefix become zero.
// When a prefix length is decreased, the bits moved outside the prefix become zero.
func (addr WrappedAddress) AdjustPrefixLenZeroed(prefixLen BitCount) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
return wrapAddrWithErr(addr.Address.AdjustPrefixLenZeroed(prefixLen))
}
// AssignPrefixForSingleBlock returns the equivalent prefix block that matches exactly the range of values in this series.
// The returned block will have an assigned prefix length indicating the prefix length for the block.
//
// There may be no such series - it is required that the range of values match the range of a prefix block.
// If there is no such series, then nil is returned.
func (addr WrappedAddress) AssignPrefixForSingleBlock() ExtendedSegmentSeries {
return convAddrToIntf(addr.Address.AssignPrefixForSingleBlock())
}
// AssignMinPrefixForBlock returns an equivalent series, assigned the smallest prefix length possible,
// such that the prefix block for that prefix length is in this series.
//
// In other words, this method assigns a prefix length to this series matching the largest prefix block in this series.
func (addr WrappedAddress) AssignMinPrefixForBlock() ExtendedSegmentSeries {
return wrapAddress(addr.Address.AssignMinPrefixForBlock())
}
// Contains returns whether this is same type and version as the given address series and whether it contains all values in the given series.
//
// Series must also have the same number of segments to be comparable, otherwise false is returned.
func (addr WrappedAddress) Contains(other ExtendedSegmentSeries) bool {
a, ok := other.Unwrap().(AddressType)
return ok && addr.Address.Contains(a)
}
// Iterator provides an iterator to iterate through the individual series of this series.
//
// When iterating, the prefix length is preserved.
// Remove it using WithoutPrefixLen prior to iterating if you wish to drop it from all individual series.
//
// Call IsMultiple to determine if this instance represents multiple series, or GetCount for the count.
func (addr WrappedAddress) Iterator() Iterator[ExtendedSegmentSeries] {
return addressSeriesIterator{addr.Address.Iterator()}
}
// PrefixIterator provides an iterator to iterate through the individual prefixes of this series,
// each iterated element spanning the range of values for its prefix.
//
// It is similar to the prefix block iterator,
// except for possibly the first and last iterated elements,
// which might not be prefix blocks,
// instead constraining themselves to values from this series.
//
// If the series has no prefix length, then this is equivalent to Iterator.
func (addr WrappedAddress) PrefixIterator() Iterator[ExtendedSegmentSeries] {
return addressSeriesIterator{addr.Address.PrefixIterator()}
}
// PrefixBlockIterator provides an iterator to iterate through the individual prefix blocks, one for each prefix of this series.
// Each iterated series will be a prefix block with the same prefix length as this series.
//
// If this series has no prefix length, then this is equivalent to Iterator.
func (addr WrappedAddress) PrefixBlockIterator() Iterator[ExtendedSegmentSeries] {
return addressSeriesIterator{addr.Address.PrefixBlockIterator()}
}
// ToBlock creates a new series block by changing the segment at the given index to have the given lower and upper value,
// and changing the following segments to be full-range.
func (addr WrappedAddress) ToBlock(segmentIndex int, lower, upper SegInt) ExtendedSegmentSeries {
return wrapAddress(addr.Address.ToBlock(segmentIndex, lower, upper))
}
// ToPrefixBlock returns the series with the same prefix as this series while the remaining bits span all values.
// The series will be the block of all series with the same prefix.
//
// If this series has no prefix, this series is returned.
func (addr WrappedAddress) ToPrefixBlock() ExtendedSegmentSeries {
return wrapAddress(addr.Address.ToPrefixBlock())
}
// ToPrefixBlockLen returns the series with the same prefix of the given length as this series while the remaining bits span all values.
// The returned series will be the block of all series with the same prefix.
func (addr WrappedAddress) ToPrefixBlockLen(prefLen BitCount) ExtendedSegmentSeries {
return wrapAddress(addr.Address.ToPrefixBlockLen(prefLen))
}
// Increment returns the item that is the given increment upwards into the range,
// with the increment of 0 returning the first in the range.
//
// If the increment i matches or exceeds the range count c, then i - c + 1
// is added to the upper item of the range.
// An increment matching the count gives you the item just above the highest in the range.
//
// If the increment is negative, it is added to the lowest of the range.
// To get the item just below the lowest of the range, use the increment -1.
//
// If this represents just a single value, the item is simply incremented by the given increment, positive or negative.
//
// If this item represents multiple values, a positive increment i is equivalent i + 1 values from the iterator and beyond.
// For instance, a increment of 0 is the first value from the iterator, an increment of 1 is the second value from the iterator, and so on.
// An increment of a negative value added to the count is equivalent to the same number of iterator values preceding the last value of the iterator.
// For instance, an increment of count - 1 is the last value from the iterator, an increment of count - 2 is the second last value, and so on.
//
// On overflow or underflow, Increment returns nil.
func (addr WrappedAddress) Increment(i int64) ExtendedSegmentSeries {
return convAddrToIntf(addr.Address.Increment(i))
}
// IncrementBoundary returns the item that is the given increment from the range boundaries of this item.
//
// If the given increment is positive, adds the value to the highest (GetUpper) in the range to produce a new item.
// If the given increment is negative, adds the value to the lowest (GetLower) in the range to produce a new item.
// If the increment is zero, returns this.
//
// If this represents just a single value, this item is simply incremented by the given increment value, positive or negative.
//
// On overflow or underflow, IncrementBoundary returns nil.
func (addr WrappedAddress) IncrementBoundary(i int64) ExtendedSegmentSeries {
return convAddrToIntf(addr.Address.IncrementBoundary(i))
}
// GetLower returns the series in the range with the lowest numeric value,
// which will be the same series if it represents a single value.
func (addr WrappedAddress) GetLower() ExtendedSegmentSeries {
return wrapAddress(addr.Address.GetLower())
}
// GetUpper returns the series in the range with the highest numeric value,
// which will be the same series if it represents a single value.
func (addr WrappedAddress) GetUpper() ExtendedSegmentSeries {
return wrapAddress(addr.Address.GetUpper())
}
// WithoutPrefixLen provides the same address series but with no prefix length.
// The values remain unchanged.
func (addr WrappedAddress) WithoutPrefixLen() ExtendedSegmentSeries {
return wrapAddress(addr.Address.WithoutPrefixLen())
}
// SetPrefixLen sets the prefix length.
//
// A prefix length will not be set to a value lower than zero or beyond the bit length of the series.
// The provided prefix length will be adjusted to these boundaries if necessary.
func (addr WrappedAddress) SetPrefixLen(prefixLen BitCount) ExtendedSegmentSeries {
return wrapAddress(addr.Address.SetPrefixLen(prefixLen))
}
// SetPrefixLenZeroed sets the prefix length.
//
// A prefix length will not be set to a value lower than zero or beyond the bit length of the series.
// The provided prefix length will be adjusted to these boundaries if necessary.
//
// If this series has a prefix length, and the prefix length is increased when setting the new prefix length,
// the bits moved within the prefix become zero.
// If this series has a prefix length, and the prefix length is decreased when setting the new prefix length,
// the bits moved outside the prefix become zero.
//
// In other words,
// bits that move from one side of the prefix length to the other
// (bits moved into the prefix or outside the prefix) are zeroed.
//
// If the result cannot be zeroed because zeroing out bits results in a non-contiguous segment, an error is returned.
func (addr WrappedAddress) SetPrefixLenZeroed(prefixLen BitCount) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
return wrapAddrWithErr(addr.Address.SetPrefixLenZeroed(prefixLen))
}
// ReverseBytes returns a new segment series with the bytes reversed. Any prefix length is dropped.
//
// If each segment is more than 1 byte long,
// and the bytes within a single segment cannot be reversed because the segment represents a range,
// and reversing the segment values results in a range that is not contiguous,
// then this returns an error.
//
// In practice this means that to be reversible,
// a range must include all values except possibly the largest and/or smallest, which reverse to themselves.
func (addr WrappedAddress) ReverseBytes() (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
return wrapAddrWithErr(addr.Address.ReverseBytes())
}
// ReverseBits returns a new segment series with the bits reversed. Any prefix length is dropped.
//
// If the bits within a single segment cannot be reversed because the segment represents a range,
// and reversing the segment values results in a range that is not contiguous, this returns an error.
//
// In practice this means that to be reversible,
// a range must include all values except possibly the largest and/or smallest,
// which reverse to themselves.
//
// If perByte is true, the bits are reversed within each byte, otherwise all the bits are reversed.
func (addr WrappedAddress) ReverseBits(perByte bool) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
a, err := addr.Address.ReverseBits(perByte)
if err != nil {
return nil, err
}
return wrapAddress(a), nil
}
// ReverseSegments returns a new series with the segments reversed.
func (addr WrappedAddress) ReverseSegments() ExtendedSegmentSeries {
return wrapAddress(addr.Address.ReverseSegments())
}
// WrappedAddressSection is the implementation of ExtendedSegmentSeries for address sections.
type WrappedAddressSection struct {
*AddressSection
}
// GetSection returns the backing section for this series, comprising all segments.
func (section WrappedAddressSection) GetSection() *AddressSection {
return section.AddressSection
}
// Contains returns whether this is same type and version as the given address series and whether it contains all values in the given series.
//
// Series must also have the same number of segments to be comparable, otherwise false is returned.
func (section WrappedAddressSection) Contains(other ExtendedSegmentSeries) bool {
s, ok := other.Unwrap().(AddressSectionType)
return ok && section.AddressSection.Contains(s)
}
// Equal returns whether the given address series is equal to this address series.
// Two address series are equal if they represent the same set of series.
// Both must be equal sections.
func (section WrappedAddressSection) Equal(other ExtendedSegmentSeries) bool {
s, ok := other.Unwrap().(AddressSectionType)
return ok && section.AddressSection.Equal(s)
}
// Unwrap returns the wrapped address section as an interface, AddressSegmentSeries.
func (section WrappedAddressSection) Unwrap() AddressSegmentSeries {
res := section.AddressSection
if res == nil {
return nil
}
return res
}
// ToIPv4 converts to an IPv4AddressSegmentSeries if this series originated as an IPv4 series.
// If not, ToIPv4 returns nil.
//
// ToIPv4 implementations can be called with a nil receiver,
// enabling you to chain this method with methods that might return a nil pointer.
func (section WrappedAddressSection) ToIPv4() IPv4AddressSegmentSeries {
return section.AddressSection.ToIPv4()
}
// ToIPv6 converts to an IPv4AddressSegmentSeries if this series originated as an IPv6 series.
// If not, ToIPv6 returns nil.
//
// ToIPv6 implementations can be called with a nil receiver,
// enabling you to chain this method with methods that might return a nil pointer.
func (section WrappedAddressSection) ToIPv6() IPv6AddressSegmentSeries {
return section.AddressSection.ToIPv6()
}
// ToIP converts to an IP address section if this originated as IPv4 or IPv6, or an implicitly zero-valued IP.
// If not, ToIP returns nil.
func (section WrappedAddressSection) ToIP() IPAddressSegmentSeries {
return section.AddressSection.ToIP()
}
// ToMAC converts to a MACAddressSegmentSeries if this series originated as a MAC series.
// If not, ToMAC returns nil.
//
// ToMAC implementations can be called with a nil receiver,
// enabling you to chain this method with methods that might return a nil pointer.
func (section WrappedAddressSection) ToMAC() MACAddressSegmentSeries {
return section.AddressSection.ToMAC()
}
// Iterator provides an iterator to iterate through the individual series of this series.
//
// When iterating, the prefix length is preserved.
// Remove it using WithoutPrefixLen prior to iterating if you wish to drop it from all individual series.
//
// Call IsMultiple to determine if this instance represents multiple series, or GetCount for the count.
func (section WrappedAddressSection) Iterator() Iterator[ExtendedSegmentSeries] {
return sectionSeriesIterator{section.AddressSection.Iterator()}
}
// PrefixIterator provides an iterator to iterate through the individual prefixes of this series,
// each iterated element spanning the range of values for its prefix.
//
// It is similar to the prefix block iterator,
// except for possibly the first and last iterated elements,
// which might not be prefix blocks,
// instead constraining themselves to values from this series.
//
// If the series has no prefix length, then this is equivalent to Iterator.
func (section WrappedAddressSection) PrefixIterator() Iterator[ExtendedSegmentSeries] {
return sectionSeriesIterator{section.AddressSection.PrefixIterator()}
}
// PrefixBlockIterator provides an iterator to iterate through the individual prefix blocks,
// one for each prefix of this series.
// Each iterated series will be a prefix block with the same prefix length as this series.
//
// If this series has no prefix length, then this is equivalent to Iterator.
func (section WrappedAddressSection) PrefixBlockIterator() Iterator[ExtendedSegmentSeries] {
return sectionSeriesIterator{section.AddressSection.PrefixBlockIterator()}
}
// ToBlock creates a new series block by changing the segment at the given index to have the given lower and upper value,
// and changing the following segments to be full-range.
func (section WrappedAddressSection) ToBlock(segmentIndex int, lower, upper SegInt) ExtendedSegmentSeries {
return wrapSection(section.AddressSection.ToBlock(segmentIndex, lower, upper))
}
// ToPrefixBlock returns the series with the same prefix as this series while the remaining bits span all values.
// The series will be the block of all series with the same prefix.
//
// If this series has no prefix, this series is returned.
func (section WrappedAddressSection) ToPrefixBlock() ExtendedSegmentSeries {
return wrapSection(section.AddressSection.ToPrefixBlock())
}
// ToPrefixBlockLen returns the series with the same prefix of the given length as this series while the remaining bits span all values.
// The returned series will be the block of all series with the same prefix.
func (section WrappedAddressSection) ToPrefixBlockLen(prefLen BitCount) ExtendedSegmentSeries {
return wrapSection(section.AddressSection.ToPrefixBlockLen(prefLen))
}
// Increment returns the item that is the given increment upwards into the range,
// with the increment of 0 returning the first in the range.
//
// If the increment i matches or exceeds the range count c, then i - c + 1
// is added to the upper item of the range.
// An increment matching the count gives you the item just above the highest in the range.
//
// If the increment is negative, it is added to the lowest of the range.
// To get the item just below the lowest of the range, use the increment -1.
//
// If this represents just a single value, the item is simply incremented by the given increment, positive or negative.
//
// If this item represents multiple values, a positive increment i is equivalent i + 1 values from the iterator and beyond.
// For instance, a increment of 0 is the first value from the iterator, an increment of 1 is the second value from the iterator, and so on.
// An increment of a negative value added to the count is equivalent to the same number of iterator values preceding the last value of the iterator.
// For instance, an increment of count - 1 is the last value from the iterator, an increment of count - 2 is the second last value, and so on.
//
// On overflow or underflow, Increment returns nil.
func (section WrappedAddressSection) Increment(i int64) ExtendedSegmentSeries {
return convSectToIntf(section.AddressSection.Increment(i))
}
// IncrementBoundary returns the item that is the given increment from the range boundaries of this item.
//
// If the given increment is positive, adds the value to the highest (GetUpper) in the range to produce a new item.
// If the given increment is negative, adds the value to the lowest (GetLower) in the range to produce a new item.
// If the increment is zero, returns this.
//
// If this represents just a single value,
// this item is simply incremented by the given increment value,
// positive or negative.
//
// On overflow or underflow, IncrementBoundary returns nil.
func (section WrappedAddressSection) IncrementBoundary(i int64) ExtendedSegmentSeries {
return convSectToIntf(section.AddressSection.IncrementBoundary(i))
}
// GetLower returns the series in the range with the lowest numeric value,
// which will be the same series if it represents a single value.
func (section WrappedAddressSection) GetLower() ExtendedSegmentSeries {
return wrapSection(section.AddressSection.GetLower())
}
// GetUpper returns the series in the range with the highest numeric value,
// which will be the same series if it represents a single value.
func (section WrappedAddressSection) GetUpper() ExtendedSegmentSeries {
return wrapSection(section.AddressSection.GetUpper())
}
// AssignPrefixForSingleBlock returns the equivalent prefix block that matches exactly the range of values in this series.
// The returned block will have an assigned prefix length indicating the prefix length for the block.
//
// There may be no such series - it is required that the range of values match the range of a prefix block.
// If there is no such series, then nil is returned.
func (section WrappedAddressSection) AssignPrefixForSingleBlock() ExtendedSegmentSeries {
return convSectToIntf(section.AddressSection.AssignPrefixForSingleBlock())
}
// AssignMinPrefixForBlock returns an equivalent series, assigned the smallest prefix length possible,
// such that the prefix block for that prefix length is in this series.
//
// In other words,
// this method assigns a prefix length to this series matching the largest prefix block in this series.
func (section WrappedAddressSection) AssignMinPrefixForBlock() ExtendedSegmentSeries {
return wrapSection(section.AddressSection.AssignMinPrefixForBlock())
}
// WithoutPrefixLen provides the same address series but with no prefix length. The values remain unchanged.
func (section WrappedAddressSection) WithoutPrefixLen() ExtendedSegmentSeries {
return wrapSection(section.AddressSection.WithoutPrefixLen())
}
// SetPrefixLen sets the prefix length.
//
// A prefix length will not be set to a value lower than zero or beyond the bit length of the series.
// The provided prefix length will be adjusted to these boundaries if necessary.
func (section WrappedAddressSection) SetPrefixLen(prefixLen BitCount) ExtendedSegmentSeries {
return wrapSection(section.AddressSection.SetPrefixLen(prefixLen))
}
// SetPrefixLenZeroed sets the prefix length.
//
// A prefix length will not be set to a value lower than zero or beyond the bit length of the series.
// The provided prefix length will be adjusted to these boundaries if necessary.
//
// If this series has a prefix length, and the prefix length is increased when setting the new prefix length, the bits moved within the prefix become zero.
// If this series has a prefix length, and the prefix length is decreased when setting the new prefix length, the bits moved outside the prefix become zero.
//
// In other words, bits that move from one side of the prefix length to the other (bits moved into the prefix or outside the prefix) are zeroed.
//
// If the result cannot be zeroed because zeroing out bits results in a non-contiguous segment, an error is returned.
func (section WrappedAddressSection) SetPrefixLenZeroed(prefixLen BitCount) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
return wrapSectWithErr(section.AddressSection.SetPrefixLenZeroed(prefixLen))
}
// AdjustPrefixLen increases or decreases the prefix length by the given increment.
//
// A prefix length will not be adjusted lower than zero or beyond the bit length of the series.
//
// If this series has no prefix length, then the prefix length will be set to the adjustment if positive,
// or it will be set to the adjustment added to the bit count if negative.
func (section WrappedAddressSection) AdjustPrefixLen(adjustment BitCount) ExtendedSegmentSeries {
return wrapSection(section.AddressSection.AdjustPrefixLen(adjustment))
}
// AdjustPrefixLenZeroed increases or decreases the prefix length by the given increment while zeroing out the bits that have moved into or outside the prefix.
//
// A prefix length will not be adjusted lower than zero or beyond the bit length of the series.
//
// If this series has no prefix length, then the prefix length will be set to the adjustment if positive,
// or it will be set to the adjustment added to the bit count if negative.
//
// When prefix length is increased, the bits moved within the prefix become zero.
// When a prefix length is decreased, the bits moved outside the prefix become zero.
func (section WrappedAddressSection) AdjustPrefixLenZeroed(adjustment BitCount) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
return wrapSectWithErr(section.AddressSection.AdjustPrefixLenZeroed(adjustment))
}
// ReverseBytes returns a new segment series with the bytes reversed.
// Any prefix length is dropped.
//
// If each segment is more than 1 byte long,
// and the bytes within a single segment cannot be reversed because the segment represents a range,
// and reversing the segment values results in a range that is not contiguous, then this returns an error.
//
// In practice this means that to be reversible,
// a range must include all values except possibly the largest and/or smallest, which reverse to themselves.
func (section WrappedAddressSection) ReverseBytes() (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
return wrapSectWithErr(section.AddressSection.ReverseBytes())
}
// ReverseBits returns a new segment series with the bits reversed.
// Any prefix length is dropped.
//
// If the bits within a single segment cannot be reversed because the segment represents a range,
// and reversing the segment values results in a range that is not contiguous, this returns an error.
//
// In practice this means that to be reversible,
// a range must include all values except possibly the largest and/or smallest, which reverse to themselves.
//
// If perByte is true, the bits are reversed within each byte, otherwise all the bits are reversed.
func (section WrappedAddressSection) ReverseBits(perByte bool) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
return wrapSectWithErr(section.AddressSection.ReverseBits(perByte))
}
// ReverseSegments returns a new series with the segments reversed.
func (section WrappedAddressSection) ReverseSegments() ExtendedSegmentSeries {
return wrapSection(section.AddressSection.ReverseSegments())
}
func wrapAddress(addr *Address) WrappedAddress {
return WrappedAddress{addr}
}
func wrapSection(section *AddressSection) WrappedAddressSection {
return WrappedAddressSection{section}
}
func wrapSectWithErr(section *AddressSection, err address_error.IncompatibleAddressError) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
if err == nil {
return wrapSection(section), nil
}
return nil, err
}
func wrapAddrWithErr(addr *Address, err address_error.IncompatibleAddressError) (ExtendedSegmentSeries, address_error.IncompatibleAddressError) {
if err == nil {
return wrapAddress(addr), nil
}
return nil, err
}
// In go, a nil value is not converted to a nil interface,
// it is converted to a non-nil interface instance with underlying value nil.
func convAddrToIntf(addr *Address) ExtendedSegmentSeries {
if addr == nil {
return nil
}
return wrapAddress(addr)
}
func convSectToIntf(sect *AddressSection) ExtendedSegmentSeries {
if sect == nil {
return nil
}
return wrapSection(sect)
}