forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.go
84 lines (75 loc) · 1.74 KB
/
misc.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
76
77
78
79
80
81
82
83
84
package utils
import (
"bytes"
"encoding/json"
"regexp"
"unicode/utf8"
)
// MapAsJSON serializes the given map as a JSON string
func MapAsJSON(m map[string]string) []byte {
bytes, err := json.Marshal(m)
if err != nil {
panic(err)
}
return bytes
}
// JoinNonEmpty takes a vararg of strings and return the join of all the non-empty strings with a delimiter between them
func JoinNonEmpty(delim string, strings ...string) string {
var buf bytes.Buffer
for _, s := range strings {
if s != "" {
if buf.Len() > 0 {
buf.WriteString(delim)
}
buf.WriteString(s)
}
}
return buf.String()
}
// DecodeUTF8 is equivalent to .decode('utf-8', 'ignore') in Python
func DecodeUTF8(bytes []byte) string {
s := string(bytes)
if !utf8.ValidString(s) {
v := make([]rune, 0, len(s))
for i, r := range s {
if r == utf8.RuneError {
_, size := utf8.DecodeRuneInString(s[i:])
if size == 1 {
continue
}
}
v = append(v, r)
}
s = string(v)
}
return s
}
// StringArrayContains returns whether a given string array contains the given element
func StringArrayContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
var invalidChars = regexp.MustCompile("([\u0000-\u0008]|[\u000B-\u000C]|[\u000E-\u001F])")
// CleanString removes any control characters from the passed in string
func CleanString(s string) string {
cleaned := invalidChars.ReplaceAllString(s, "")
// check whether this is valid UTF8
if !utf8.ValidString(cleaned) {
v := make([]rune, 0, len(cleaned))
for i, r := range s {
if r == utf8.RuneError {
_, size := utf8.DecodeRuneInString(s[i:])
if size == 1 {
continue
}
}
v = append(v, r)
}
cleaned = string(v)
}
return cleaned
}