-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASCII.go
44 lines (39 loc) · 976 Bytes
/
ASCII.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
package wiz
import (
"regexp"
"unicode"
)
// Converts binary to ASCII - returns false if non-ASCII bytes found
func ASCII(b []byte) (string, bool) {
for _, c := range b {
if c > unicode.MaxASCII {
return "", false
}
}
return string(b), true
}
// Converts binary to printable string - returns false if non-printable characters found
func Printable(b []byte) (string, bool) {
for _, c := range b {
if c > unicode.MaxASCII || !unicode.IsGraphic(rune(c)) {
return "", false
}
}
return string(b), true
}
// Strips non-ascii characters from a string
func StipNonASCII(in string) string {
re := regexp.MustCompile("[[:^ascii:]]")
return re.ReplaceAllLiteralString(in, "")
}
// Strips non-printable and non-ascii characters from a string
func StripNonPrintableASCII(in string) string {
b := []byte(in)
out := []byte{}
for _, c := range b {
if c <= unicode.MaxASCII && unicode.IsGraphic(rune(c)) {
out = append(out, c)
}
}
return string(out)
}