-
Notifications
You must be signed in to change notification settings - Fork 0
/
re.c
2394 lines (2118 loc) · 55.2 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-2003 Yukihiro Matsumoto
**********************************************************************/
#include "ruby.h"
#include "re.h"
#include "regint.h"
#include <ctype.h>
#define MBCTYPE_ASCII 0
#define MBCTYPE_EUC 1
#define MBCTYPE_SJIS 2
#define MBCTYPE_UTF8 3
static VALUE rb_eRegexpError;
#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(p1, p2, len)
char *p1, *p2;
long len;
{
int tmp;
while (len--) {
if (tmp = casetable[(unsigned)*p1++] - casetable[(unsigned)*p2++])
return tmp;
}
return 0;
}
int
rb_memcmp(p1, p2, len)
char *p1, *p2;
long len;
{
if (!ruby_ignorecase) {
return memcmp(p1, p2, len);
}
return rb_memcicmp(p1, p2, len);
}
long
rb_memsearch(x0, m, y0, n)
char *x0, *y0;
long m, n;
{
unsigned char *x = (unsigned char *)x0, *y = (unsigned char *)y0;
unsigned char *s, *e;
long i;
int d;
unsigned long hx, hy;
#define KR_REHASH(a, b, h) (((h) << 1) - ((long)(a)<<d) + (b))
if (m > n) return -1;
s = y; e = s + n - m;
/* Preprocessing */
/* computes d = 2^(m-1) with
the left-shift operator */
d = sizeof(hx) * CHAR_BIT - 1;
if (d > m) d = m;
if (ruby_ignorecase) {
if (n == m) {
return rb_memcicmp(x, s, m) == 0 ? 0 : -1;
}
/* Prepare hash value */
for (hy = hx = i = 0; i < d; ++i) {
hx = KR_REHASH(0, casetable[x[i]], hx);
hy = KR_REHASH(0, casetable[s[i]], hy);
}
/* Searching */
while (hx != hy || rb_memcicmp(x, s, m)) {
if (s >= e) return -1;
hy = KR_REHASH(casetable[*s], casetable[*(s+d)], hy);
s++;
}
}
else {
if (n == m) {
return memcmp(x, s, m) == 0 ? 0 : -1;
}
/* Prepare hash value */
for (hy = hx = i = 0; i < d; ++i) {
hx = KR_REHASH(0, x[i], hx);
hy = KR_REHASH(0, s[i], hy);
}
/* Searching */
while (hx != hy || memcmp(x, s, m)) {
if (s >= e) return -1;
hy = KR_REHASH(*s, *(s+d), hy);
s++;
}
}
return s-y;
}
#define REG_CASESTATE FL_USER0
#define KCODE_NONE 0
#define KCODE_EUC FL_USER1
#define KCODE_SJIS FL_USER2
#define KCODE_UTF8 FL_USER3
#define KCODE_FIXED FL_USER4
#define KCODE_MASK (KCODE_EUC|KCODE_SJIS|KCODE_UTF8)
static int reg_kcode = DEFAULT_KCODE;
static void
kcode_euc(re)
struct RRegexp *re;
{
FL_UNSET(re, KCODE_MASK);
FL_SET(re, KCODE_EUC);
FL_SET(re, KCODE_FIXED);
}
static void
kcode_sjis(re)
struct RRegexp *re;
{
FL_UNSET(re, KCODE_MASK);
FL_SET(re, KCODE_SJIS);
FL_SET(re, KCODE_FIXED);
}
static void
kcode_utf8(re)
struct RRegexp *re;
{
FL_UNSET(re, KCODE_MASK);
FL_SET(re, KCODE_UTF8);
FL_SET(re, KCODE_FIXED);
}
static void
kcode_none(re)
struct RRegexp *re;
{
FL_UNSET(re, KCODE_MASK);
FL_SET(re, KCODE_FIXED);
}
static int curr_kcode;
static void
kcode_set_option(re)
VALUE re;
{
if (!FL_TEST(re, KCODE_FIXED)) return;
curr_kcode = RBASIC(re)->flags & KCODE_MASK;
if (reg_kcode == curr_kcode) return;
switch (curr_kcode) {
case KCODE_NONE:
onigenc_set_default_encoding(ONIG_ENCODING_ASCII);
break;
case KCODE_EUC:
onigenc_set_default_encoding(ONIG_ENCODING_EUC_JP);
break;
case KCODE_SJIS:
onigenc_set_default_encoding(ONIG_ENCODING_SJIS);
break;
case KCODE_UTF8:
onigenc_set_default_encoding(ONIG_ENCODING_UTF8);
break;
}
}
static void
kcode_reset_option()
{
if (reg_kcode == curr_kcode) return;
switch (reg_kcode) {
case KCODE_NONE:
onigenc_set_default_encoding(ONIG_ENCODING_ASCII);
break;
case KCODE_EUC:
onigenc_set_default_encoding(ONIG_ENCODING_EUC_JP);
break;
case KCODE_SJIS:
onigenc_set_default_encoding(ONIG_ENCODING_SJIS);
break;
case KCODE_UTF8:
onigenc_set_default_encoding(ONIG_ENCODING_UTF8);
break;
}
}
int
rb_reg_mbclen2(c, re)
unsigned int c;
VALUE re;
{
int len;
unsigned char uc = (unsigned char)c;
if (!FL_TEST(re, KCODE_FIXED))
return mbclen(uc);
kcode_set_option(re);
len = mbclen(uc);
kcode_reset_option();
return len;
}
static void
rb_reg_check(re)
VALUE re;
{
if (!RREGEXP(re)->ptr || !RREGEXP(re)->str) {
rb_raise(rb_eTypeError, "uninitialized Regexp");
}
}
static void
rb_reg_expr_str(str, s, len)
VALUE str;
const char *s;
long len;
{
const char *p, *pend;
int need_escape = 0;
p = s; pend = p + len;
while (p<pend) {
if (*p == '/' || (!ISPRINT(*p) && !ismbchar(*p))) {
need_escape = 1;
break;
}
p += mbclen(*p);
}
if (!need_escape) {
rb_str_buf_cat(str, s, len);
}
else {
p = s;
while (p<pend) {
if (*p == '\\') {
int n = mbclen(p[1]) + 1;
rb_str_buf_cat(str, p, n);
p += n;
continue;
}
else if (*p == '/') {
char c = '\\';
rb_str_buf_cat(str, &c, 1);
rb_str_buf_cat(str, p, 1);
}
else if (ismbchar(*p)) {
rb_str_buf_cat(str, p, mbclen(*p));
p += mbclen(*p);
continue;
}
else if (ISPRINT(*p)) {
rb_str_buf_cat(str, p, 1);
}
else if (!ISSPACE(*p)) {
char b[8];
sprintf(b, "\\%03o", *p & 0377);
rb_str_buf_cat(str, b, 4);
}
else {
rb_str_buf_cat(str, p, 1);
}
p++;
}
}
}
static VALUE
rb_reg_desc(s, len, re)
const char *s;
long len;
VALUE re;
{
VALUE str = rb_str_buf_new2("/");
rb_reg_expr_str(str, s, len);
rb_str_buf_cat2(str, "/");
if (re) {
rb_reg_check(re);
if (RREGEXP(re)->ptr->options & ONIG_OPTION_MULTILINE)
rb_str_buf_cat2(str, "m");
if (RREGEXP(re)->ptr->options & ONIG_OPTION_IGNORECASE)
rb_str_buf_cat2(str, "i");
if (RREGEXP(re)->ptr->options & ONIG_OPTION_EXTEND)
rb_str_buf_cat2(str, "x");
if (FL_TEST(re, KCODE_FIXED)) {
switch ((RBASIC(re)->flags & KCODE_MASK)) {
case KCODE_NONE:
rb_str_buf_cat2(str, "n");
break;
case KCODE_EUC:
rb_str_buf_cat2(str, "e");
break;
case KCODE_SJIS:
rb_str_buf_cat2(str, "s");
break;
case KCODE_UTF8:
rb_str_buf_cat2(str, "u");
break;
}
}
}
OBJ_INFECT(str, re);
return str;
}
/*
* call-seq:
* rxp.source => str
*
* Returns the original string of the pattern.
*
* /ab+c/ix.source #=> "ab+c"
*/
static VALUE
rb_reg_source(re)
VALUE re;
{
VALUE str;
rb_reg_check(re);
str = rb_str_new(RREGEXP(re)->str,RREGEXP(re)->len);
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.to_s #=> /ab+c/ix
*/
static VALUE
rb_reg_inspect(re)
VALUE re;
{
rb_reg_check(re);
return rb_reg_desc(RREGEXP(re)->str, RREGEXP(re)->len, re);
}
/*
* call-seq:
* rxp.to_s => str
*
* Returns a string containing the regular expression and its options (using the
* <code>(?xxx:yyy)</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(re)
VALUE re;
{
int options;
const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
long len;
const char* ptr;
VALUE str = rb_str_buf_new2("(?");
rb_reg_check(re);
options = RREGEXP(re)->ptr->options;
ptr = RREGEXP(re)->str;
len = RREGEXP(re)->len;
again:
if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
int err = 1;
ptr += 2;
if ((len -= 2) > 0) {
do {
if (*ptr == 'm') {
options |= ONIG_OPTION_MULTILINE;
}
else if (*ptr == 'i') {
options |= ONIG_OPTION_IGNORECASE;
}
else if (*ptr == 'x') {
options |= ONIG_OPTION_EXTEND;
}
else break;
++ptr;
} while (--len > 0);
}
if (len > 1 && *ptr == '-') {
++ptr;
--len;
do {
if (*ptr == 'm') {
options &= ~ONIG_OPTION_MULTILINE;
}
else if (*ptr == 'i') {
options &= ~ONIG_OPTION_IGNORECASE;
}
else if (*ptr == 'x') {
options &= ~ONIG_OPTION_EXTEND;
}
else break;
++ptr;
} while (--len > 0);
}
if (*ptr == ')') {
--len;
++ptr;
goto again;
}
if (*ptr == ':' && ptr[len-1] == ')') {
int r;
Regexp *rp;
kcode_set_option(re);
r = onig_alloc_init(&rp, ONIG_OPTION_DEFAULT,
ONIGENC_AMBIGUOUS_MATCH_DEFAULT,
onigenc_get_default_encoding(),
OnigDefaultSyntax);
if (r == 0) {
++ptr;
len -= 2;
err = (onig_compile(rp, ptr, ptr + len, NULL) != 0);
}
kcode_reset_option();
onig_free(rp);
}
if (err) {
options = RREGEXP(re)->ptr->options;
ptr = RREGEXP(re)->str;
len = RREGEXP(re)->len;
}
}
if (options & ONIG_OPTION_MULTILINE) rb_str_buf_cat2(str, "m");
if (options & ONIG_OPTION_IGNORECASE) rb_str_buf_cat2(str, "i");
if (options & ONIG_OPTION_EXTEND) rb_str_buf_cat2(str, "x");
if ((options & embeddable) != embeddable) {
rb_str_buf_cat2(str, "-");
if (!(options & ONIG_OPTION_MULTILINE)) rb_str_buf_cat2(str, "m");
if (!(options & ONIG_OPTION_IGNORECASE)) rb_str_buf_cat2(str, "i");
if (!(options & ONIG_OPTION_EXTEND)) rb_str_buf_cat2(str, "x");
}
rb_str_buf_cat2(str, ":");
rb_reg_expr_str(str, ptr, len);
rb_str_buf_cat2(str, ")");
OBJ_INFECT(str, re);
return str;
}
static void
rb_reg_raise(s, len, err, re, ce)
const char *s;
long len;
const char *err;
VALUE re;
int ce;
{
VALUE desc = rb_reg_desc(s, len, re);
if (ce)
rb_compile_error("%s: %s", err, RSTRING(desc)->ptr);
else
rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING(desc)->ptr);
}
/*
* call-seq:
* rxp.casefold? => true or false
*
* Returns the value of the case-insensitive flag.
*/
static VALUE
rb_reg_casefold_p(re)
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 #=> 128
* /cat/ix.options #=> 131
* Regexp.new('cat', true).options #=> 129
* Regexp.new('cat', 0, 's').options #=> 384
*
* r = /cat/ix
* Regexp.new(r.source, r.options) #=> /cat/ix
*/
static VALUE
rb_reg_options_m(re)
VALUE re;
{
int options = rb_reg_options(re);
return INT2NUM(options);
}
/*
* call-seq:
* rxp.kcode => str
*
* Returns the character set code for the regexp.
*/
static VALUE
rb_reg_kcode_m(re)
VALUE re;
{
char *kcode;
if (FL_TEST(re, KCODE_FIXED)) {
switch (RBASIC(re)->flags & KCODE_MASK) {
case KCODE_NONE:
kcode = "none"; break;
case KCODE_EUC:
kcode = "euc"; break;
case KCODE_SJIS:
kcode = "sjis"; break;
case KCODE_UTF8:
kcode = "utf8"; break;
default:
rb_bug("unknown kcode - should not happen");
break;
}
return rb_str_new2(kcode);
}
return Qnil;
}
static Regexp*
make_regexp(s, len, flags, ce)
const char *s;
long len;
int flags;
int ce;
{
Regexp *rp;
char err[ONIG_MAX_ERROR_MESSAGE_LEN];
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_alloc_init(&rp, flags,
ONIGENC_AMBIGUOUS_MATCH_DEFAULT,
onigenc_get_default_encoding(),
OnigDefaultSyntax);
if (r) {
onig_error_code_to_str((UChar* )err, r);
rb_reg_raise(s, len, err, 0, ce);
}
r = onig_compile(rp, (UChar* )s, (UChar* )(s + len), &einfo);
if (r != 0) {
onig_free(rp);
(void )onig_error_code_to_str((UChar* )err, r, &einfo);
rb_reg_raise(s, len, err, 0, ce);
}
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. <code>Matchdata</code> is also known as
* <code>MatchingData</code>.
*
*/
static VALUE rb_cMatch;
static VALUE match_alloc _((VALUE));
static VALUE
match_alloc(klass)
VALUE klass;
{
NEWOBJ(match, struct RMatch);
OBJSETUP(match, klass, T_MATCH);
match->str = 0;
match->regs = 0;
match->regs = ALLOC(struct re_registers);
MEMZERO(match->regs, struct re_registers, 1);
return (VALUE)match;
}
/* :nodoc: */
static VALUE
match_init_copy(obj, orig)
VALUE obj, orig;
{
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;
onig_region_free(RMATCH(obj)->regs, 0);
RMATCH(obj)->regs->allocated = 0;
onig_region_copy(RMATCH(obj)->regs, RMATCH(orig)->regs);
return obj;
}
/*
* 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(match)
VALUE match;
{
return INT2FIX(RMATCH(match)->regs->num_regs);
}
/*
* call-seq:
* mtch.offset(n) => array
*
* Returns a two-element array containing the beginning and ending offsets of
* the <em>n</em>th match.
*
* m = /(.)(.)(\d+)(\d)/.match("THX1138.")
* m.offset(0) #=> [1, 7]
* m.offset(4) #=> [6, 7]
*/
static VALUE
match_offset(match, n)
VALUE match, n;
{
int i = NUM2INT(n);
if (i < 0 || RMATCH(match)->regs->num_regs <= i)
rb_raise(rb_eIndexError, "index %d out of matches", i);
if (RMATCH(match)->regs->beg[i] < 0)
return rb_assoc_new(Qnil, Qnil);
return rb_assoc_new(INT2FIX(RMATCH(match)->regs->beg[i]),
INT2FIX(RMATCH(match)->regs->end[i]));
}
/*
* call-seq:
* mtch.begin(n) => integer
*
* Returns the offset of the start of the <em>n</em>th element of the match
* array in the string.
*
* m = /(.)(.)(\d+)(\d)/.match("THX1138.")
* m.begin(0) #=> 1
* m.begin(2) #=> 2
*/
static VALUE
match_begin(match, n)
VALUE match, n;
{
int i = NUM2INT(n);
if (i < 0 || RMATCH(match)->regs->num_regs <= i)
rb_raise(rb_eIndexError, "index %d out of matches", i);
if (RMATCH(match)->regs->beg[i] < 0)
return Qnil;
return INT2FIX(RMATCH(match)->regs->beg[i]);
}
/*
* call-seq:
* mtch.end(n) => integer
*
* Returns the offset of the character immediately following the end of the
* <em>n</em>th element of the match array in the string.
*
* m = /(.)(.)(\d+)(\d)/.match("THX1138.")
* m.end(0) #=> 7
* m.end(2) #=> 3
*/
static VALUE
match_end(match, n)
VALUE match, n;
{
int i = NUM2INT(n);
if (i < 0 || RMATCH(match)->regs->num_regs <= i)
rb_raise(rb_eIndexError, "index %d out of matches", i);
if (RMATCH(match)->regs->beg[i] < 0)
return Qnil;
return INT2FIX(RMATCH(match)->regs->end[i]);
}
#define MATCH_BUSY FL_USER2
void
rb_match_busy(match)
VALUE match;
{
FL_SET(match, MATCH_BUSY);
}
int ruby_ignorecase;
static int may_need_recompile;
static void
rb_reg_prepare_re(re)
VALUE re;
{
int need_recompile = 0;
int state;
rb_reg_check(re);
state = FL_TEST(re, REG_CASESTATE);
/* ignorecase status */
if (ruby_ignorecase && !state) {
FL_SET(re, REG_CASESTATE);
RREGEXP(re)->ptr->options |= ONIG_OPTION_IGNORECASE;
need_recompile = 1;
}
if (!ruby_ignorecase && state) {
FL_UNSET(re, REG_CASESTATE);
RREGEXP(re)->ptr->options &= ~ONIG_OPTION_IGNORECASE;
need_recompile = 1;
}
if (!FL_TEST(re, KCODE_FIXED) &&
(RBASIC(re)->flags & KCODE_MASK) != reg_kcode) {
need_recompile = 1;
RBASIC(re)->flags &= ~KCODE_MASK;
RBASIC(re)->flags |= reg_kcode;
}
if (need_recompile) {
char err[ONIG_MAX_ERROR_MESSAGE_LEN];
int r;
OnigErrorInfo einfo;
regex_t *reg;
UChar *pattern;
if (FL_TEST(re, KCODE_FIXED))
kcode_set_option(re);
rb_reg_check(re);
reg = RREGEXP(re)->ptr;
pattern = ((UChar* )RREGEXP(re)->str);
r = onig_recompile(reg, pattern, pattern + RREGEXP(re)->len,
reg->options, onigenc_get_default_encoding(),
OnigDefaultSyntax, &einfo);
if (r != 0) {
(void )onig_error_code_to_str((UChar* )err, r, &einfo);
rb_reg_raise(pattern, RREGEXP(re)->len, err, re, Qfalse);
}
}
}
long
rb_reg_adjust_startpos(re, str, pos, reverse)
VALUE re, str;
long pos, reverse;
{
long range;
OnigEncoding enc;
UChar *p, *string;
rb_reg_check(re);
if (may_need_recompile) rb_reg_prepare_re(re);
if (FL_TEST(re, KCODE_FIXED))
kcode_set_option(re);
else if (reg_kcode != curr_kcode)
kcode_reset_option();
if (reverse) {
range = -pos;
}
else {
range = RSTRING(str)->len - pos;
}
enc = (RREGEXP(re)->ptr)->enc;
if (pos > 0 && ONIGENC_MBC_MAXLEN(enc) != 1 && pos < RSTRING(str)->len) {
string = (UChar* )RSTRING(str)->ptr;
if (range > 0) {
p = onigenc_get_right_adjust_char_head(enc, string, string + pos);
}
else {
p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, string, string + pos);
}
return p - string;
}
return pos;
}
long
rb_reg_search(re, str, pos, reverse)
VALUE re, str;
long pos, reverse;
{
long result;
VALUE match;
static struct re_registers regs;
long range;
if (pos > RSTRING(str)->len || pos < 0) {
rb_backref_set(Qnil);
return -1;
}
rb_reg_check(re);
if (may_need_recompile) rb_reg_prepare_re(re);
if (FL_TEST(re, KCODE_FIXED))
kcode_set_option(re);
else if (reg_kcode != curr_kcode)
kcode_reset_option();
if (reverse) {
range = -pos;
}
else {
range = RSTRING(str)->len - pos;
}
result = onig_search(RREGEXP(re)->ptr,
(UChar* )(RSTRING(str)->ptr),
((UChar* )(RSTRING(str)->ptr) + RSTRING(str)->len),
((UChar* )(RSTRING(str)->ptr) + pos),
((UChar* )(RSTRING(str)->ptr) + pos + range),
®s, ONIG_OPTION_NONE);
if (FL_TEST(re, KCODE_FIXED))
kcode_reset_option();
if (result < 0) {
if (result == ONIG_MISMATCH) {
rb_backref_set(Qnil);
return result;
}
else {
char err[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str((UChar* )err, result);
rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len, err, 0, Qfalse);
}
}
match = rb_backref_get();
if (NIL_P(match) || FL_TEST(match, MATCH_BUSY)) {
match = match_alloc(rb_cMatch);
}
else {
if (rb_safe_level() >= 3)
OBJ_TAINT(match);
else
FL_UNSET(match, FL_TAINT);
}
onig_region_copy(RMATCH(match)->regs, ®s);
RMATCH(match)->str = rb_str_new4(str);
rb_backref_set(match);
OBJ_INFECT(match, re);
OBJ_INFECT(match, str);
return result;
}
VALUE
rb_reg_nth_defined(nth, match)