Skip to content

Commit 924fb33

Browse files
committed
Add SliceToEnd() methods.
1 parent 351c95f commit 924fb33

4 files changed

+30
-0
lines changed

bitarray_slice.go

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func (ba *BitArray) Slice(start, end int) *BitArray {
3838
return &BitArray{b: buf, nBits: nBits}
3939
}
4040

41+
// SliceToEnd is shorthand for Slice(start, ba.Len()) and returns the subpart
42+
// from the position specified start to the last bit.
43+
func (ba *BitArray) SliceToEnd(start int) *BitArray { return ba.Slice(start, ba.Len()) }
44+
4145
// ToWidth returns a new BitArray resized to wid bits with its contents
4246
// preserved. If wid is less than ba.Len(), some bits will be lost. If wid is
4347
// greater than be.Len(), the expanded space will be filled with 0s. In both

bitarray_slice_example_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ func ExampleBitArray_Slice() {
2020
// 1101
2121
}
2222

23+
func ExampleBitArray_SliceToEnd() {
24+
ba := bitarray.MustParse("0011-1010 01101")
25+
fmt.Printf("% b\n", ba.SliceToEnd(4))
26+
fmt.Printf("% b\n", ba.SliceToEnd(9))
27+
28+
// Output:
29+
// 10100110 1
30+
// 1101
31+
}
32+
2333
func ExampleBitArray_ToWidth() {
2434
ba := bitarray.MustParse("1010-1111 0000-11")
2535
fmt.Printf("% s\n", ba.ToWidth(7, bitarray.AlignLeft))

buffer_slice.go

+4
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ func (buf *Buffer) Slice(start, end int) *Buffer {
2929
off := buf.off + start
3030
return &Buffer{b: buf.b[off>>3:], nBits: end - start, off: off & 7}
3131
}
32+
33+
// SliceToEnd is shorthand for Slice(start, buf.Len()) and returns the subpart
34+
// from the position specified start to the last bit.
35+
func (buf *Buffer) SliceToEnd(start int) *Buffer { return buf.Slice(start, buf.Len()) }

buffer_slice_example_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ func ExampleBuffer_Slice() {
2424
// 1101
2525
}
2626

27+
func ExampleBuffer_SliceToEnd() {
28+
ba := bitarray.MustParse("0011-1010 0110-1")
29+
buf := bitarray.NewBufferFromBitArray(ba)
30+
31+
fmt.Println(buf.SliceToEnd(4))
32+
fmt.Println(buf.SliceToEnd(9))
33+
34+
// Output:
35+
// 101001101
36+
// 1101
37+
}
38+
2739
func ExampleBuffer_Slice_update() {
2840
ba := bitarray.MustParse("1000-0000 0000-01")
2941
buf := bitarray.NewBufferFromBitArray(ba)

0 commit comments

Comments
 (0)