-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
75 lines (68 loc) · 1.54 KB
/
regex.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
package wizardry
import (
"bytes"
"encoding/binary"
"github.com/yizhibenpao/go-filetype/wizardry/wizutil"
"regexp"
)
type RegexTestFlags int64
const (
Case_insensitive = 1 << iota
Match_To_Start
Limit_Lines
Trim
)
func IntToBytes(n int) []byte {
x := int32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, x)
return bytesBuffer.Bytes()
}
// StringTest looks for a string pattern in target, at given index
func RegexTest(sr *wizutil.SliceReader, targetIndex int64, maxLen int64, regexString string, flags RegexTestFlags) int64 {
bv := &wizutil.ByteView{
Input: sr,
LookBack: 0,
}
if flags&Case_insensitive > 0 {
regexString = "(?i)" + regexString
}
testp := regexp.MustCompile(regexString)
if flags&Match_To_Start > 0 {
targetIndex = 0
}
testbyte := []byte("")
if flags&Limit_Lines > 0 {
maxLen = maxLen * 80
for i := int64(0); i < maxLen; i++ {
if bv.Get(targetIndex+i) != 10 {
testbyte = append(testbyte, IntToBytes(bv.Get(targetIndex+i))...)
} else {
if flags&Trim > 0 {
testbyte = bytes.Trim(testbyte, " ")
}
loc := testp.FindIndex(testbyte)
if loc == nil {
testbyte = []byte("")
} else {
return int64(loc[1])
}
}
}
return -1
} else {
for i := int64(0); i < maxLen; i++ {
testbyte = append(testbyte, IntToBytes(bv.Get(targetIndex+i))...)
}
if flags&Trim > 0 {
testbyte = bytes.Trim(testbyte, " ")
}
loc := testp.FindIndex(testbyte)
if loc == nil {
return -1
} else {
return int64(loc[1])
}
}
return -1
}