-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatterns.go
77 lines (69 loc) · 1.54 KB
/
patterns.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
package rogue
import (
"bytes"
"encoding/hex"
"regexp"
)
var separatePattern = regexp.MustCompile("\\s+")
func stringToByteArray(pattern string) ([]byte, []bool, error) {
bytes := separatePattern.Split(pattern, -1)
result := make([]byte, len(bytes))
wildcards := make([]bool, len(bytes))
for i, b := range bytes {
if b == "?" || b == "??" {
result[i] = 0x0
wildcards[i] = true
} else {
parsed, err := hex.DecodeString(b)
if err != nil {
return nil, nil, err
}
result[i] = byte(parsed[0])
}
}
return result, wildcards, nil
}
// hexToUintptr converts b into a uintptr.
// It's optimized to assume the input will not be invalid.
// (I.e., that /proc/$$/maps won't produce a garbage value.)
func hexToUintptr(b []byte) (n uintptr) {
for _, d := range b {
n *= 16
switch {
case '0' <= d && d <= '9':
n += uintptr(d - '0')
case 'a' <= d && d <= 'z':
n += uintptr(d - 'a' + 10)
case 'A' <= d && d <= 'Z':
n += uintptr(d - 'A' + 10)
default:
return 0
}
}
return n
}
// parseUint parses b into a uint64. See hexToUintptr for more.
func parseUint(b []byte) (n uint64) {
for _, d := range b {
n *= 10
switch {
case '0' <= d && d <= '9':
n += uint64(d - '0')
case 'a' <= d && d <= 'z':
n += uint64(d - 'a' + 10)
case 'A' <= d && d <= 'Z':
n += uint64(d - 'A' + 10)
default:
return 0
}
}
return n
}
// splitOn splits b in half on the first occurance of c.
func splitOn(b []byte, c byte) (p1, p2 []byte) {
i := bytes.IndexByte(b, c)
if i < 0 {
return nil, nil
}
return b[:i], b[i+1:]
}