Skip to content

Commit 8602250

Browse files
committed
Updated
1 parent 010dcf7 commit 8602250

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

regex.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,69 @@ func RepFunc(str []byte, re string, rep func(func(int) []byte) []byte, blank ...
187187
return res
188188
}
189189

190+
func RepFuncFirst(str []byte, re string, rep func(func(int) []byte) []byte, blank ...bool) []byte {
191+
reg := Compile(re)
192+
193+
// ind := reg.FindAllIndex(str, pcre.UTF8)
194+
// ind := reg.FindAllIndex(str, 0)
195+
pos := reg.FindIndex(str, 0)
196+
197+
res := []byte{}
198+
trim := 0
199+
// for _, pos := range ind {
200+
v := str[pos[0]:pos[1]]
201+
m := reg.Matcher(v, 0)
202+
203+
if len(blank) != 0 {
204+
gCache := map[int][]byte{}
205+
r := rep(func(g int) []byte {
206+
if v, ok := gCache[g]; ok {
207+
return v
208+
}
209+
v := m.Group(g)
210+
gCache[g] = v
211+
return v
212+
})
213+
214+
if r == nil {
215+
return nil
216+
}
217+
} else {
218+
if trim == 0 {
219+
res = append(res, str[:pos[0]]...)
220+
} else {
221+
res = append(res, str[trim:pos[0]]...)
222+
}
223+
trim = pos[1]
224+
225+
gCache := map[int][]byte{}
226+
r := rep(func(g int) []byte {
227+
if v, ok := gCache[g]; ok {
228+
return v
229+
}
230+
v := m.Group(g)
231+
gCache[g] = v
232+
return v
233+
})
234+
235+
if r == nil {
236+
res = append(res, str[trim:]...)
237+
return res
238+
}
239+
240+
res = append(res, r...)
241+
}
242+
// }
243+
244+
if len(blank) != 0 {
245+
return nil
246+
}
247+
248+
res = append(res, str[trim:]...)
249+
250+
return res
251+
}
252+
190253
func RepStr(str []byte, re string, rep []byte) []byte {
191254
reg := Compile(re)
192255

regex_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,47 @@ import (
99
)
1010

1111
func TestCompile(t *testing.T) {
12-
var check = func(s string){
12+
var check = func(s string) {
1313
re1 := Compile(s)
1414
re2 := Compile(s)
1515
if re1.Groups() != re2.Groups() {
1616
t.Error(s, errors.New("first result does not match cache result"))
1717
}
1818
}
19+
1920
check("")
2021
check("a(b)")
2122
}
2223

2324
func TestReplace(t *testing.T) {
24-
var check = func(s string, re, r string, e string){
25+
var check = func(s string, re, r string, e string) {
2526
res := RepStr([]byte(s), re, []byte(r))
2627
if !bytes.Equal(res, []byte(e)) {
2728
t.Error(res, errors.New("result does not match expected result"))
2829
}
2930
}
31+
3032
check("this is a test", `(?#a\s+)test`, "", "this is a ")
3133
check("string with `block` quotes", `\'.*?\'`, "'single'", "string with 'single' quotes")
3234
}
3335

36+
func TestReplaceFirst(t *testing.T) {
37+
var check = func(s string, re string, r func(func(int) []byte) []byte, e string) {
38+
res := RepFuncFirst([]byte(s), re, r)
39+
if !bytes.Equal(res, []byte(e)) {
40+
t.Error(res, errors.New("result does not match expected result"))
41+
}
42+
}
43+
44+
check("test 1 and test 2", `test`, func(data func(int) []byte) []byte {
45+
return []byte{}
46+
}, " 1 and test 2")
47+
}
48+
3449
func TestConcurent(t *testing.T) {
3550
for i := 0; i < 10; i++ {
3651
for i := 0; i < 10; i++ {
37-
go (func(){
52+
go (func() {
3853
res := RepFunc([]byte("test"), `(t)`, func(data func(int) []byte) []byte {
3954
return data(1)
4055
})

0 commit comments

Comments
 (0)