From ed424d007c1f0830e3c73bcf3e35e93a2f0d22e6 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 15 Jul 2015 14:14:19 +0200 Subject: Add setter/getter --- ber/bitstring.go | 37 +++++++++++++++++++++++++++++++++++++ ber/bitstring_test.go | 17 +++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/ber/bitstring.go b/ber/bitstring.go index 2195143..7e0fd93 100644 --- a/ber/bitstring.go +++ b/ber/bitstring.go @@ -46,3 +46,40 @@ func (bs BitString) String() (s string) { } return } + +func (bs BitString) IsSet(n int) bool { + if n > len(bs) { + return false + } + return bs[n] +} + +func (bs *BitString) Set(n int) { + if len(*bs) < n { + nbs := make(BitString, n+1) + copy(nbs, *bs) + *bs = nbs + } + (*bs)[n] = true +} + +func (bs *BitString) Clear(n int) { + if len(*bs) < n { + nbs := make(BitString, n+1) + copy(nbs, *bs) + *bs = nbs + } + (*bs)[n] = false +} + +func (bs BitString) Equal(s BitString) bool { + if len(bs) != len(s) { + return false + } + for i := 0; i < len(bs); i++ { + if bs[i] != s[i] { + return false + } + } + return true +} diff --git a/ber/bitstring_test.go b/ber/bitstring_test.go index ae4051d..babde4d 100644 --- a/ber/bitstring_test.go +++ b/ber/bitstring_test.go @@ -52,3 +52,20 @@ func TestBitString(t *testing.T) { } } } + +func TestSet(t *testing.T) { + bs := BitString{true, false} + bs.Set(3) + ex := BitString{true, false, false, true} + if !bs.Equal(ex) { + t.Error("expexted", ex, "got", bs) + } + bs.Clear(0) + ex = BitString{false, false, false, true} + if !bs.Equal(ex) { + t.Error("expexted", ex, "got", bs) + } + if bs.IsSet(0) { + t.Error("got", bs) + } +} -- cgit v1.2.3