aboutsummaryrefslogtreecommitdiff
path: root/ber
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-07-15 14:14:19 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-07-15 14:14:19 +0200
commited424d007c1f0830e3c73bcf3e35e93a2f0d22e6 (patch)
tree5aca7b42107c4aab63a8902304bdfe2d478aa981 /ber
parent6d033680eb74abe871e7b261e6911d646f9e365b (diff)
Add setter/getter
Diffstat (limited to 'ber')
-rw-r--r--ber/bitstring.go37
-rw-r--r--ber/bitstring_test.go17
2 files changed, 54 insertions, 0 deletions
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)
+ }
+}