summaryrefslogtreecommitdiffstats
path: root/lib/byte.go
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-06-05 16:11:06 -0400
committersiddharth <s@ricketyspace.net>2021-06-05 16:11:06 -0400
commit6b06f77ce077e421a6219c454f1c8a43fe329c33 (patch)
tree8a8b66611d7fae55a7343ce9325222acdf40752a /lib/byte.go
parent1284c582a914c942dd2bd21b1749bf6567515d2b (diff)
lib: add byte.go
- Add `ByteInBytes` function - Add `BytesInCommon` function
Diffstat (limited to 'lib/byte.go')
-rw-r--r--lib/byte.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/byte.go b/lib/byte.go
new file mode 100644
index 0000000..744c0d7
--- /dev/null
+++ b/lib/byte.go
@@ -0,0 +1,31 @@
+// Copyright © 2020 rsiddharth <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package lib
+
+// Returns true if byte `b` is in `bs`
+func ByteInBytes(b byte, bs []byte) bool {
+ for _, bi := range bs {
+ if b == bi {
+ return true
+ }
+ }
+ return false
+}
+
+// Returns bytes that are common in the given array of array of bytes `bbytes`.
+func BytesInCommon(bbytes [][]byte) []byte {
+ common := make([]byte, 0)
+ switch l := len(bbytes); {
+ case l == 1:
+ common = bbytes[0]
+ case l > 1:
+ commonRest := BytesInCommon(bbytes[1:])
+ for _, b := range bbytes[0] {
+ if ByteInBytes(b, commonRest) {
+ common = append(common, b)
+ }
+ }
+ }
+ return common
+}