summaryrefslogtreecommitdiffstats
path: root/lib/byte.go
blob: 7e5ead4d9a8eedb005eb64fef2566a3b62ace118 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright © 2021 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 {
	var common []byte
	switch l := len(bbytes); {
	case l == 1:
		common = make([]byte, len(bbytes[0]))
		copy(common, bbytes[0])
	case l > 1:
		common = make([]byte, 0)
		commonRest := BytesInCommon(bbytes[1:])
		for _, b := range bbytes[0] {
			if ByteInBytes(b, commonRest) {
				common = append(common, b)
			}
		}
	}
	return common
}

func ByteIsUpper(b byte) bool {
	if 'A' <= b && b <= 'Z' {
		return true
	}
	return false
}