forked from xtrafrancyz/vk-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
replace.go
143 lines (122 loc) · 3.76 KB
/
replace.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
package replacer
import (
"bytes"
"regexp"
"sync"
"github.com/valyala/bytebufferpool"
)
var (
replaceBufferPool bytebufferpool.Pool
// В sync.Pool необходимо хранить только указатели, иначе при каждом Put будет аллокация для interface{}
// В итоге тут хранится *[]int, в добавок этот указатель нужно будет переиспользовать для записи
matchesPool = sync.Pool{New: func() interface{} {
m := make([]int, 0, 16)
return &m
}}
)
type regexReplace struct {
regex *regexp.Regexp
replacement []byte
}
func newRegexReplace(regex, replace string) *regexReplace {
return ®exReplace{
regex: regexp.MustCompile(regex),
replacement: []byte(replace),
}
}
func (v *regexReplace) Apply(input *bytebufferpool.ByteBuffer) *bytebufferpool.ByteBuffer {
return applyRegexpBytes(v.regex.FindAllSubmatchIndex(input.B, -1), input, func(src, dst []byte, match []int) []byte {
return v.regex.Expand(dst, v.replacement, src, match)
})
}
type regexFuncReplace struct {
regex *regexp.Regexp
replacer func(src, dst []byte, start, end int) []byte
}
func newRegexFuncReplace(regex string, replacer func(src, dst []byte, start, end int) []byte) *regexFuncReplace {
return ®exFuncReplace{
regex: regexp.MustCompile(regex),
replacer: replacer,
}
}
func (v *regexFuncReplace) Apply(input *bytebufferpool.ByteBuffer) *bytebufferpool.ByteBuffer {
return v.ApplyFunc(input, v.replacer)
}
func (v *regexFuncReplace) ApplyFunc(input *bytebufferpool.ByteBuffer,
f func(src, dst []byte, start, end int) []byte) *bytebufferpool.ByteBuffer {
return applyRegexpBytes(v.regex.FindAllIndex(input.B, -1), input, func(src, dst []byte, match []int) []byte {
return f(src, dst, match[0], match[1])
})
}
func applyRegexpBytes(matches [][]int, input *bytebufferpool.ByteBuffer,
expand func(src, dst []byte, match []int) []byte) *bytebufferpool.ByteBuffer {
l := len(matches)
if l == 0 {
return input
}
output := AcquireBuffer()
output.B = append(output.B, input.B[:matches[0][0]]...)
for i, match := range matches {
output.B = expand(input.B, output.B, match)
if i+1 < l {
output.B = append(output.B, input.B[match[1]:matches[i+1][0]]...)
}
}
output.B = append(output.B, input.B[matches[l-1][1]:]...)
ReleaseBuffer(input)
return output
}
type stringReplace struct {
needle []byte
needleLen int
replacement []byte
replLen int
}
func newStringReplace(needle, replace string) *stringReplace {
r := &stringReplace{
needle: []byte(needle),
replacement: []byte(replace),
}
r.replLen = len(r.replacement)
r.needleLen = len(r.needle)
return r
}
func (v *stringReplace) Apply(input *bytebufferpool.ByteBuffer) *bytebufferpool.ByteBuffer {
index := bytes.Index(input.B, v.needle)
if index == -1 {
return input
}
offset := index
_matches := matchesPool.Get().(*[]int)
matches := *_matches
for {
index = bytes.Index(input.B[offset:], v.needle)
if index == -1 {
break
}
matches = append(matches, offset+index)
offset += index + v.needleLen
}
output := AcquireBuffer()
neededLength := input.Len() + len(matches)*(v.replLen-v.needleLen)
if cap(output.B) < neededLength {
output.B = make([]byte, neededLength)
} else {
output.B = output.B[0:neededLength]
}
offset = 0
for i, idx := range matches {
if i == 0 {
offset += copy(output.B[offset:], input.B[0:idx])
} else {
offset += copy(output.B[offset:], input.B[matches[i-1]+v.needleLen:idx])
}
offset += copy(output.B[offset:], v.replacement)
}
offset += copy(output.B[offset:], input.B[matches[len(matches)-1]+v.needleLen:])
output.B = output.B[0:offset]
*_matches = matches[:0]
matchesPool.Put(_matches)
ReleaseBuffer(input)
return output
}