-
Notifications
You must be signed in to change notification settings - Fork 0
/
re.c
3521 lines (3104 loc) · 89.3 KB
/
re.c
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
re.c -
$Author$
created at: Mon Aug 9 18:24:49 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#include "ruby/ruby.h"
#include "ruby/re.h"
#include "ruby/encoding.h"
#include "ruby/util.h"
#include "regint.h"
#include <ctype.h>
VALUE rb_eRegexpError;
typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
#define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN)
#define BEG(no) regs->beg[no]
#define END(no) regs->end[no]
#if 'a' == 97 /* it's ascii */
static const char casetable[] = {
'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
/* ' ' '!' '"' '#' '$' '%' '&' ''' */
'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
/* '(' ')' '*' '+' ',' '-' '.' '/' */
'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
/* '0' '1' '2' '3' '4' '5' '6' '7' */
'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
/* '8' '9' ':' ';' '<' '=' '>' '?' */
'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
/* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
/* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
/* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
/* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
/* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
/* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
/* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
/* 'x' 'y' 'z' '{' '|' '}' '~' */
'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
'\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
'\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
'\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
'\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
};
#else
# error >>> "You lose. You will need a translation table for your character set." <<<
#endif
int
rb_memcicmp(const void *x, const void *y, long len)
{
const unsigned char *p1 = x, *p2 = y;
int tmp;
while (len--) {
if ((tmp = casetable[(unsigned)*p1++] - casetable[(unsigned)*p2++]))
return tmp;
}
return 0;
}
#undef rb_memcmp
int
rb_memcmp(const void *p1, const void *p2, long len)
{
return memcmp(p1, p2, len);
}
static inline long
rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
{
const unsigned char *x = xs, *xe = xs + m;
const unsigned char *y = ys, *ye = ys + n;
#ifndef VALUE_MAX
# if SIZEOF_VALUE == 8
# define VALUE_MAX 0xFFFFFFFFFFFFFFFFULL
# elif SIZEOF_VALUE == 4
# define VALUE_MAX 0xFFFFFFFFUL
# endif
#endif
VALUE hx, hy, mask = VALUE_MAX >> ((SIZEOF_VALUE - m) * CHAR_BIT);
if (m > SIZEOF_VALUE)
rb_bug("!!too long pattern string!!");
/* Prepare hash value */
for (hx = *x++, hy = *y++; x < xe; ++x, ++y) {
hx <<= CHAR_BIT;
hy <<= CHAR_BIT;
hx |= *x;
hy |= *y;
}
/* Searching */
while (hx != hy) {
if (y == ye)
return -1;
hy <<= CHAR_BIT;
hy |= *y;
hy &= mask;
y++;
}
return y - ys - m;
}
static inline long
rb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long n)
{
const unsigned char *x = xs, *xe = xs + m;
const unsigned char *y = ys;
VALUE i, qstable[256];
/* Preprocessing */
for (i = 0; i < 256; ++i)
qstable[i] = m + 1;
for (; x < xe; ++x)
qstable[*x] = xe - x;
/* Searching */
for (; y + m <= ys + n; y += *(qstable + y[m])) {
if (*xs == *y && memcmp(xs, y, m) == 0)
return y - ys;
}
return -1;
}
static inline unsigned int
rb_memsearch_qs_utf8_hash(const unsigned char *x)
{
register const unsigned int mix = 8353;
register unsigned int h = *x;
if (h < 0xC0) {
return h + 256;
}
else if (h < 0xE0) {
h *= mix;
h += x[1];
}
else if (h < 0xF0) {
h *= mix;
h += x[1];
h *= mix;
h += x[2];
}
else if (h < 0xF5) {
h *= mix;
h += x[1];
h *= mix;
h += x[2];
h *= mix;
h += x[3];
}
else {
return h + 256;
}
return (unsigned char)h;
}
static inline long
rb_memsearch_qs_utf8(const unsigned char *xs, long m, const unsigned char *ys, long n)
{
const unsigned char *x = xs, *xe = xs + m;
const unsigned char *y = ys;
VALUE i, qstable[512];
/* Preprocessing */
for (i = 0; i < 512; ++i) {
qstable[i] = m + 1;
}
for (; x < xe; ++x) {
qstable[rb_memsearch_qs_utf8_hash(x)] = xe - x;
}
/* Searching */
for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(y+m)]) {
if (*xs == *y && memcmp(xs, y, m) == 0)
return y - ys;
}
return -1;
}
long
rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
{
const unsigned char *x = x0, *y = y0;
if (m > n) return -1;
else if (m == n) {
return memcmp(x0, y0, m) == 0 ? 0 : -1;
}
else if (m < 1) {
return 0;
}
else if (m == 1) {
const unsigned char *ys = y, *ye = ys + n;
for (; y < ye; ++y) {
if (*x == *y)
return y - ys;
}
return -1;
}
else if (m <= SIZEOF_VALUE) {
return rb_memsearch_ss(x0, m, y0, n);
}
else if (enc == rb_utf8_encoding()){
return rb_memsearch_qs_utf8(x0, m, y0, n);
}
else {
return rb_memsearch_qs(x0, m, y0, n);
}
}
#define REG_LITERAL FL_USER5
#define REG_ENCODING_NONE FL_USER6
#define KCODE_FIXED FL_USER4
#define ARG_REG_OPTION_MASK \
(ONIG_OPTION_IGNORECASE|ONIG_OPTION_MULTILINE|ONIG_OPTION_EXTEND)
#define ARG_ENCODING_FIXED 16
#define ARG_ENCODING_NONE 32
static int
char_to_option(int c)
{
int val;
switch (c) {
case 'i':
val = ONIG_OPTION_IGNORECASE;
break;
case 'x':
val = ONIG_OPTION_EXTEND;
break;
case 'm':
val = ONIG_OPTION_MULTILINE;
break;
default:
val = 0;
break;
}
return val;
}
static char *
option_to_str(char str[4], int options)
{
char *p = str;
if (options & ONIG_OPTION_MULTILINE) *p++ = 'm';
if (options & ONIG_OPTION_IGNORECASE) *p++ = 'i';
if (options & ONIG_OPTION_EXTEND) *p++ = 'x';
*p = 0;
return str;
}
extern int
rb_char_to_option_kcode(int c, int *option, int *kcode)
{
*option = 0;
switch (c) {
case 'n':
*kcode = rb_ascii8bit_encindex();
return (*option = ARG_ENCODING_NONE);
case 'e':
*kcode = rb_enc_find_index("EUC-JP");
break;
case 's':
*kcode = rb_enc_find_index("Windows-31J");
break;
case 'u':
*kcode = rb_utf8_encindex();
break;
default:
*kcode = -1;
return (*option = char_to_option(c));
}
*option = ARG_ENCODING_FIXED;
return 1;
}
static void
rb_reg_check(VALUE re)
{
if (!RREGEXP(re)->ptr || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
rb_raise(rb_eTypeError, "uninitialized Regexp");
}
}
static void
rb_reg_expr_str(VALUE str, const char *s, long len)
{
rb_encoding *enc = rb_enc_get(str);
const char *p, *pend;
int need_escape = 0;
int c, clen;
p = s; pend = p + len;
while (p<pend) {
c = rb_enc_ascget(p, pend, &clen, enc);
if (c == -1) {
p += mbclen(p, pend, enc);
}
else if (c != '/' && rb_enc_isprint(c, enc)) {
p += clen;
}
else {
need_escape = 1;
break;
}
}
if (!need_escape) {
rb_str_buf_cat(str, s, len);
}
else {
p = s;
while (p<pend) {
c = rb_enc_ascget(p, pend, &clen, enc);
if (c == '\\' && p+clen < pend) {
int n = clen + mbclen(p+clen, pend, enc);
rb_str_buf_cat(str, p, n);
p += n;
continue;
}
else if (c == '/') {
char c = '\\';
rb_str_buf_cat(str, &c, 1);
rb_str_buf_cat(str, p, clen);
}
else if (c == -1) {
int l = mbclen(p, pend, enc);
rb_str_buf_cat(str, p, l);
p += l;
continue;
}
else if (rb_enc_isprint(c, enc)) {
rb_str_buf_cat(str, p, clen);
}
else if (!rb_enc_isspace(c, enc)) {
char b[8];
snprintf(b, sizeof(b), "\\x%02X", c);
rb_str_buf_cat(str, b, 4);
}
else {
rb_str_buf_cat(str, p, clen);
}
p += clen;
}
}
}
static VALUE
rb_reg_desc(const char *s, long len, VALUE re)
{
VALUE str = rb_str_buf_new2("/");
if (re && rb_enc_asciicompat(rb_enc_get(re))) {
rb_enc_copy(str, re);
}
else {
rb_enc_associate(str, rb_usascii_encoding());
}
rb_reg_expr_str(str, s, len);
rb_str_buf_cat2(str, "/");
if (re) {
char opts[4];
rb_reg_check(re);
if (*option_to_str(opts, RREGEXP(re)->ptr->options))
rb_str_buf_cat2(str, opts);
}
OBJ_INFECT(str, re);
return str;
}
/*
* call-seq:
* rxp.source => str
*
* Returns the original string of the pattern.
*
* /ab+c/ix.source #=> "ab+c"
*
* Note that escape sequences are retained as is.
*
* /\x20\+/.source #=> "\\x20\\+"
*
*/
static VALUE
rb_reg_source(VALUE re)
{
VALUE str;
rb_reg_check(re);
str = rb_enc_str_new(RREGEXP_SRC_PTR(re),RREGEXP_SRC_LEN(re), rb_enc_get(re));
if (OBJ_TAINTED(re)) OBJ_TAINT(str);
return str;
}
/*
* call-seq:
* rxp.inspect => string
*
* Produce a nicely formatted string-version of _rxp_. Perhaps surprisingly,
* <code>#inspect</code> actually produces the more natural version of
* the string than <code>#to_s</code>.
*
* /ab+c/ix.inspect #=> "/ab+c/ix"
*
*/
static VALUE
rb_reg_inspect(VALUE re)
{
if (!RREGEXP(re)->ptr || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
return rb_any_to_s(re);
}
return rb_reg_desc(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), re);
}
/*
* call-seq:
* rxp.to_s => str
*
* Returns a string containing the regular expression and its options (using the
* <code>(?opts:source)</code> notation. This string can be fed back in to
* <code>Regexp::new</code> to a regular expression with the same semantics as
* the original. (However, <code>Regexp#==</code> may not return true when
* comparing the two, as the source of the regular expression itself may
* differ, as the example shows). <code>Regexp#inspect</code> produces a
* generally more readable version of <i>rxp</i>.
*
* r1 = /ab+c/ix #=> /ab+c/ix
* s1 = r1.to_s #=> "(?ix-m:ab+c)"
* r2 = Regexp.new(s1) #=> /(?ix-m:ab+c)/
* r1 == r2 #=> false
* r1.source #=> "ab+c"
* r2.source #=> "(?ix-m:ab+c)"
*/
static VALUE
rb_reg_to_s(VALUE re)
{
int options, opt;
const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
long len;
const UChar* ptr;
VALUE str = rb_str_buf_new2("(?");
char optbuf[5];
rb_reg_check(re);
rb_enc_copy(str, re);
options = RREGEXP(re)->ptr->options;
ptr = (UChar*)RREGEXP_SRC_PTR(re);
len = RREGEXP_SRC_LEN(re);
again:
if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
int err = 1;
ptr += 2;
if ((len -= 2) > 0) {
do {
opt = char_to_option((int )*ptr);
if (opt != 0) {
options |= opt;
}
else {
break;
}
++ptr;
} while (--len > 0);
}
if (len > 1 && *ptr == '-') {
++ptr;
--len;
do {
opt = char_to_option((int )*ptr);
if (opt != 0) {
options &= ~opt;
}
else {
break;
}
++ptr;
} while (--len > 0);
}
if (*ptr == ')') {
--len;
++ptr;
goto again;
}
if (*ptr == ':' && ptr[len-1] == ')') {
Regexp *rp;
++ptr;
len -= 2;
err = onig_new(&rp, ptr, ptr + len, ONIG_OPTION_DEFAULT,
rb_enc_get(re), OnigDefaultSyntax, NULL);
onig_free(rp);
}
if (err) {
options = RREGEXP(re)->ptr->options;
ptr = (UChar*)RREGEXP_SRC_PTR(re);
len = RREGEXP_SRC_LEN(re);
}
}
if (*option_to_str(optbuf, options)) rb_str_buf_cat2(str, optbuf);
if ((options & embeddable) != embeddable) {
optbuf[0] = '-';
option_to_str(optbuf + 1, ~options);
rb_str_buf_cat2(str, optbuf);
}
rb_str_buf_cat2(str, ":");
rb_reg_expr_str(str, (char*)ptr, len);
rb_str_buf_cat2(str, ")");
rb_enc_copy(str, re);
OBJ_INFECT(str, re);
return str;
}
static void
rb_reg_raise(const char *s, long len, const char *err, VALUE re)
{
VALUE desc = rb_reg_desc(s, len, re);
rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING_PTR(desc));
}
static VALUE
rb_enc_reg_error_desc(const char *s, long len, rb_encoding *enc, int options, const char *err)
{
char opts[6];
VALUE desc = rb_str_buf_new2(err);
rb_enc_associate(desc, enc);
rb_str_buf_cat2(desc, ": /");
rb_reg_expr_str(desc, s, len);
opts[0] = '/';
option_to_str(opts + 1, options);
rb_str_buf_cat2(desc, opts);
return rb_exc_new3(rb_eRegexpError, desc);
}
static void
rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err)
{
rb_exc_raise(rb_enc_reg_error_desc(s, len, enc, options, err));
}
static VALUE
rb_reg_error_desc(VALUE str, int options, const char *err)
{
return rb_enc_reg_error_desc(RSTRING_PTR(str), RSTRING_LEN(str),
rb_enc_get(str), options, err);
}
static void
rb_reg_raise_str(VALUE str, int options, const char *err)
{
rb_exc_raise(rb_reg_error_desc(str, options, err));
}
/*
* call-seq:
* rxp.casefold? => true or false
*
* Returns the value of the case-insensitive flag.
*
* /a/.casefold? #=> false
* /a/i.casefold? #=> true
* /(?i:a)/.casefold? #=> false
*/
static VALUE
rb_reg_casefold_p(VALUE re)
{
rb_reg_check(re);
if (RREGEXP(re)->ptr->options & ONIG_OPTION_IGNORECASE) return Qtrue;
return Qfalse;
}
/*
* call-seq:
* rxp.options => fixnum
*
* Returns the set of bits corresponding to the options used when creating this
* Regexp (see <code>Regexp::new</code> for details. Note that additional bits
* may be set in the returned options: these are used internally by the regular
* expression code. These extra bits are ignored if the options are passed to
* <code>Regexp::new</code>.
*
* Regexp::IGNORECASE #=> 1
* Regexp::EXTENDED #=> 2
* Regexp::MULTILINE #=> 4
*
* /cat/.options #=> 0
* /cat/ix.options #=> 3
* Regexp.new('cat', true).options #=> 1
* /\xa1\xa2/e.options #=> 16
*
* r = /cat/ix
* Regexp.new(r.source, r.options) #=> /cat/ix
*/
static VALUE
rb_reg_options_m(VALUE re)
{
int options = rb_reg_options(re);
return INT2NUM(options);
}
static int
reg_names_iter(const OnigUChar *name, const OnigUChar *name_end,
int back_num, int *back_refs, OnigRegex regex, void *arg)
{
VALUE ary = (VALUE)arg;
rb_ary_push(ary, rb_str_new((const char *)name, name_end-name));
return 0;
}
/*
* call-seq:
* rxp.names => [name1, name2, ...]
*
* Returns a list of names of captures as an array of strings.
*
* /(?<foo>.)(?<bar>.)(?<baz>.)/.names
* #=> ["foo", "bar", "baz"]
*
* /(?<foo>.)(?<foo>.)/.names
* #=> ["foo"]
*
* /(.)(.)/.names
* #=> []
*/
static VALUE
rb_reg_names(VALUE re)
{
VALUE ary = rb_ary_new();
rb_reg_check(re);
onig_foreach_name(RREGEXP(re)->ptr, reg_names_iter, (void*)ary);
return ary;
}
static int
reg_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
int back_num, int *back_refs, OnigRegex regex, void *arg)
{
VALUE hash = (VALUE)arg;
VALUE ary = rb_ary_new2(back_num);
int i;
for(i = 0; i < back_num; i++)
rb_ary_store(ary, i, INT2NUM(back_refs[i]));
rb_hash_aset(hash, rb_str_new((const char*)name, name_end-name),ary);
return 0;
}
/*
* call-seq:
* rxp.named_captures => hash
*
* Returns a hash representing information about named captures of <i>rxp</i>.
*
* A key of the hash is a name of the named captures.
* A value of the hash is an array which is list of indexes of corresponding
* named captures.
*
* /(?<foo>.)(?<bar>.)/.named_captures
* #=> {"foo"=>[1], "bar"=>[2]}
*
* /(?<foo>.)(?<foo>.)/.named_captures
* #=> {"foo"=>[1, 2]}
*
* If there are no named captures, an empty hash is returned.
*
* /(.)(.)/.named_captures
* #=> {}
*/
static VALUE
rb_reg_named_captures(VALUE re)
{
VALUE hash = rb_hash_new();
rb_reg_check(re);
onig_foreach_name(RREGEXP(re)->ptr, reg_named_captures_iter, (void*)hash);
return hash;
}
static Regexp*
make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_buffer err,
const char *sourcefile, int sourceline)
{
Regexp *rp;
int r;
OnigErrorInfo einfo;
/* Handle escaped characters first. */
/* Build a copy of the string (in dest) with the
escaped characters translated, and generate the regex
from that.
*/
r = onig_new(&rp, (UChar*)s, (UChar*)(s + len), flags,
enc, OnigDefaultSyntax, &einfo);
if (r) {
onig_error_code_to_str((UChar*)err, r, &einfo);
return 0;
}
return rp;
}
/*
* Document-class: MatchData
*
* <code>MatchData</code> is the type of the special variable <code>$~</code>,
* and is the type of the object returned by <code>Regexp#match</code> and
* <code>Regexp.last_match</code>. It encapsulates all the results of a pattern
* match, results normally accessed through the special variables
* <code>$&</code>, <code>$'</code>, <code>$`</code>, <code>$1</code>,
* <code>$2</code>, and so on.
*
*/
VALUE rb_cMatch;
static VALUE
match_alloc(VALUE klass)
{
NEWOBJ(match, struct RMatch);
OBJSETUP(match, klass, T_MATCH);
match->str = 0;
match->rmatch = 0;
match->regexp = 0;
match->rmatch = ALLOC(struct rmatch);
MEMZERO(match->rmatch, struct rmatch, 1);
return (VALUE)match;
}
typedef struct {
long byte_pos;
long char_pos;
} pair_t;
static int
pair_byte_cmp(const void *pair1, const void *pair2)
{
long diff = ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos;
#if SIZEOF_LONG > SIZEOF_INT
return diff ? diff > 0 ? 1 : -1 : 0;
#else
return (int)diff;
#endif
}
static void
update_char_offset(VALUE match)
{
struct rmatch *rm = RMATCH(match)->rmatch;
struct re_registers *regs;
int i, num_regs, num_pos;
long c;
char *s, *p, *q, *e;
rb_encoding *enc;
pair_t *pairs;
if (rm->char_offset_updated)
return;
regs = &rm->regs;
num_regs = rm->regs.num_regs;
if (rm->char_offset_num_allocated < num_regs) {
REALLOC_N(rm->char_offset, struct rmatch_offset, num_regs);
rm->char_offset_num_allocated = num_regs;
}
enc = rb_enc_get(RMATCH(match)->str);
if (rb_enc_mbmaxlen(enc) == 1) {
for (i = 0; i < num_regs; i++) {
rm->char_offset[i].beg = BEG(i);
rm->char_offset[i].end = END(i);
}
rm->char_offset_updated = 1;
return;
}
pairs = ALLOCA_N(pair_t, num_regs*2);
num_pos = 0;
for (i = 0; i < num_regs; i++) {
if (BEG(i) < 0)
continue;
pairs[num_pos++].byte_pos = BEG(i);
pairs[num_pos++].byte_pos = END(i);
}
qsort(pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
s = p = RSTRING_PTR(RMATCH(match)->str);
e = s + RSTRING_LEN(RMATCH(match)->str);
c = 0;
for (i = 0; i < num_pos; i++) {
q = s + pairs[i].byte_pos;
c += rb_enc_strlen(p, q, enc);
pairs[i].char_pos = c;
p = q;
}
for (i = 0; i < num_regs; i++) {
pair_t key, *found;
if (BEG(i) < 0) {
rm->char_offset[i].beg = -1;
rm->char_offset[i].end = -1;
continue;
}
key.byte_pos = BEG(i);
found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
rm->char_offset[i].beg = found->char_pos;
key.byte_pos = END(i);
found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
rm->char_offset[i].end = found->char_pos;
}
rm->char_offset_updated = 1;
}
static void
match_check(VALUE match)
{
if (!RMATCH(match)->regexp) {
rb_raise(rb_eTypeError, "uninitialized Match");
}
}
/* :nodoc: */
static VALUE
match_init_copy(VALUE obj, VALUE orig)
{
struct rmatch *rm;
if (obj == orig) return obj;
if (!rb_obj_is_instance_of(orig, rb_obj_class(obj))) {
rb_raise(rb_eTypeError, "wrong argument class");
}
RMATCH(obj)->str = RMATCH(orig)->str;
RMATCH(obj)->regexp = RMATCH(orig)->regexp;
rm = RMATCH(obj)->rmatch;
onig_region_copy(&rm->regs, RMATCH_REGS(orig));
if (!RMATCH(orig)->rmatch->char_offset_updated) {
rm->char_offset_updated = 0;
}
else {
if (rm->char_offset_num_allocated < rm->regs.num_regs) {
REALLOC_N(rm->char_offset, struct rmatch_offset, rm->regs.num_regs);
rm->char_offset_num_allocated = rm->regs.num_regs;
}
MEMCPY(rm->char_offset, RMATCH(orig)->rmatch->char_offset,
struct rmatch_offset, rm->regs.num_regs);
rm->char_offset_updated = 1;
}
return obj;
}
/*
* call-seq:
* mtch.regexp => regexp
*
* Returns the regexp.
*
* m = /a.*b/.match("abc")
* m.regexp #=> /a.*b/
*/
static VALUE
match_regexp(VALUE match)
{
match_check(match);
return RMATCH(match)->regexp;
}
/*
* call-seq:
* mtch.names => [name1, name2, ...]
*
* Returns a list of names of captures as an array of strings.
* It is same as mtch.regexp.names.
*
* /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names
* #=> ["foo", "bar", "baz"]
*
* m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" y:nil>
* m.names #=> ["x", "y"]
*/
static VALUE
match_names(VALUE match)
{
match_check(match);
return rb_reg_names(RMATCH(match)->regexp);
}
/*
* call-seq:
* mtch.length => integer
* mtch.size => integer
*
* Returns the number of elements in the match array.
*
* m = /(.)(.)(\d+)(\d)/.match("THX1138.")
* m.length #=> 5
* m.size #=> 5
*/
static VALUE
match_size(VALUE match)
{
match_check(match);
return INT2FIX(RMATCH_REGS(match)->num_regs);
}
static int
match_backref_number(VALUE match, VALUE backref)
{
const char *name;
int num;
struct re_registers *regs = RMATCH_REGS(match);
VALUE regexp = RMATCH(match)->regexp;
match_check(match);
switch(TYPE(backref)) {
default:
return NUM2INT(backref);
case T_SYMBOL:
name = rb_id2name(SYM2ID(backref));
break;
case T_STRING:
name = StringValueCStr(backref);
break;
}
num = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
(const unsigned char*)name,
(const unsigned char*)name + strlen(name),
regs);
if (num < 1) {
rb_raise(rb_eIndexError, "undefined group name reference: %s", name);
}