-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrmask.go
More file actions
215 lines (200 loc) · 6.39 KB
/
strmask.go
File metadata and controls
215 lines (200 loc) · 6.39 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Package strmask provides two simple functions to validate and format a string based on a mask.
package strmask
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
var asciiLetters = &unicode.RangeTable{
R16: []unicode.Range16{
{'A', 'Z', 1},
{'a', 'z', 1},
},
}
// Reverses the provided string. Since this functions reverses the string rune by rune, it has problems with unicode combining characters.
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
// This functions just reverses any backslashes in the string with the rune preceding it. It's used to preserve escaped characters in reversed strings.
func reverseBackslashs(s string) string {
runes := []rune(s)
for i := 0; i < len(runes); i++ {
if runes[i] == '\\' && i > 0 {
runes[i-1], runes[i] = runes[i], runes[i-1]
}
}
return string(runes)
}
// ValidateAndFormatMask formats the string s based on the mask, reporting any invalid runes that doesn't fit the provided mask.
//
// The mask argument consists of three fields separated by semicolons: the mask, a pad character and a Right-to-Left specifier. Only the first field is required.
//
// The pad character is the character added to the result string when a required character class cannot be matched in the source string (note that the unmatched character in the source string is not skipped). If no pad character is specified, a space is used.
//
// If RTL processing is requested ('1'), the mask and the source string are reversed before processing, and the output is reversed afterwards. This causes extra characters in the source string to be at the left of the masked text and if the mask is bigger than the source string, the extra required characters in the mask are also padded to left.
//
// An information table about the mask symbols can be found at: https://github.com/frones/strmask
func ValidateAndFormatMask(mask string, str string) (string, error) {
padChar := ' '
rtl := false
args := strings.Split(mask, ";")
for _, s := range strings.Split(mask, ";")[1:] {
if r, _ := utf8.DecodeLastRuneInString(args[0]); r == '\\' {
args[0] += ";" + s
args = append(args[:1], args[2:]...)
}
}
mask = args[0]
if len(args) >= 2 {
padChar, _ = utf8.DecodeRuneInString(args[1])
}
if len(args) >= 3 {
rtl = args[2] == "1"
}
if rtl {
mask = reverseBackslashs(reverse(mask))
str = reverse(str)
}
printNext := false
output := ""
strOffset := 0
lastErrOffset := -1
err := ""
charCase := 'N'
for _, r := range mask {
if printNext {
output += string(r)
printNext = false
if r2, w := utf8.DecodeRuneInString(str[strOffset:]); w > 0 && r2 == r {
strOffset += w
}
continue
}
switch r {
case '\\':
printNext = true
case '>':
charCase = 'U'
case '<':
charCase = 'L'
case '=':
charCase = 'N'
case '9', '0':
if r2, w := utf8.DecodeRuneInString(str[strOffset:]); w > 0 && unicode.IsDigit(r2) {
output += string(r2)
strOffset += w
} else if r == '0' {
if lastErrOffset < strOffset {
if w > 0 {
err += fmt.Sprintf("invalid character \"%s\" (expected a digit) at position %d\n", string(r2), strOffset)
} else {
err += fmt.Sprintf("expected a digit at position %d, but end of string found\n", strOffset)
}
lastErrOffset = strOffset
}
output += string(padChar)
}
case 'L', 'l':
if r2, w := utf8.DecodeRuneInString(str[strOffset:]); w > 0 && unicode.In(r2, asciiLetters) {
if charCase == 'U' {
output += string(unicode.ToUpper(r2))
} else if charCase == 'L' {
output += string(unicode.ToLower(r2))
} else {
output += string(r2)
}
strOffset += w
} else if r == 'L' {
if lastErrOffset < strOffset {
if w > 0 {
err += fmt.Sprintf("invalid character \"%s\" (expected an ascii letter) at position %d\n", string(r2), strOffset)
} else {
err += fmt.Sprintf("expected an ascii letter at position %d, but end of string found\n", strOffset)
}
lastErrOffset = strOffset
}
output += string(padChar)
}
case 'A', 'a':
if r2, w := utf8.DecodeRuneInString(str[strOffset:]); w > 0 && (unicode.In(r2, asciiLetters) || unicode.IsDigit(r2)) {
if charCase == 'U' {
output += string(unicode.ToUpper(r2))
} else if charCase == 'L' {
output += string(unicode.ToLower(r2))
} else {
output += string(r2)
}
strOffset += w
} else if r == 'A' {
if lastErrOffset < strOffset {
if w > 0 {
err += fmt.Sprintf("invalid character \"%s\" (expected an ascii letter or a digit) at position %d\n", string(r2), strOffset)
} else {
err += fmt.Sprintf("expected an ascii letter or a digit at position %d, but end of string found\n", strOffset)
}
lastErrOffset = strOffset
}
output += string(padChar)
}
case 'W', 'w':
if r2, w := utf8.DecodeRuneInString(str[strOffset:]); w > 0 && unicode.IsLetter(r2) {
if charCase == 'U' {
output += string(unicode.ToUpper(r2))
} else if charCase == 'L' {
output += string(unicode.ToLower(r2))
} else {
output += string(r2)
}
strOffset += w
} else if r == 'W' {
if lastErrOffset < strOffset {
if w > 0 {
err += fmt.Sprintf("invalid character \"%s\" (expected an unicode letter) at position %d\n", string(r2), strOffset)
} else {
err += fmt.Sprintf("expected an unicode letter at position %d, but end of string found\n", strOffset)
}
lastErrOffset = strOffset
}
output += string(padChar)
}
case 'C', 'c':
if r2, w := utf8.DecodeRuneInString(str[strOffset:]); w > 0 {
if charCase == 'U' {
output += string(unicode.ToUpper(r2))
} else if charCase == 'L' {
output += string(unicode.ToLower(r2))
} else {
output += string(r2)
}
strOffset += w
}
default:
output += string(r)
r2, w := utf8.DecodeRuneInString(str[strOffset:])
if w > 0 && r2 == r {
strOffset += w
}
}
}
if len(str) > strOffset {
output += str[strOffset:]
}
if rtl {
output = reverse(output)
}
if err != "" {
return output, fmt.Errorf(err)
} else {
return output, nil
}
}
// FormatMask is a single-return value helper function to the ValidateAndFormatMask function. It just ignores any errors and outputs the formatted string returned.
func FormatMask(mask string, s string) string {
output, _ := ValidateAndFormatMask(mask, s)
return output
}