-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
63 lines (55 loc) · 1.32 KB
/
string.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
package asn1
import (
"fmt"
"unicode"
)
type stringEncoder string
func (e stringEncoder) encode() ([]byte, error) {
return []byte(e), nil
}
// https://en.wikipedia.org/wiki/PrintableString
func isPrintable(b byte) bool {
for _, c := range string(b) {
switch {
case c >= 'a' && c <= 'z':
case c >= 'A' && c <= 'Z':
case c >= '0' && c <= '9':
default:
switch c {
case ' ', '\'', '(', ')', '+', ',', '-', '.', '/', ':', '=', '?':
default:
return false
}
}
}
return true
}
func makePrintableString(s string) (string, error) {
stringBytes := []byte(s)
for i := 0; i < len(stringBytes); i++ {
if !isPrintable(stringBytes[i]) {
return "", fmt.Errorf("PrintableString contains invalid character: '%s'", string(stringBytes[i]))
}
}
return s, nil
}
func makeIA5String(s string) (string, error) {
stringBytes := []byte(s)
for i := 0; i < len(stringBytes); i++ {
if stringBytes[i] > 127 {
return "", fmt.Errorf("IA5String contains invalid character: '%s'", string(stringBytes[i]))
}
}
return s, nil
}
func isNumeric(b byte) bool {
return unicode.IsDigit(rune(b)) || b == ' '
}
func makeNumericString(s string) (string, error) {
for i := 0; i < len(s); i++ {
if !isNumeric(s[i]) {
return "", fmt.Errorf("NumericString contains invalid character: '%s'", string(s[i]))
}
}
return s, nil
}