forked from marcomaggi/cre2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre2.go
826 lines (688 loc) · 20.5 KB
/
re2.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
package re2
// #cgo LDFLAGS: ${SRCDIR}/libcre2.a ${SRCDIR}/libre2.a ${SRCDIR}/libstdc++.a -lm
// #cgo CFLAGS: -I${SRCDIR}/src
// #include <stdlib.h>
// #include <cre2.h>
// int length_ptr(char *begin, char *end) {
// return (int)(end - begin);
// }
// char *next_ptr(char *pos) {
// return ++pos;
// }
//cre2_decl int cre2_partial_match_re_helper(cre2_regexp_t* rex, const char* textp, int textlen,
// cre2_string_t * match, int nmatch) {
// const cre2_string_t text = {textp, textlen};
// return cre2_partial_match_re(rex, &text, match, nmatch);
//}
import "C"
import (
"errors"
"fmt"
"math"
"unsafe"
// "runtime"
)
// XXX : Reader系は未実装
// (ファイルハンドラをStringPieceを引数で受け取るre2ラッパーに渡す上手い方法が思いつかない)
// XXX : String系は、内部で[]byteへの変換をしているため、文字列の値コピー分のオーバーヘッドが余計にかかっている
// XXX : (Must)?Compile(POSIX)?や、Longestのパフォーマンスが、regexpパッケージに比べて著しく遅い
// NOTE : regexpパッケージのRegexpと同じ動作になるようにしているが、
// 動作が違う一部のメソッドはRE2というprefixを付けて差別化を図っている(ex:ReplaceAll)
// TODO : リファクタリング(重複コード、パターン化された箇所をメソッドにまとめる)
// XXX : FindAllやFindAllSubmatch等の引数bがlen(b) == 0時のコーナーケース対応がとりあえずになっている
// TODO : リファクタリング(コーナーケースの綺麗な対応)
// regexpインスタンスを解放する処理を担う
// NOTE : メソッドはregexpと統一しているが、このライブラリだと解放処理が必要なので、
// 返り値に差異を持たせ、単純置き換え時のClose呼び出し忘れを防ぐ
type Closer struct{}
func (c *Closer) Close(re *Regexp) {
if re != nil {
C.cre2_delete(unsafe.Pointer(re.cre2_re))
C.cre2_delete(unsafe.Pointer(re.cre2_re_with_bracket))
C.cre2_opt_delete(unsafe.Pointer(re.cre2_opt))
C.free(unsafe.Pointer(re.c_expr))
C.free(unsafe.Pointer(re.c_expr_with_bracket))
// GCが追い付かずにメモリアロケートが失敗する時に有効化
// 普段使いだとまず気にする必要はないはず
// runtime.GC()
}
}
// re2では、MatchやFindAndConsume時、ヒット箇所の抜出には外枠の()が必要で、Replace時は必要ない
// その為、その差異を吸収する為に()の有り・無しの2種類を持つ
type Regexp struct {
cre2_re *C.cre2_regexp_t
cre2_re_with_bracket *C.cre2_regexp_t
cre2_opt *C.cre2_options_t
c_expr *C.char
c_expr_length C.int
c_expr_with_bracket *C.char
c_expr_with_bracket_length C.int
}
func MatchString(expr string, s string) (matched bool, err error) {
re, closer, err := Compile(expr)
defer closer.Close(re)
if err != nil {
return false, err
}
return re.MatchString(s), nil
}
func Match(expr string, b []byte) (matched bool, err error) {
re, closer, err := Compile(expr)
defer closer.Close(re)
if err != nil {
return false, err
}
return re.Match(b), nil
}
// [ ]
//func MatchReader(pattern string, r io.RuneReader) (matched bool, err error) {
// // not implemented
//}
func RE2QuoteMeta(s string) string {
b := []byte(s)
var c_input *C.char
if len(b) > 0 {
c_input = (*C.char)(unsafe.Pointer(&b[0]))
} else {
c_input = nil
}
cre2_input := C.cre2_string_t{
data: c_input,
length: C.int(len(b)),
}
var cre2_quoted C.cre2_string_t
result := C.cre2_quote_meta(&cre2_quoted, &cre2_input)
if !bool(result == 0) {
panic("bad_alloc: malloc failed.")
}
ret := C.GoBytes(unsafe.Pointer(cre2_quoted.data), cre2_quoted.length)
C.free(unsafe.Pointer(cre2_quoted.data))
return string(ret)
}
func makeOptions(posix, longest, literal bool) *C.cre2_options_t {
// encodingはUTF-8のみ
opt := C.cre2_opt_new()
// C.cre2_opt_set_encoding(opt, C.CRE2_UTF8) // デフォルトでutf
if posix {
C.cre2_opt_set_posix_syntax(opt, 1)
}
if longest {
C.cre2_opt_set_longest_match(opt, 1)
}
if literal {
C.cre2_opt_set_literal(opt, 1)
}
C.cre2_opt_set_log_errors(opt, 0)
return (*C.cre2_options_t)(opt)
}
func compile(expr string, posix, longest bool) (*Regexp, *Closer, error) {
c_expr := C.CString(expr)
c_expr_length := C.int(len(expr))
c_expr_with_bracket := C.CString("(" + expr + ")") // RE2のFindAndConsume用に()を付与
c_expr_with_bracket_length := C.int(len(expr) + 2)
cre2_opt := makeOptions(posix, longest, false)
closer := &Closer{}
// XXX : cre2_newのコストが数倍になっている
// ()無し
cre2_re := C.cre2_new(c_expr, c_expr_length, unsafe.Pointer(cre2_opt))
errCode := C.cre2_error_code(cre2_re)
if errCode != C.CRE2_NO_ERROR {
C.cre2_delete(unsafe.Pointer(cre2_re))
C.cre2_opt_delete(unsafe.Pointer(cre2_opt))
C.free(unsafe.Pointer(c_expr))
C.free(unsafe.Pointer(c_expr_with_bracket))
return nil, closer, errors.New(fmt.Sprintf("Compile error code:%d", errCode))
}
// ()有り
cre2_re_with_bracket := C.cre2_new(c_expr_with_bracket, c_expr_with_bracket_length, unsafe.Pointer(cre2_opt))
errCode = C.cre2_error_code(cre2_re)
if errCode != C.CRE2_NO_ERROR {
C.cre2_delete(unsafe.Pointer(cre2_re))
C.cre2_delete(unsafe.Pointer(cre2_re_with_bracket))
C.cre2_opt_delete(unsafe.Pointer(cre2_opt))
C.free(unsafe.Pointer(c_expr))
C.free(unsafe.Pointer(c_expr_with_bracket))
return nil, closer, errors.New(fmt.Sprintf("Compile error code:%d", errCode))
}
re := &Regexp{
cre2_re: (*C.cre2_regexp_t)(cre2_re),
cre2_re_with_bracket: (*C.cre2_regexp_t)(cre2_re_with_bracket),
cre2_opt: cre2_opt,
c_expr: c_expr,
c_expr_length: c_expr_length,
c_expr_with_bracket: c_expr_with_bracket,
c_expr_with_bracket_length: c_expr_with_bracket_length,
}
return re, closer, nil // regexp.Compileと違い、Closerも返す
}
func Compile(expr string) (*Regexp, *Closer, error) {
return compile(expr, false, false)
}
func CompilePOSIX(expr string) (*Regexp, *Closer, error) {
return compile(expr, true, true)
}
func MustCompile(expr string) (*Regexp, *Closer) {
re, closer, err := Compile(expr)
if err != nil {
panic(err)
}
return re, closer // regexp.MustCompileと違い、Closerも返す
}
func MustCompilePOSIX(expr string) (*Regexp, *Closer) {
re, closer, err := CompilePOSIX(expr)
if err != nil {
panic(err)
}
return re, closer // regexp.MustCompilePOSIXと違い、Closerも返す
}
// RE2Expand ...
// NOTE : RE2Expandは、FindSubmatchIndexの戻り値を利用する方法を想定している
// その為、その想定内ならタグが使えない違いしかないが、
// RE2Expand単体で使う際は、matchに対する挙動が変わってくる
func (re *Regexp) RE2Expand(dst []byte, template []byte, src []byte, match []int) []byte {
text := make([]byte, match[1]-match[0])
copy(text, src[match[0]:match[1]])
var cre2_text C.cre2_string_t
if len(text) > 0 {
cre2_text = C.cre2_string_t{
data: (*C.char)(unsafe.Pointer(&text[0])),
length: C.int(len(text)),
}
} else {
cre2_text = C.cre2_string_t{
data: nil,
length: 0,
}
}
re2_rewrite := replaceRE2Sequences(template) // re2用のsequenceへ($n -> \\n)
c_re2_rewrite := (*C.char)(unsafe.Pointer(&re2_rewrite[0]))
cre2_re2_rewrite := C.cre2_string_t{
data: c_re2_rewrite,
length: C.int(len(re2_rewrite)),
}
cre2_out := C.cre2_string_t{}
C.cre2_extract_re(unsafe.Pointer(re.cre2_re), &cre2_text, &cre2_re2_rewrite, &cre2_out)
ret := C.GoBytes(unsafe.Pointer(cre2_out.data), cre2_out.length)
C.free(unsafe.Pointer(cre2_out.data)) // cre2_outはメモリが再確保されている
// 古いバッファ(text)は[]byteなので解放はGoのGCが担う
return append(dst, ret...)
}
func (re *Regexp) RE2ExpandString(dst []byte, template string, src string, match []int) []byte {
return re.RE2Expand(dst, []byte(template), []byte(src), match)
}
func (re *Regexp) Find(b []byte) []byte {
ret := re.FindAll(b, 1)
if ret == nil {
return nil
}
if len(ret) != 1 {
panic(fmt.Sprintf("len(ret):%d != 1", len(ret)))
}
return ret[0]
}
func (re *Regexp) FindAll(b []byte, n int) [][]byte {
var c_input *C.char
var cre2_input C.cre2_string_t
if len(b) > 0 {
c_input = (*C.char)(unsafe.Pointer(&b[0]))
cre2_input = C.cre2_string_t{
data: (*C.char)(unsafe.Pointer(&b[0])),
length: C.int(len(b)),
}
} else {
c_input = nil
cre2_input = C.cre2_string_t{
data: nil,
length: 0,
}
}
var _n int
if n >= 0 {
_n = n
} else {
// XXX : 無限ループにしなくて良いのか?
_n = math.MaxInt32
}
find := false
var ret [][]byte
for i := 0; i < _n; i++ {
var cre2_match [1]C.cre2_string_t
result := C.cre2_find_and_consume_re(unsafe.Pointer(re.cre2_re_with_bracket), &cre2_input, &cre2_match[0], 1)
if !bool(result != 0) {
break
}
var offset_begin int
if len(b) > 0 {
offset_begin = int(C.length_ptr(c_input, cre2_match[0].data))
} else {
offset_begin = 0
}
offset_end := offset_begin + int(cre2_match[0].length)
// 見つからなかった場合は一つずらす
if offset_end == offset_begin {
cre2_input.data = C.next_ptr(cre2_input.data)
cre2_input.length -= 1
if find {
i--
find = false
continue
}
} else {
find = true
}
bytes := C.GoBytes(unsafe.Pointer(cre2_match[0].data), cre2_match[0].length)
ret = append(ret, bytes)
}
if len(ret) == 0 {
return nil
}
return ret
}
func (re *Regexp) FindAllIndex(b []byte, n int) [][]int {
var c_input *C.char
if len(b) > 0 {
c_input = (*C.char)(unsafe.Pointer(&b[0]))
} else {
c_input = nil
}
cre2_input := C.cre2_string_t{
data: c_input,
length: C.int(len(b)),
}
var _n int
if n >= 0 {
_n = n
} else {
// XXX : 無限ループにしなくて良いのか?
_n = math.MaxInt32
}
find := false
var ret [][]int
for i := 0; i < _n; i++ {
var cre2_match [1]C.cre2_string_t
result := C.cre2_find_and_consume_re(unsafe.Pointer(re.cre2_re_with_bracket), &cre2_input, &cre2_match[0], 1)
if !bool(result != 0) {
break
}
var offset_begin int
if len(b) > 0 {
offset_begin = int(C.length_ptr(c_input, cre2_match[0].data))
} else {
offset_begin = 0
}
offset_end := offset_begin + int(cre2_match[0].length)
// 見つからなかった場合は一つずらす
if offset_end == offset_begin {
cre2_input.data = C.next_ptr(cre2_input.data)
cre2_input.length -= 1
if find {
i--
find = false
continue
}
} else {
find = true
}
indexes := []int{
offset_begin,
offset_end,
}
ret = append(ret, indexes)
}
if len(ret) == 0 {
return nil
}
return ret
}
func (re *Regexp) FindAllString(s string, n int) []string {
bytes := re.FindAll([]byte(s), n)
if bytes == nil {
return nil
}
var ret []string
for _, x := range bytes {
ret = append(ret, string(x))
}
return ret
}
func (re *Regexp) FindAllStringIndex(s string, n int) [][]int {
return re.FindAllIndex([]byte(s), n)
}
func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
bytes := re.FindAllSubmatch([]byte(s), n)
if bytes == nil {
return nil
}
var ret [][]string
for _, x := range bytes {
var a []string
for _, y := range x {
a = append(a, string(y))
}
ret = append(ret, a)
}
return ret
}
func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int {
return re.FindAllSubmatchIndex([]byte(s), n)
}
func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte {
var c_input *C.char
if len(b) > 0 {
c_input = (*C.char)(unsafe.Pointer(&b[0]))
} else {
c_input = nil
}
cre2_input := C.cre2_string_t{
data: c_input,
length: C.int(len(b)),
}
var _n int
if n >= 0 {
_n = n
} else {
// XXX : 無限ループにしなくて良いのか?
_n = math.MaxInt32
}
find := false
var ret [][][]byte
for i := 0; i < _n; i++ {
groups := C.cre2_num_capturing_groups(unsafe.Pointer(re.cre2_re_with_bracket))
cre2_match := make([]C.cre2_string_t, int(groups))
result := C.cre2_find_and_consume_re(unsafe.Pointer(re.cre2_re_with_bracket), &cre2_input, &cre2_match[0], groups)
if !bool(result != 0) {
break
}
var offset_begin int
if len(b) > 0 {
offset_begin = int(C.length_ptr(c_input, cre2_match[0].data))
} else {
offset_begin = 0
}
offset_end := offset_begin + int(cre2_match[0].length)
// 見つからなかった場合は一つずらす
if offset_end == offset_begin {
cre2_input.data = C.next_ptr(cre2_input.data)
cre2_input.length -= 1
if find {
i--
find = false
continue
}
} else {
find = true
}
var bytes [][]byte
for i := 0; i < int(groups); i++ {
b := C.GoBytes(unsafe.Pointer(cre2_match[i].data), cre2_match[i].length)
bytes = append(bytes, b)
}
ret = append(ret, bytes)
}
if len(ret) == 0 {
return nil
}
return ret
}
func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int {
var c_input *C.char
if len(b) > 0 {
c_input = (*C.char)(unsafe.Pointer(&b[0]))
} else {
c_input = nil
}
cre2_input := C.cre2_string_t{
data: c_input,
length: C.int(len(b)),
}
var _n int
if n >= 0 {
_n = n
} else {
// XXX : 無限ループにしなくて良いのか?
_n = math.MaxInt32
}
find := false
var ret [][]int
for i := 0; i < _n; i++ {
groups := C.cre2_num_capturing_groups(unsafe.Pointer(re.cre2_re_with_bracket))
cre2_match := make([]C.cre2_string_t, int(groups))
result := C.cre2_find_and_consume_re(unsafe.Pointer(re.cre2_re_with_bracket), &cre2_input, &cre2_match[0], groups)
if !bool(result != 0) {
break
}
var offset_begin int
if len(b) > 0 {
offset_begin = int(C.length_ptr(c_input, cre2_match[0].data))
} else {
offset_begin = 0
}
offset_end := offset_begin + int(cre2_match[0].length)
// 見つからなかった場合は一つずらす
if offset_end == offset_begin {
cre2_input.data = C.next_ptr(cre2_input.data)
cre2_input.length -= 1
if find {
i--
find = false
continue
}
} else {
find = true
}
var indexes []int
for j := 0; j < int(groups); j++ {
offset_begin := int(C.length_ptr(c_input, cre2_match[j].data))
offset_end := offset_begin + int(cre2_match[j].length)
indexes = append(indexes, offset_begin)
indexes = append(indexes, offset_end)
}
ret = append(ret, indexes)
}
if len(ret) == 0 {
return nil
}
return ret
}
func (re *Regexp) FindIndex(b []byte) (loc []int) {
ret := re.FindAllIndex(b, 1)
if ret == nil {
return nil
}
if len(ret) != 1 {
panic(fmt.Sprintf("len(ret):%d != 1", len(ret)))
}
return ret[0]
}
// [ ]
//func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int) {
// // not implemented
//}
// [ ]
//func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int {
// // not implemented
//}
func (re *Regexp) FindString(s string) string {
return string(re.Find([]byte(s)))
}
func (re *Regexp) FindStringIndex(s string) (loc []int) {
return re.FindIndex([]byte(s))
}
func (re *Regexp) FindStringSubmatch(s string) []string {
bytes := re.FindSubmatch([]byte(s))
var ret []string
for _, x := range bytes {
ret = append(ret, string(x))
}
return ret
}
func (re *Regexp) FindStringSubmatchIndex(s string) []int {
return re.FindSubmatchIndex([]byte(s))
}
func (re *Regexp) FindSubmatch(b []byte) [][]byte {
ret := re.FindAllSubmatch(b, 1)
if ret == nil {
return nil
}
if len(ret) != 1 {
panic(fmt.Sprintf("len(ret):%d != 1", len(ret)))
}
return ret[0]
}
func (re *Regexp) FindSubmatchIndex(b []byte) []int {
ret := re.FindAllSubmatchIndex(b, 1)
if ret == nil {
return nil
}
if len(ret) != 1 {
panic(fmt.Sprintf("len(ret):%d !- 1", len(ret)))
}
return ret[0]
}
// [ ]
//func (re *Regexp) LiteralPrefix() (prefix string, complete bool) {
// // not implemented
//}
// NOTE : re2のoptionsは変更出来ないので、インスタンスを作り変える
func (re *Regexp) Longest() {
longest := C.cre2_opt_longest_match(unsafe.Pointer(re.cre2_opt))
if bool(longest != 0) {
return // do not need
}
posix := C.cre2_opt_posix_syntax(unsafe.Pointer(re.cre2_opt))
// インスタンスの作り変え
newOpt := makeOptions(bool(posix != 0), true, false)
newOrigRe := C.cre2_new(re.c_expr, re.c_expr_length, unsafe.Pointer(newOpt))
newOrigRe_with_bracket := C.cre2_new(re.c_expr_with_bracket, re.c_expr_with_bracket_length, unsafe.Pointer(newOpt))
C.cre2_delete(unsafe.Pointer(re.cre2_re))
C.cre2_delete(unsafe.Pointer(re.cre2_re_with_bracket))
C.cre2_opt_delete(unsafe.Pointer(re.cre2_opt))
re.cre2_re = (*C.cre2_regexp_t)(newOrigRe)
re.cre2_re_with_bracket = (*C.cre2_regexp_t)(newOrigRe_with_bracket)
re.cre2_opt = newOpt
}
func (re *Regexp) Match(b []byte) bool {
var (
data *C.char
length C.int
)
if len(b) > 0 {
data = (*C.char)(unsafe.Pointer(&b[0]))
length = C.int(len(b))
} else {
data = nil
length = 0
}
result := C.cre2_partial_match_re_helper(unsafe.Pointer(re.cre2_re_with_bracket), data, length, nil, 0)
return bool(result != 0)
}
func (re *Regexp) MatchString(s string) bool {
return re.Match([]byte(s))
}
func (re *Regexp) NumSubexp() int {
groups := C.cre2_num_capturing_groups(unsafe.Pointer(re.cre2_re))
return int(groups)
}
func (re *Regexp) RE2ReplaceAll(src, repl []byte) []byte {
text_and_target := make([]byte, len(src))
copy(text_and_target, src)
c_text_and_target := (*C.char)(unsafe.Pointer(&text_and_target[0]))
cre2_text_and_target := C.cre2_string_t{
data: c_text_and_target,
length: C.int(len(text_and_target)),
}
re2_repl := replaceRE2Sequences(repl) // re2用のsequenceへ($n -> \\n)
var cre2_re2_repl C.cre2_string_t
if len(re2_repl) > 0 {
cre2_re2_repl = C.cre2_string_t{
data: (*C.char)(unsafe.Pointer(&re2_repl[0])),
length: C.int(len(re2_repl)),
}
} else {
cre2_re2_repl = C.cre2_string_t{
data: nil,
length: 0,
}
}
C.cre2_global_replace_re(unsafe.Pointer(re.cre2_re), &cre2_text_and_target, &cre2_re2_repl)
ret := C.GoBytes(unsafe.Pointer(cre2_text_and_target.data), cre2_text_and_target.length)
C.free(unsafe.Pointer(cre2_text_and_target.data)) // cre2_text_and_targetはメモリが再確保されている
// 古いバッファ(text_and_target)は[]byteなので解放はGoのGCが担う
return ret
}
// [ ]
//func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte {
// // not implemented
//}
func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte {
text_and_target := make([]byte, len(src))
copy(text_and_target, src)
c_text_and_target := (*C.char)(unsafe.Pointer(&text_and_target[0]))
cre2_text_and_target := C.cre2_string_t{
data: c_text_and_target,
length: C.int(len(text_and_target)),
}
re2_repl := replaceRE2InvalidSequences(repl) // sequencesの解析をしないように、\\を非sequencesへ置き換える
var cre2_re2_repl C.cre2_string_t
if len(re2_repl) > 0 {
cre2_re2_repl = C.cre2_string_t{
data: (*C.char)(unsafe.Pointer(&re2_repl[0])),
length: C.int(len(re2_repl)),
}
} else {
cre2_re2_repl = C.cre2_string_t{
data: nil,
length: 0,
}
}
C.cre2_global_replace_re(unsafe.Pointer(re.cre2_re), &cre2_text_and_target, &cre2_re2_repl)
ret := C.GoBytes(unsafe.Pointer(cre2_text_and_target.data), cre2_text_and_target.length)
C.free(unsafe.Pointer(cre2_text_and_target.data)) // cre2_text_and_targetはメモリが再確保されている
// 古いバッファ(text_and_target)は[]byteなので解放はGoのGCが担う
return ret
}
func (re *Regexp) ReplaceAllLiteralString(src, repl string) string {
return string(re.ReplaceAllLiteral([]byte(src), []byte(repl)))
}
// re2の置換方法に従う
// ({}で囲った記法が使えない)
func (re *Regexp) RE2ReplaceAllString(src, repl string) string {
return string(re.RE2ReplaceAll([]byte(src), []byte(repl)))
}
// [ ]
//func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string {
// // not implemented
//}
func (re *Regexp) Split(s string, n int) []string {
if n == 0 {
return nil
}
if re.c_expr_length > 0 && len(s) == 0 {
return []string{""}
}
matches := re.FindAllStringIndex(s, n)
strings := make([]string, 0, len(matches))
beg := 0
end := 0
for _, match := range matches {
if n > 0 && len(strings) >= n-1 {
break
}
end = match[0]
if match[1] != 0 {
strings = append(strings, s[beg:end])
}
beg = match[1]
}
if end != len(s) {
strings = append(strings, s[beg:])
}
return strings
}
func (re *Regexp) String() string {
return C.GoString(re.c_expr)
}
// [ ]
//func (re *Regexp) SubexpNames() []string {
// // not implemented
//}