File: bytes.go

package info (click to toggle)
irtt 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 600 kB
  • sloc: sh: 60; makefile: 9
file content (45 lines) | stat: -rw-r--r-- 683 bytes parent folder | download
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
42
43
44
45
package irtt

import (
	"encoding/hex"
	"strings"
)

// bytes helpers

// make zeroes array so we can use copy builtin for fast zero-ing
var zeroes = make([]byte, 64*1024)

func decodeHexOrNot(s string) (b []byte, err error) {
	if strings.HasPrefix(s, "0x") {
		b, err = hex.DecodeString(s[2:])
		return
	}
	b = []byte(s)
	return
}

func bytesEqual(a, b []byte) bool {
	if a == nil && b == nil {
		return true
	}
	if a == nil || b == nil {
		return false
	}
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func zero(b []byte) {
	if len(b) > len(zeroes) {
		zeroes = make([]byte, len(b)*2)
	}
	copy(b, zeroes)
}