-
Notifications
You must be signed in to change notification settings - Fork 5
/
string.go
155 lines (137 loc) · 3.41 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package sanitize
import (
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
// sanitizeStrField sanitizes a string field. Requires the whole
// reflect.Value for the struct because it needs access to both the Value and
// Type of the struct.
func sanitizeStrField(s Sanitizer, structValue reflect.Value, idx int) error {
fieldValue := structValue.Field(idx)
tags := s.fieldTags(structValue.Type().Field(idx).Tag)
if fieldValue.Kind() == reflect.Ptr && !fieldValue.IsNil() {
fieldValue = fieldValue.Elem()
}
isSlice := fieldValue.Kind() == reflect.Slice
var fields []reflect.Value
if !isSlice {
fields = []reflect.Value{fieldValue}
} else {
for i := 0; i < fieldValue.Len(); i++ {
fields = append(fields, fieldValue.Index(i))
}
}
for _, field := range fields {
isPtr := field.Kind() == reflect.Ptr
if isPtr && field.IsNil() {
// Only handle "def" if it is present, then finish san.
if _, ok := tags["def"]; ok {
defStr := tags["def"]
field.Set(reflect.ValueOf(&defStr))
}
return nil
}
if isPtr && !field.IsNil() {
// Dereference then continue as normal.
field = field.Elem()
}
// Let's strip out invalid characters before anything else
if _, ok := tags["xss"]; ok {
oldStr := field.String()
field.SetString(xss(oldStr))
}
// Trim must happen before the other tags, no matter what other
// components there are.
if trimset, ok := tags["trim"]; ok {
if len(trimset) == 0 {
trimset = " "
}
oldStr := field.String()
field.SetString(strings.Trim(oldStr, trimset))
}
// Apply rest of transforms
if _, ok := tags["date"]; ok {
oldStr := field.String()
field.SetString(date(s.dateInput, s.dateKeepFormat, s.dateOutput, oldStr))
}
if _, ok := tags["max"]; ok {
max, err := strconv.ParseInt(tags["max"], 10, 32)
if err != nil {
return err
}
oldStr := field.String()
if max < int64(len(oldStr)) {
field.SetString(oldStr[0:max])
}
}
if _, ok := tags["lower"]; ok {
oldStr := field.String()
field.SetString(strings.ToLower(oldStr))
}
if _, ok := tags["upper"]; ok {
oldStr := field.String()
field.SetString(strings.ToUpper(oldStr))
}
if _, ok := tags["title"]; ok {
oldStr := field.String()
field.SetString(toTitle(oldStr))
}
if _, ok := tags["cap"]; ok {
oldStr := field.String()
field.SetString(toCap(oldStr))
}
}
return nil
}
func toTitle(s string) string {
return strings.Title(strings.ToLower((s)))
}
func toCap(s string) string {
b := make([]byte, len(s))
casediff := byte('a' - 'A')
i := 0
for ; i < len(s); i++ { // Looking for first character
b[i] = s[i]
c := b[i]
if c >= 'A' && c <= 'Z' { // Already capitalized
break
}
if c >= 'a' && c <= 'z' { // Must be capitalized
b[i] -= casediff
break
}
}
i++
for ; i < len(s); i++ { // Lowering all other characters
b[i] = s[i]
c := b[i]
if c >= 'A' && c <= 'Z' {
b[i] += casediff
}
}
return string(b)
}
var replaceWhitespaces = regexp.MustCompile(`\s\s+`)
var blacklistStripping = regexp.MustCompile(`[\p{Me}\p{C}<>=;(){}\[\]?]`)
func xss(s string) string {
s = blacklistStripping.ReplaceAllString(s, " ")
s = replaceWhitespaces.ReplaceAllString(s, " ")
return s
}
func date(in []string, keepFormat bool, out, v string) string {
for _, f := range in {
t, err := time.Parse(f, v)
if err != nil {
continue
}
outf := f
if !keepFormat {
outf = out
}
return t.Format(outf)
}
return ""
}