-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
75 lines (66 loc) · 1.84 KB
/
fields.go
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
package testes
import (
"fmt"
"strings"
)
// CheckPairs returns true if the given args
// slice contains an even number of elements.
func CheckPairs[T any](args ...T) bool {
return len(args) > 1 && len(args)%2 == 0
}
// Fields2String returns a list of string
// representations of a variadic list of args.
func Fields2String[T any](args ...T) []string {
s := fmt.Sprint(args)
return strings.Fields(s)
}
// ToFields cleans and normalizes whitespace
// in a slice of strings.
func ToFields(elems ...string) []string {
return strings.Fields(strings.Join(elems, " "))
}
// Join concatenates the string representations of a variadic
// list of elements to create a single string. The separator
// string sep is placed between elements in the resulting string.
func Join[T any, E ~[]T](elems E, sep string) string {
switch len(elems) {
case 0:
return ""
case 1:
return fmt.Sprint(elems[0])
}
n := len(sep) * (len(elems) - 1)
for i := 0; i < len(elems); i++ {
n += Len(elems[i])
}
b := &strings.Builder{}
b.Grow(n)
fmt.Fprintf(b, "%v", elems[0])
for _, v := range elems[1:] {
fmt.Fprint(b, sep)
fmt.Fprintf(b, "%v", v)
}
return b.String()
}
// Args2Pairs returns a slice of string pairs from
// a standard slice of strings. Whitespace is trimmed
// and normalized.
//
// Args2Pairs will panic if the number of arguments
// is less than 2 or not a multiple of 2.
func Args2Pairs(args ...string) [][2]string {
if len(args) < 2 {
panic("args2pairs: must be at least 2 arguments")
}
if len(args)%2 != 0 {
panic("args2pairs: number of arguments must be multiple of 2")
}
// length of args is a positive even integer number of arguments
fields := ToFields(args...)
listLen := len(fields) / 2
list := make([][2]string, 0, listLen)
for i := 0; i < len(fields); i = +2 {
list = append(list, [2]string{fields[i], fields[i+1]})
}
return list
}