Browse Source

Added string list helpers.

Bernd Fix 3 months ago
parent
commit
f038179c3d
1 changed files with 39 additions and 9 deletions
  1. 39
    9
      src/gnunet/util/array.go

+ 39
- 9
src/gnunet/util/array.go View File

@@ -4,16 +4,23 @@ import (
4 4
 	"fmt"
5 5
 )
6 6
 
7
+// Error variables
7 8
 var (
8 9
 	ErrUtilArrayTooSmall = fmt.Errorf("Array to small")
9 10
 )
10 11
 
12
+//----------------------------------------------------------------------
13
+// Byte array helpers
14
+//----------------------------------------------------------------------
15
+
16
+// Clone creates a new array of same content as the argument.
11 17
 func Clone(d []byte) []byte {
12 18
 	r := make([]byte, len(d))
13 19
 	copy(r, d)
14 20
 	return r
15 21
 }
16 22
 
23
+// Reverse the content of a byte array
17 24
 func Reverse(b []byte) []byte {
18 25
 	bl := len(b)
19 26
 	r := make([]byte, bl)
@@ -23,15 +30,6 @@ func Reverse(b []byte) []byte {
23 30
 	return r
24 31
 }
25 32
 
26
-func ReverseStrings(s []string) []string {
27
-	sl := len(s)
28
-	r := make([]string, sl)
29
-	for i := 0; i < sl; i++ {
30
-		r[sl-i-1] = s[i]
31
-	}
32
-	return r
33
-}
34
-
35 33
 // CopyBlock copies 'in' to 'out' so that 'out' is filled completely.
36 34
 // - If 'in' is larger than 'out', it is left-truncated before copy
37 35
 // - If 'in' is smaller than 'out', it is left-padded with 0 before copy
@@ -50,8 +48,40 @@ func CopyBlock(out, in []byte) {
50 48
 	copy(out[to:], in[from:])
51 49
 }
52 50
 
51
+// Fill an array with a value
53 52
 func Fill(b []byte, val byte) {
54 53
 	for i := 0; i < len(b); i++ {
55 54
 		b[i] = val
56 55
 	}
57 56
 }
57
+
58
+//----------------------------------------------------------------------
59
+// String list helpers
60
+//----------------------------------------------------------------------
61
+
62
+// Reverse StringList reverse an array of strings
63
+func ReverseStringList(s []string) []string {
64
+	sl := len(s)
65
+	r := make([]string, sl)
66
+	for i := 0; i < sl; i++ {
67
+		r[sl-i-1] = s[i]
68
+	}
69
+	return r
70
+}
71
+
72
+// Convert a binary representation of a string list. Each string is '\0'-
73
+// terminated. The whole byte array is parsed; if the final string is not
74
+// terminated, it is skipped.
75
+func StringList(b []byte) []string {
76
+	res := make([]string, 0)
77
+	str := ""
78
+	for _, ch := range b {
79
+		if ch == 0 {
80
+			res = append(res, str)
81
+			str = ""
82
+			continue
83
+		}
84
+		str += string(ch)
85
+	}
86
+	return res
87
+}

Loading…
Cancel
Save