aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-07-15 14:32:02 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-07-15 14:32:02 +0200
commita1c1227233f35978dd00ab8de541c7c763bbcd72 (patch)
tree570bb816502518aa0fdf4ee9d83579928eebdcc4
parented424d007c1f0830e3c73bcf3e35e93a2f0d22e6 (diff)
Expand to powers of 2
-rw-r--r--ber/bitstring.go12
-rw-r--r--ber/bitstring_test.go10
2 files changed, 16 insertions, 6 deletions
diff --git a/ber/bitstring.go b/ber/bitstring.go
index 7e0fd93..eabc354 100644
--- a/ber/bitstring.go
+++ b/ber/bitstring.go
@@ -54,9 +54,17 @@ func (bs BitString) IsSet(n int) bool {
return bs[n]
}
+func pow2(n int) int {
+ i := 1
+ for i < n+1 {
+ i <<= 1
+ }
+ return i
+}
+
func (bs *BitString) Set(n int) {
if len(*bs) < n {
- nbs := make(BitString, n+1)
+ nbs := make(BitString, pow2(n))
copy(nbs, *bs)
*bs = nbs
}
@@ -65,7 +73,7 @@ func (bs *BitString) Set(n int) {
func (bs *BitString) Clear(n int) {
if len(*bs) < n {
- nbs := make(BitString, n+1)
+ nbs := make(BitString, pow2(n))
copy(nbs, *bs)
*bs = nbs
}
diff --git a/ber/bitstring_test.go b/ber/bitstring_test.go
index babde4d..3c67228 100644
--- a/ber/bitstring_test.go
+++ b/ber/bitstring_test.go
@@ -55,16 +55,18 @@ func TestBitString(t *testing.T) {
func TestSet(t *testing.T) {
bs := BitString{true, false}
- bs.Set(3)
- ex := BitString{true, false, false, true}
+ bs.Set(4)
+ ex := BitString{true, false, false, false, true, false, false, false}
if !bs.Equal(ex) {
t.Error("expexted", ex, "got", bs)
}
- bs.Clear(0)
- ex = BitString{false, false, false, true}
+ bs = bs[:4]
+ bs.Set(3)
+ ex = BitString{true, false, false, true}
if !bs.Equal(ex) {
t.Error("expexted", ex, "got", bs)
}
+ bs.Clear(0)
if bs.IsSet(0) {
t.Error("got", bs)
}