-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
46 lines (37 loc) · 1.05 KB
/
utils.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
package golex
import (
"fmt"
"strings"
)
func expandCharacterPattern(pattern string) (string, error) {
var validChars strings.Builder
length := len(pattern)
for i := 0; i < length; i++ {
if i+2 < length && pattern[i+1] == '-' {
start := pattern[i]
end := pattern[i+2]
// Validate that 'start' is less than 'end' and both are letters or digits
if start > end || !(isSameType(start, end)) {
return "", fmt.Errorf("invalid pattern range: %c-%c", start, end)
}
// Expand the range from start to end
for c := start; c <= end; c++ {
validChars.WriteRune(rune(c))
}
i += 2 // Skip over the next two characters as they are part of the range
} else {
// If it's not a range, just add the character
validChars.WriteByte(pattern[i])
}
}
return validChars.String(), nil
}
func isSameType(a, b byte) bool {
return (isLetter(a) && isLetter(b)) || (isDigit(a) && isDigit(b))
}
func isLetter(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func isDigit(c byte) bool {
return c >= '0' && c <= '9'
}