aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/util/array.go
blob: c6d63714bd1b56038ebd0336264eb65ac8976adb (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019, 2020 Bernd Fix  >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package util

import (
	"fmt"
)

// Error variables
var (
	ErrUtilArrayTooSmall = fmt.Errorf("array to small")
)

//----------------------------------------------------------------------
// generic array helpers
//----------------------------------------------------------------------

// Clone creates a new array of same content as the argument.
func Clone[T []E, E any](d T) T {
	r := make(T, len(d))
	copy(r, d)
	return r
}

// Equals returns true if two arrays match.
func Equals[T []E, E comparable](a, b T) bool {
	if len(a) != len(b) {
		return false
	}
	for i, e := range a {
		if e != b[i] {
			return false
		}
	}
	return true
}

// Reverse the content of an array
func Reverse[T []E, E any](b T) T {
	bl := len(b)
	r := make(T, bl)
	for i := 0; i < bl; i++ {
		r[bl-i-1] = b[i]
	}
	return r
}

// IsAll returns true if all elements in an array are set to null.
func IsAll[T []E, E comparable](b T, null E) bool {
	for _, v := range b {
		if v != null {
			return false
		}
	}
	return true
}

// Fill an array with a value
func Fill[T []E, E any](b T, val E) {
	for i := range b {
		b[i] = val
	}
}

//----------------------------------------------------------------------
// byte array helpers
//----------------------------------------------------------------------

// CopyAlignedBlock copies 'in' to 'out' so that 'out' is filled completely.
// - If 'in' is larger than 'out', it is left-truncated before copy
// - If 'in' is smaller than 'out', it is left-padded with 0 before copy
func CopyAlignedBlock(out, in []byte) {
	count := len(in)
	size := len(out)
	from, to := 0, 0
	if count > size {
		from = count - size
	} else if count < size {
		to = size - count
		for i := 0; i < to; i++ {
			out[i] = 0
		}
	}
	copy(out[to:], in[from:])
}

//----------------------------------------------------------------------
// String list helpers
//----------------------------------------------------------------------

// StringList converts a binary representation of a string list. Each string
// is '\0'-terminated. The whole byte array is parsed; if the final string is
// not terminated, it is skipped.
func StringList(b []byte) []string {
	res := make([]string, 0)
	str := ""
	for _, ch := range b {
		if ch == 0 {
			res = append(res, str)
			str = ""
			continue
		}
		str += string(ch)
	}
	return res
}