-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfragment.go
162 lines (152 loc) · 3.81 KB
/
fragment.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
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
package matcher
import (
"bytes"
"github.com/viant/parsly"
"github.com/viant/parsly/matcher/option"
"unsafe"
)
type FragmentFold struct {
embed *bool //can be embedded within word
value []byte
size int
}
func (d *FragmentFold) isEmbed() bool {
if d.embed == nil {
return true
}
return *d.embed
}
func (d *FragmentFold) Match(cursor *parsly.Cursor) int {
matchEnd := cursor.Pos + d.size
if matchEnd <= cursor.InputSize {
if MatchFold(d.value, cursor.Input, 0, cursor.Pos) {
if d.isEmbed() {
return d.size
}
if matchEnd >= len(cursor.Input) {
return d.size
}
if IsLetter(cursor.Input[matchEnd]) || IsDigit(cursor.Input[matchEnd]) {
return 0
}
return d.size
}
}
return 0
}
type Fragment struct {
embed *bool //can be embedded within word
value []byte
size int
}
func (d *Fragment) isEmbed() bool {
if d.embed == nil {
return true
}
return *d.embed
}
func (d *Fragment) Match(cursor *parsly.Cursor) int {
matchEnd := cursor.Pos + d.size
if matchEnd <= cursor.InputSize {
if bytes.Equal(cursor.Input[cursor.Pos:matchEnd], d.value) {
if d.isEmbed() {
return d.size
}
if matchEnd >= len(cursor.Input) {
return d.size
}
if IsLetter(cursor.Input[matchEnd]) || IsDigit(cursor.Input[matchEnd]) {
return 0
}
return d.size
}
}
return 0
}
// NewFragments creates FragmentFold matcher
func NewFragment(value string, options ...Option) parsly.Matcher {
caseOpt := &option.Case{}
if AssignOption(options, &caseOpt) && !caseOpt.Sensitive {
return &FragmentFold{
value: []byte(value),
size: len(value),
embed: caseOpt.Embed,
}
}
return &Fragment{
value: []byte(value),
size: len(value),
embed: caseOpt.Embed,
}
}
const (
uint16LowerMask = uint16(0x2020)
uint32LowerMask = uint32(0x20202020)
uint64LowerMask = uint64(0x2020202020202020)
)
// MatchFold returns true if source is matched with target (case insensitive)
func MatchFold(target, source []byte, targetOffset, sourceOffset int) bool {
bytesToCheck := len(target) - targetOffset
if sourceOffset+bytesToCheck > len(source) {
return false
}
if (target[targetOffset] | 0x20) != (source[sourceOffset] | 0x20) {
return false
}
last := len(target) - 1
if (target[last] | 0x20) != (source[sourceOffset+last] | 0x20) {
return false
}
switch bytesToCheck {
case 1, 2:
return true
case 3:
x := *(*uint16)(unsafe.Pointer(&target[targetOffset+1])) | uint16LowerMask
y := *(*uint16)(unsafe.Pointer(&source[sourceOffset+1])) | uint16LowerMask
return x == y
case 4:
x := *(*uint32)(unsafe.Pointer(&target[targetOffset])) | uint32LowerMask
y := *(*uint32)(unsafe.Pointer(&source[sourceOffset])) | uint32LowerMask
return x == y
case 5, 6:
x := *(*uint32)(unsafe.Pointer(&target[targetOffset+1])) | uint32LowerMask
y := *(*uint32)(unsafe.Pointer(&source[sourceOffset+1])) | uint32LowerMask
return x == y
case 7:
if (target[targetOffset+1] | 0x20) != (source[sourceOffset+1] | 0x20) {
return false
}
x := *(*uint32)(unsafe.Pointer(&target[targetOffset+2])) | uint32LowerMask
y := *(*uint32)(unsafe.Pointer(&source[sourceOffset+2])) | uint32LowerMask
return x == y
case 8:
x := *(*uint64)(unsafe.Pointer(&target[targetOffset])) | uint64LowerMask
y := *(*uint64)(unsafe.Pointer(&source[sourceOffset])) | uint64LowerMask
if x != y {
return false
}
return true
}
repeat := bytesToCheck / 8
for i := 0; i < repeat; i++ {
x := *(*uint64)(unsafe.Pointer(&target[targetOffset])) | uint64LowerMask
y := *(*uint64)(unsafe.Pointer(&source[sourceOffset])) | uint64LowerMask
if x != y {
return false
}
targetOffset += 8
sourceOffset += 8
}
rem := bytesToCheck % 8
if rem == 0 {
return true
}
for i := 0; i < rem; i++ {
if (target[targetOffset] | 0x20) != (source[sourceOffset] | 0x20) {
return false
}
targetOffset++
sourceOffset++
}
return true
}