github.com/tunabay/go-bitarray@v1.3.1/bitarray_slice_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 "fmt" 9 10 "github.com/tunabay/go-bitarray" 11 ) 12 13 func ExampleBitArray_Slice() { 14 ba := bitarray.MustParse("0011-1010 0110-1111 110") 15 fmt.Printf("% s\n", ba.Slice(4, 14)) 16 fmt.Printf("% s\n", ba.Slice(9, 13)) 17 18 // Output: 19 // 10100110 11 20 // 1101 21 } 22 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 33 func ExampleBitArray_ToWidth() { 34 ba := bitarray.MustParse("1010-1111 0000-11") 35 fmt.Printf("% s\n", ba.ToWidth(7, bitarray.AlignLeft)) 36 fmt.Printf("% s\n", ba.ToWidth(7, bitarray.AlignRight)) 37 fmt.Printf("% s\n", ba.ToWidth(20, bitarray.AlignLeft)) 38 fmt.Printf("% s\n", ba.ToWidth(20, bitarray.AlignRight)) 39 40 // Output: 41 // 1010111 42 // 1000011 43 // 10101111 00001100 0000 44 // 00000010 10111100 0011 45 } 46 47 func ExampleBitArray_TrimPrefix() { 48 ba1 := bitarray.MustParse("1010-1011 0011-0011 01") 49 ba2 := bitarray.MustParse("1010-1111 1111") 50 ba3 := bitarray.MustParse("1010-10") 51 prefix := bitarray.MustParse("1010-10") 52 fmt.Printf("% s\n", ba1.TrimPrefix(prefix)) 53 fmt.Printf("% s\n", ba2.TrimPrefix(prefix)) 54 fmt.Printf("[% s]\n", ba3.TrimPrefix(prefix)) 55 56 // Output: 57 // 11001100 1101 58 // 10101111 1111 59 // [] 60 } 61 62 func ExampleBitArray_TrimSuffix() { 63 ba1 := bitarray.MustParse("1010-1011 0011-00") 64 ba2 := bitarray.MustParse("1111-1000 11") 65 ba3 := bitarray.MustParse("1100") 66 suffix := bitarray.MustParse("1100") 67 fmt.Printf("% s\n", ba1.TrimSuffix(suffix)) 68 fmt.Printf("% s\n", ba2.TrimSuffix(suffix)) 69 fmt.Printf("[% s]\n", ba3.TrimSuffix(suffix)) 70 71 // Output: 72 // 10101011 00 73 // 11111000 11 74 // [] 75 } 76 77 func ExampleBitArray_TrimLeadingZeros() { 78 ba1 := bitarray.MustParse("0000-0010 1100-11") 79 ba2 := bitarray.MustParse("1000-0000 0") 80 ba3 := bitarray.MustParse("0000-0000 0000-0000 0000") 81 fmt.Printf("% s\n", ba1.TrimLeadingZeros()) 82 fmt.Printf("% s\n", ba2.TrimLeadingZeros()) 83 fmt.Printf("[% s]\n", ba3.TrimLeadingZeros()) 84 85 // Output: 86 // 10110011 87 // 10000000 0 88 // [] 89 } 90 91 func ExampleBitArray_TrimTrailingZeros() { 92 ba1 := bitarray.MustParse("1010-0110 000") 93 ba2 := bitarray.MustParse("0000-0000 01") 94 ba3 := bitarray.MustParse("0000-0000 0000-0000 0000") 95 fmt.Printf("% s\n", ba1.TrimTrailingZeros()) 96 fmt.Printf("% s\n", ba2.TrimTrailingZeros()) 97 fmt.Printf("[% s]\n", ba3.TrimTrailingZeros()) 98 99 // Output: 100 // 1010011 101 // 00000000 01 102 // [] 103 }