github.com/tunabay/go-bitarray@v1.3.1/builder_example_test.go (about) 1 // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved. 2 // Use of this source code is governed by the MIT license that can be found in 3 // the LICENSE file. 4 5 package bitarray_test 6 7 import ( 8 "bytes" 9 "fmt" 10 "io" 11 12 "github.com/tunabay/go-bitarray" 13 ) 14 15 func ExampleBuilder() { 16 b := bitarray.NewBuilder() 17 18 // Add bits with various methods. 19 b.WriteBitsFromBytes([]byte{0b_1100_0111}, 2, 3) // 000 20 b.WriteByte(0xFF) // 11111111 21 b.WriteBit(0) // 0 22 b.WriteBitArray(bitarray.MustParse("111000111")) // 111000111 23 b.WriteByteBits([]byte{0, 1, 0, 1}) // 0101 24 25 // Build a BitArray containing the accumulated bits. 26 ba := b.BitArray() 27 fmt.Printf("% b\n", ba) 28 29 // Output: 30 // 00011111 11101110 00111010 1 31 } 32 33 func ExampleNewBuilder() { 34 // Create an empty builder and add bitarrays. 35 b1 := bitarray.NewBuilder() 36 b1.WriteBitArray(bitarray.MustParse("1111")) 37 b1.WriteBitArray(bitarray.MustParse("000")) 38 fmt.Printf("%b\n", b1.BitArray()) 39 40 // Same as above. 41 b2 := bitarray.NewBuilder( 42 bitarray.MustParse("1111"), 43 bitarray.MustParse("000"), 44 ) 45 fmt.Printf("%b\n", b2.BitArray()) 46 47 // Output: 48 // 1111000 49 // 1111000 50 } 51 52 func ExampleBuilder_BitArray() { 53 b := bitarray.NewBuilder() 54 55 b.WriteBitArray(bitarray.MustParse("1111-00")) 56 b.WriteBitArray(bitarray.MustParse("1100-11")) 57 58 ba := b.BitArray() 59 fmt.Printf("% b\n", ba) 60 61 // Output: 62 // 11110011 0011 63 } 64 65 func ExampleBuilder_String() { 66 b := bitarray.NewBuilder() 67 68 b.WriteBitArray(bitarray.MustParse("1111-00")) 69 b.WriteBitArray(bitarray.MustParse("1100-11")) 70 71 fmt.Println(b.String()) 72 fmt.Println(b.BitArray().String()) 73 74 // Output: 75 // 111100110011 76 // 111100110011 77 } 78 79 func ExampleBuilder_Reset() { 80 b := bitarray.NewBuilder() 81 82 b.WriteBitArray(bitarray.MustParse("1111-00")) 83 b.WriteBitArray(bitarray.MustParse("1100-11")) 84 85 b.Reset() 86 87 b.WriteBitArray(bitarray.MustParse("111")) 88 89 fmt.Println(b) 90 91 // Output: 92 // 111 93 } 94 95 func ExampleBuilder_Len() { 96 b := bitarray.NewBuilder() 97 b.WriteBitArray(bitarray.MustParse("1111-00")) 98 b.WriteBitArray(bitarray.MustParse("1100-11")) 99 100 fmt.Println(b.Len()) 101 102 // Output: 103 // 12 104 } 105 106 func ExampleBuilder_WriteBitsFromBytes() { 107 b := bitarray.NewBuilder() 108 109 src := []byte{0xF3, 0x50} // 1111-0011 0101-0000 110 111 b.WriteBitsFromBytes(src, 3, 4) // 1001 112 b.WriteBitsFromBytes(src, 8, 0) // (empty) 113 b.WriteBitsFromBytes(src, 5, 6) // 011010 114 b.WriteBitsFromBytes(src, 11, 3) // 100 115 116 fmt.Println(b) 117 118 // Output: 119 // 1001011010100 120 } 121 122 func ExampleBuilder_Write() { 123 b := bitarray.NewBuilder() 124 125 src := []byte{0xF0, 0xF0} // 1111-0000 1111-0000 126 n, err := b.Write(src) 127 if err != nil { 128 panic(err) 129 } 130 131 fmt.Println(n) 132 fmt.Println(b) 133 134 // Output: 135 // 2 136 // 1111000011110000 137 } 138 139 func ExampleBuilder_Write_copy() { 140 b := bitarray.NewBuilder(bitarray.MustParse("0000")) 141 142 src := bytes.NewReader([]byte{0xFF, 0x00, 0xAA}) 143 _, err := io.Copy(b, src) // uses b.Write() via src.WriteTo() 144 if err != nil { 145 panic(err) 146 } 147 148 ba := b.BitArray() 149 fmt.Printf("% b\n", ba) 150 151 // Output: 152 // 00001111 11110000 00001010 1010 153 } 154 155 func ExampleBuilder_ReadFrom() { 156 b := bitarray.NewBuilder(bitarray.MustParse("0000")) 157 158 src := bytes.NewReader([]byte{0xFF, 0x00, 0xAA}) 159 n, err := b.ReadFrom(src) 160 if err != nil { 161 panic(err) 162 } 163 fmt.Println(n) 164 165 ba := b.BitArray() 166 fmt.Printf("% b\n", ba) 167 168 // Output: 169 // 3 170 // 00001111 11110000 00001010 1010 171 } 172 173 func ExampleBuilder_WriteBit() { 174 b := bitarray.NewBuilder() 175 176 b.WriteBit(0) 177 b.WriteBit(1) 178 b.WriteBit(0) 179 b.WriteBit(1) 180 181 fmt.Println(b) 182 183 // Output: 184 // 0101 185 } 186 187 func ExampleBuilder_WriteBitArray() { 188 b := bitarray.NewBuilder() 189 190 b.WriteBitArray(bitarray.MustParse("111")) 191 b.WriteBitArray(bitarray.MustParse("0000")) 192 193 fmt.Println(b) 194 195 // Output: 196 // 1110000 197 } 198 199 func ExampleBuilder_WriteByteBits() { 200 b := bitarray.NewBuilder() 201 202 src := []byte{1, 0, 1, 0, 1, 0} 203 b.WriteByteBits(src) 204 205 fmt.Println(b) 206 207 // Output: 208 // 101010 209 }