-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
88 lines (79 loc) · 2.51 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
package wizardry
import "github.com/yizhibenpao/go-filetype/wizardry/wizutil"
// StringTestFlags describes how to perform a string test
type StringTestFlags int64
const (
// CompactWhitespace ("W" flag) compacts whitespace in the target,
// which must contain at least one whitespace character
CompactWhitespace = 1 << iota
// OptionalBlanks ("w" flag) treats every blank in the magic as an optional blank
OptionalBlanks
// LowerMatchesBoth ("c" flag) specifies case-insensitive matching: lower case
// characters in the magic match both lower and upper case characters
// in the target
LowerMatchesBoth
// UpperMatchesBoth ("C" flag) specifies case-insensitive matching: upper case
// characters in the magic match both lower and upper case characters
// in the target
UpperMatchesBoth
// ForceText ("t" flag) forces the test to be done for text files
ForceText
// ForceBinary ("b" flag) forces the test to be done for binary files
ForceBinary
)
// StringTest looks for a string pattern in target, at given index
func StringTest(sr *wizutil.SliceReader, targetIndex int64, patternString string, flags StringTestFlags) int64 {
bv := &wizutil.ByteView{
Input: sr,
LookBack: 0,
}
pattern := []byte(patternString)
patternSize := len(pattern)
patternIndex := 0
for {
patternByte := pattern[patternIndex]
targetInt := bv.Get(targetIndex)
if targetInt == -1 {
return -1
}
targetByte := byte(targetInt)
matches := patternByte == targetByte
if matches {
// perfect match, advance both
targetIndex++
patternIndex++
} else if flags&OptionalBlanks > 0 && wizutil.IsWhitespace(patternByte) {
// cool, it's optional then
patternIndex++
} else if flags&LowerMatchesBoth > 0 && wizutil.IsLowerLetter(patternByte) && wizutil.ToLower(targetByte) == patternByte {
// case insensitive match
targetIndex++
patternIndex++
} else if flags&UpperMatchesBoth > 0 && wizutil.IsUpperLetter(patternByte) && wizutil.ToUpper(targetByte) == patternByte {
// case insensitive match
targetIndex++
patternIndex++
} else {
// not a match
return -1
}
if flags&CompactWhitespace > 0 && wizutil.IsWhitespace(targetByte) {
// if we had whitespace, skip any whitespace coming after it
for {
targetIndex++
targetInt = bv.Get(targetIndex)
if targetInt == -1 {
return -1
}
targetByte = byte(targetInt)
if !wizutil.IsWhitespace(targetByte) {
break
}
}
}
if patternIndex >= patternSize {
// hey it matched all the way!
return targetIndex
}
}
}