-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstrscan.c
2374 lines (2142 loc) · 56.5 KB
/
strscan.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
/*
$Id$
Copyright (c) 1999-2006 Minero Aoki
This program is free software.
You can redistribute this program under the terms of the Ruby's or 2-clause
BSD License. For details, see the COPYING and LICENSE.txt files.
*/
#include "ruby/ruby.h"
#include "ruby/re.h"
#include "ruby/encoding.h"
#ifdef RUBY_EXTCONF_H
# include RUBY_EXTCONF_H
#endif
#ifdef HAVE_ONIG_REGION_MEMSIZE
extern size_t onig_region_memsize(const struct re_registers *regs);
#endif
#include <stdbool.h>
#define STRSCAN_VERSION "3.1.2"
/* =======================================================================
Data Type Definitions
======================================================================= */
static VALUE StringScanner;
static VALUE ScanError;
static ID id_byteslice;
static int usascii_encindex, utf8_encindex, binary_encindex;
struct strscanner
{
/* multi-purpose flags */
unsigned long flags;
#define FLAG_MATCHED (1 << 0)
/* the string to scan */
VALUE str;
/* scan pointers */
long prev; /* legal only when MATCHED_P(s) */
long curr; /* always legal */
/* the regexp register; legal only when MATCHED_P(s) */
struct re_registers regs;
/* regexp used for last scan */
VALUE regex;
/* anchor mode */
bool fixed_anchor_p;
};
#define MATCHED_P(s) ((s)->flags & FLAG_MATCHED)
#define MATCHED(s) ((s)->flags |= FLAG_MATCHED)
#define CLEAR_MATCHED(s) ((s)->flags &= ~FLAG_MATCHED)
#define CLEAR_NAMED_CAPTURES(s) ((s)->regex = Qnil)
#define CLEAR_MATCH_STATUS(s) do {\
CLEAR_MATCHED(s);\
CLEAR_NAMED_CAPTURES(s);\
} while (0)
#define S_PBEG(s) (RSTRING_PTR((s)->str))
#define S_LEN(s) (RSTRING_LEN((s)->str))
#define S_PEND(s) (S_PBEG(s) + S_LEN(s))
#define CURPTR(s) (S_PBEG(s) + (s)->curr)
#define S_RESTLEN(s) (S_LEN(s) - (s)->curr)
#define EOS_P(s) ((s)->curr >= RSTRING_LEN(p->str))
#define GET_SCANNER(obj,var) do {\
(var) = check_strscan(obj);\
if (NIL_P((var)->str)) rb_raise(rb_eArgError, "uninitialized StringScanner object");\
} while (0)
/* =======================================================================
Function Prototypes
======================================================================= */
static inline long minl _((const long n, const long x));
static VALUE extract_range _((struct strscanner *p, long beg_i, long end_i));
static VALUE extract_beg_len _((struct strscanner *p, long beg_i, long len));
static struct strscanner *check_strscan _((VALUE obj));
static void strscan_mark _((void *p));
static void strscan_free _((void *p));
static size_t strscan_memsize _((const void *p));
static VALUE strscan_s_allocate _((VALUE klass));
static VALUE strscan_initialize _((int argc, VALUE *argv, VALUE self));
static VALUE strscan_init_copy _((VALUE vself, VALUE vorig));
static VALUE strscan_s_mustc _((VALUE self));
static VALUE strscan_terminate _((VALUE self));
static VALUE strscan_clear _((VALUE self));
static VALUE strscan_get_string _((VALUE self));
static VALUE strscan_set_string _((VALUE self, VALUE str));
static VALUE strscan_concat _((VALUE self, VALUE str));
static VALUE strscan_get_pos _((VALUE self));
static VALUE strscan_set_pos _((VALUE self, VALUE pos));
static VALUE strscan_do_scan _((VALUE self, VALUE regex,
int succptr, int getstr, int headonly));
static VALUE strscan_scan _((VALUE self, VALUE re));
static VALUE strscan_match_p _((VALUE self, VALUE re));
static VALUE strscan_skip _((VALUE self, VALUE re));
static VALUE strscan_check _((VALUE self, VALUE re));
static VALUE strscan_scan_full _((VALUE self, VALUE re,
VALUE succp, VALUE getp));
static VALUE strscan_scan_until _((VALUE self, VALUE re));
static VALUE strscan_skip_until _((VALUE self, VALUE re));
static VALUE strscan_check_until _((VALUE self, VALUE re));
static VALUE strscan_search_full _((VALUE self, VALUE re,
VALUE succp, VALUE getp));
static void adjust_registers_to_matched _((struct strscanner *p));
static VALUE strscan_getch _((VALUE self));
static VALUE strscan_get_byte _((VALUE self));
static VALUE strscan_getbyte _((VALUE self));
static VALUE strscan_peek _((VALUE self, VALUE len));
static VALUE strscan_peep _((VALUE self, VALUE len));
static VALUE strscan_scan_base10_integer _((VALUE self));
static VALUE strscan_unscan _((VALUE self));
static VALUE strscan_bol_p _((VALUE self));
static VALUE strscan_eos_p _((VALUE self));
static VALUE strscan_empty_p _((VALUE self));
static VALUE strscan_rest_p _((VALUE self));
static VALUE strscan_matched_p _((VALUE self));
static VALUE strscan_matched _((VALUE self));
static VALUE strscan_matched_size _((VALUE self));
static VALUE strscan_aref _((VALUE self, VALUE idx));
static VALUE strscan_pre_match _((VALUE self));
static VALUE strscan_post_match _((VALUE self));
static VALUE strscan_rest _((VALUE self));
static VALUE strscan_rest_size _((VALUE self));
static VALUE strscan_inspect _((VALUE self));
static VALUE inspect1 _((struct strscanner *p));
static VALUE inspect2 _((struct strscanner *p));
/* =======================================================================
Utils
======================================================================= */
static VALUE
str_new(struct strscanner *p, const char *ptr, long len)
{
VALUE str = rb_str_new(ptr, len);
rb_enc_copy(str, p->str);
return str;
}
static inline long
minl(const long x, const long y)
{
return (x < y) ? x : y;
}
static VALUE
extract_range(struct strscanner *p, long beg_i, long end_i)
{
if (beg_i > S_LEN(p)) return Qnil;
end_i = minl(end_i, S_LEN(p));
return str_new(p, S_PBEG(p) + beg_i, end_i - beg_i);
}
static VALUE
extract_beg_len(struct strscanner *p, long beg_i, long len)
{
if (beg_i > S_LEN(p)) return Qnil;
len = minl(len, S_LEN(p) - beg_i);
return str_new(p, S_PBEG(p) + beg_i, len);
}
/* =======================================================================
Constructor
======================================================================= */
static void
strscan_mark(void *ptr)
{
struct strscanner *p = ptr;
rb_gc_mark(p->str);
rb_gc_mark(p->regex);
}
static void
strscan_free(void *ptr)
{
struct strscanner *p = ptr;
onig_region_free(&(p->regs), 0);
ruby_xfree(p);
}
static size_t
strscan_memsize(const void *ptr)
{
const struct strscanner *p = ptr;
size_t size = sizeof(*p) - sizeof(p->regs);
#ifdef HAVE_ONIG_REGION_MEMSIZE
size += onig_region_memsize(&p->regs);
#endif
return size;
}
static const rb_data_type_t strscanner_type = {
"StringScanner",
{strscan_mark, strscan_free, strscan_memsize},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};
static VALUE
strscan_s_allocate(VALUE klass)
{
struct strscanner *p;
VALUE obj = TypedData_Make_Struct(klass, struct strscanner, &strscanner_type, p);
CLEAR_MATCH_STATUS(p);
onig_region_init(&(p->regs));
p->str = Qnil;
return obj;
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* StringScanner.new(string, fixed_anchor: false) -> string_scanner
*
* Returns a new `StringScanner` object whose [stored string][1]
* is the given `string`;
* sets the [fixed-anchor property][10]:
*
* ```rb
* scanner = StringScanner.new('foobarbaz')
* scanner.string # => "foobarbaz"
* scanner.fixed_anchor? # => false
* put_situation(scanner)
* # Situation:
* # pos: 0
* # charpos: 0
* # rest: "foobarbaz"
* # rest_size: 9
* ```
*
*/
static VALUE
strscan_initialize(int argc, VALUE *argv, VALUE self)
{
struct strscanner *p;
VALUE str, options;
p = check_strscan(self);
rb_scan_args(argc, argv, "11", &str, &options);
options = rb_check_hash_type(options);
if (!NIL_P(options)) {
VALUE fixed_anchor;
ID keyword_ids[1];
keyword_ids[0] = rb_intern("fixed_anchor");
rb_get_kwargs(options, keyword_ids, 0, 1, &fixed_anchor);
if (fixed_anchor == Qundef) {
p->fixed_anchor_p = false;
}
else {
p->fixed_anchor_p = RTEST(fixed_anchor);
}
}
else {
p->fixed_anchor_p = false;
}
StringValue(str);
p->str = str;
return self;
}
static struct strscanner *
check_strscan(VALUE obj)
{
return rb_check_typeddata(obj, &strscanner_type);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* dup -> shallow_copy
*
* Returns a shallow copy of `self`;
* the [stored string][1] in the copy is the same string as in `self`.
*/
static VALUE
strscan_init_copy(VALUE vself, VALUE vorig)
{
struct strscanner *self, *orig;
self = check_strscan(vself);
orig = check_strscan(vorig);
if (self != orig) {
self->flags = orig->flags;
self->str = orig->str;
self->prev = orig->prev;
self->curr = orig->curr;
if (rb_reg_region_copy(&self->regs, &orig->regs))
rb_memerror();
RB_GC_GUARD(vorig);
}
return vself;
}
/* =======================================================================
Instance Methods
======================================================================= */
/*
* call-seq:
* StringScanner.must_C_version -> self
*
* Returns +self+; defined for backward compatibility.
*/
/* :nodoc: */
static VALUE
strscan_s_mustc(VALUE self)
{
return self;
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* reset -> self
*
* Sets both [byte position][2] and [character position][7] to zero,
* and clears [match values][9];
* returns +self+:
*
* ```rb
* scanner = StringScanner.new('foobarbaz')
* scanner.exist?(/bar/) # => 6
* scanner.reset # => #<StringScanner 0/9 @ "fooba...">
* put_situation(scanner)
* # Situation:
* # pos: 0
* # charpos: 0
* # rest: "foobarbaz"
* # rest_size: 9
* # => nil
* match_values_cleared?(scanner) # => true
* ```
*
*/
static VALUE
strscan_reset(VALUE self)
{
struct strscanner *p;
GET_SCANNER(self, p);
p->curr = 0;
CLEAR_MATCH_STATUS(p);
return self;
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
* :include: strscan/methods/terminate.md
*/
static VALUE
strscan_terminate(VALUE self)
{
struct strscanner *p;
GET_SCANNER(self, p);
p->curr = S_LEN(p);
CLEAR_MATCH_STATUS(p);
return self;
}
/*
* call-seq:
* clear -> self
*
* This method is obsolete; use the equivalent method StringScanner#terminate.
*/
/* :nodoc: */
static VALUE
strscan_clear(VALUE self)
{
rb_warning("StringScanner#clear is obsolete; use #terminate instead");
return strscan_terminate(self);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* string -> stored_string
*
* Returns the [stored string][1]:
*
* ```rb
* scanner = StringScanner.new('foobar')
* scanner.string # => "foobar"
* scanner.concat('baz')
* scanner.string # => "foobarbaz"
* ```
*
*/
static VALUE
strscan_get_string(VALUE self)
{
struct strscanner *p;
GET_SCANNER(self, p);
return p->str;
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* string = other_string -> other_string
*
* Replaces the [stored string][1] with the given `other_string`:
*
* - Sets both [positions][11] to zero.
* - Clears [match values][9].
* - Returns `other_string`.
*
* ```rb
* scanner = StringScanner.new('foobar')
* scanner.scan(/foo/)
* put_situation(scanner)
* # Situation:
* # pos: 3
* # charpos: 3
* # rest: "bar"
* # rest_size: 3
* match_values_cleared?(scanner) # => false
*
* scanner.string = 'baz' # => "baz"
* put_situation(scanner)
* # Situation:
* # pos: 0
* # charpos: 0
* # rest: "baz"
* # rest_size: 3
* match_values_cleared?(scanner) # => true
* ```
*
*/
static VALUE
strscan_set_string(VALUE self, VALUE str)
{
struct strscanner *p = check_strscan(self);
StringValue(str);
p->str = str;
p->curr = 0;
CLEAR_MATCH_STATUS(p);
return str;
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* concat(more_string) -> self
*
* - Appends the given `more_string`
* to the [stored string][1].
* - Returns `self`.
* - Does not affect the [positions][11]
* or [match values][9].
*
*
* ```rb
* scanner = StringScanner.new('foo')
* scanner.string # => "foo"
* scanner.terminate
* scanner.concat('barbaz') # => #<StringScanner 3/9 "foo" @ "barba...">
* scanner.string # => "foobarbaz"
* put_situation(scanner)
* # Situation:
* # pos: 3
* # charpos: 3
* # rest: "barbaz"
* # rest_size: 6
* ```
*
*/
static VALUE
strscan_concat(VALUE self, VALUE str)
{
struct strscanner *p;
GET_SCANNER(self, p);
StringValue(str);
rb_str_append(p->str, str);
return self;
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
* :include: strscan/methods/get_pos.md
*/
static VALUE
strscan_get_pos(VALUE self)
{
struct strscanner *p;
GET_SCANNER(self, p);
return INT2FIX(p->curr);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
* :include: strscan/methods/get_charpos.md
*/
static VALUE
strscan_get_charpos(VALUE self)
{
struct strscanner *p;
GET_SCANNER(self, p);
return LONG2NUM(rb_enc_strlen(S_PBEG(p), CURPTR(p), rb_enc_get(p->str)));
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
* :include: strscan/methods/set_pos.md
*/
static VALUE
strscan_set_pos(VALUE self, VALUE v)
{
struct strscanner *p;
long i;
GET_SCANNER(self, p);
i = NUM2INT(v);
if (i < 0) i += S_LEN(p);
if (i < 0) rb_raise(rb_eRangeError, "index out of range");
if (i > S_LEN(p)) rb_raise(rb_eRangeError, "index out of range");
p->curr = i;
return LONG2NUM(i);
}
static inline UChar *
match_target(struct strscanner *p)
{
if (p->fixed_anchor_p) {
return (UChar *)S_PBEG(p);
}
else
{
return (UChar *)CURPTR(p);
}
}
static inline void
set_registers(struct strscanner *p, size_t pos, size_t length)
{
const int at = 0;
OnigRegion *regs = &(p->regs);
onig_region_clear(regs);
if (onig_region_set(regs, at, 0, 0)) return;
if (p->fixed_anchor_p) {
regs->beg[at] = pos + p->curr;
regs->end[at] = pos + p->curr + length;
}
else
{
regs->beg[at] = pos;
regs->end[at] = pos + length;
}
}
static inline void
succ(struct strscanner *p)
{
if (p->fixed_anchor_p) {
p->curr = p->regs.end[0];
}
else
{
p->curr += p->regs.end[0];
}
}
static inline long
last_match_length(struct strscanner *p)
{
if (p->fixed_anchor_p) {
return p->regs.end[0] - p->prev;
}
else
{
return p->regs.end[0];
}
}
static inline long
adjust_register_position(struct strscanner *p, long position)
{
if (p->fixed_anchor_p) {
return position;
}
else {
return p->prev + position;
}
}
/* rb_reg_onig_match is available in Ruby 3.3 and later. */
#ifndef HAVE_RB_REG_ONIG_MATCH
static OnigPosition
rb_reg_onig_match(VALUE re, VALUE str,
OnigPosition (*match)(regex_t *reg, VALUE str, struct re_registers *regs, void *args),
void *args, struct re_registers *regs)
{
OnigPosition result;
regex_t *reg = rb_reg_prepare_re(re, str);
bool tmpreg = reg != RREGEXP_PTR(re);
if (!tmpreg) RREGEXP(re)->usecnt++;
result = match(reg, str, regs, args);
if (!tmpreg) RREGEXP(re)->usecnt--;
if (tmpreg) {
if (RREGEXP(re)->usecnt) {
onig_free(reg);
}
else {
onig_free(RREGEXP_PTR(re));
RREGEXP_PTR(re) = reg;
}
}
if (result < 0) {
if (result != ONIG_MISMATCH) {
rb_raise(ScanError, "regexp buffer overflow");
}
}
return result;
}
#endif
static OnigPosition
strscan_match(regex_t *reg, VALUE str, struct re_registers *regs, void *args_ptr)
{
struct strscanner *p = (struct strscanner *)args_ptr;
return onig_match(reg,
match_target(p),
(UChar* )(CURPTR(p) + S_RESTLEN(p)),
(UChar* )CURPTR(p),
regs,
ONIG_OPTION_NONE);
}
static OnigPosition
strscan_search(regex_t *reg, VALUE str, struct re_registers *regs, void *args_ptr)
{
struct strscanner *p = (struct strscanner *)args_ptr;
return onig_search(reg,
match_target(p),
(UChar *)(CURPTR(p) + S_RESTLEN(p)),
(UChar *)CURPTR(p),
(UChar *)(CURPTR(p) + S_RESTLEN(p)),
regs,
ONIG_OPTION_NONE);
}
static void
strscan_enc_check(VALUE str1, VALUE str2)
{
if (RB_ENCODING_GET(str1) != RB_ENCODING_GET(str2)) {
rb_enc_check(str1, str2);
}
}
static VALUE
strscan_do_scan(VALUE self, VALUE pattern, int succptr, int getstr, int headonly)
{
struct strscanner *p;
GET_SCANNER(self, p);
CLEAR_MATCH_STATUS(p);
if (S_RESTLEN(p) < 0) {
return Qnil;
}
if (RB_TYPE_P(pattern, T_REGEXP)) {
OnigPosition ret;
p->regex = pattern;
ret = rb_reg_onig_match(p->regex,
p->str,
headonly ? strscan_match : strscan_search,
(void *)p,
&(p->regs));
if (ret == ONIG_MISMATCH) {
return Qnil;
}
}
else {
StringValue(pattern);
if (S_RESTLEN(p) < RSTRING_LEN(pattern)) {
strscan_enc_check(p->str, pattern);
return Qnil;
}
if (headonly) {
strscan_enc_check(p->str, pattern);
if (memcmp(CURPTR(p), RSTRING_PTR(pattern), RSTRING_LEN(pattern)) != 0) {
return Qnil;
}
set_registers(p, 0, RSTRING_LEN(pattern));
}
else {
rb_encoding *enc = rb_enc_check(p->str, pattern);
long pos = rb_memsearch(RSTRING_PTR(pattern), RSTRING_LEN(pattern),
CURPTR(p), S_RESTLEN(p), enc);
if (pos == -1) {
return Qnil;
}
set_registers(p, pos, RSTRING_LEN(pattern));
}
}
MATCHED(p);
p->prev = p->curr;
if (succptr) {
succ(p);
}
{
const long length = last_match_length(p);
if (getstr) {
return extract_beg_len(p, p->prev, length);
}
else {
return INT2FIX(length);
}
}
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
* :include: strscan/methods/scan.md
*/
static VALUE
strscan_scan(VALUE self, VALUE re)
{
return strscan_do_scan(self, re, 1, 1, 1);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* match?(pattern) -> updated_position or nil
*
* Attempts to [match][17] the given `pattern`
* at the beginning of the [target substring][3];
* does not modify the [positions][11].
*
* If the match succeeds:
*
* - Sets [match values][9].
* - Returns the size in bytes of the matched substring.
*
*
* ```rb
* scanner = StringScanner.new('foobarbaz')
* scanner.pos = 3
* scanner.match?(/bar/) => 3
* put_match_values(scanner)
* # Basic match values:
* # matched?: true
* # matched_size: 3
* # pre_match: "foo"
* # matched : "bar"
* # post_match: "baz"
* # Captured match values:
* # size: 1
* # captures: []
* # named_captures: {}
* # values_at: ["bar", nil]
* # []:
* # [0]: "bar"
* # [1]: nil
* put_situation(scanner)
* # Situation:
* # pos: 3
* # charpos: 3
* # rest: "barbaz"
* # rest_size: 6
* ```
*
* If the match fails:
*
* - Clears match values.
* - Returns `nil`.
* - Does not increment positions.
*
* ```rb
* scanner.match?(/nope/) # => nil
* match_values_cleared?(scanner) # => true
* ```
*
*/
static VALUE
strscan_match_p(VALUE self, VALUE re)
{
return strscan_do_scan(self, re, 0, 0, 1);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
* :include: strscan/methods/skip.md
*/
static VALUE
strscan_skip(VALUE self, VALUE re)
{
return strscan_do_scan(self, re, 1, 0, 1);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* check(pattern) -> matched_substring or nil
*
* Attempts to [match][17] the given `pattern`
* at the beginning of the [target substring][3];
* does not modify the [positions][11].
*
* If the match succeeds:
*
* - Returns the matched substring.
* - Sets all [match values][9].
*
* ```rb
* scanner = StringScanner.new('foobarbaz')
* scanner.pos = 3
* scanner.check('bar') # => "bar"
* put_match_values(scanner)
* # Basic match values:
* # matched?: true
* # matched_size: 3
* # pre_match: "foo"
* # matched : "bar"
* # post_match: "baz"
* # Captured match values:
* # size: 1
* # captures: []
* # named_captures: {}
* # values_at: ["bar", nil]
* # []:
* # [0]: "bar"
* # [1]: nil
* # => 0..1
* put_situation(scanner)
* # Situation:
* # pos: 3
* # charpos: 3
* # rest: "barbaz"
* # rest_size: 6
* ```
*
* If the match fails:
*
* - Returns `nil`.
* - Clears all [match values][9].
*
* ```rb
* scanner.check(/nope/) # => nil
* match_values_cleared?(scanner) # => true
* ```
*
*/
static VALUE
strscan_check(VALUE self, VALUE re)
{
return strscan_do_scan(self, re, 0, 1, 1);
}
/*
* call-seq:
* scan_full(pattern, advance_pointer_p, return_string_p) -> matched_substring or nil
*
* Equivalent to one of the following:
*
* - +advance_pointer_p+ +true+:
*
* - +return_string_p+ +true+: StringScanner#scan(pattern).
* - +return_string_p+ +false+: StringScanner#skip(pattern).
*
* - +advance_pointer_p+ +false+:
*
* - +return_string_p+ +true+: StringScanner#check(pattern).
* - +return_string_p+ +false+: StringScanner#match?(pattern).
*
*/
/* :nodoc: */
static VALUE
strscan_scan_full(VALUE self, VALUE re, VALUE s, VALUE f)
{
return strscan_do_scan(self, re, RTEST(s), RTEST(f), 1);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
* :include: strscan/methods/scan_until.md
*/
static VALUE
strscan_scan_until(VALUE self, VALUE re)
{
return strscan_do_scan(self, re, 1, 1, 0);
}
/*
* :markup: markdown
* :include: strscan/link_refs.txt
*
* call-seq:
* exist?(pattern) -> byte_offset or nil
*
* Attempts to [match][17] the given `pattern`
* anywhere (at any [position][2])
* n the [target substring][3];
* does not modify the [positions][11].
*
* If the match succeeds:
*
* - Returns a byte offset:
* the distance in bytes between the current [position][2]
* and the end of the matched substring.
* - Sets all [match values][9].
*
* ```rb
* scanner = StringScanner.new('foobarbazbatbam')
* scanner.pos = 6
* scanner.exist?(/bat/) # => 6
* put_match_values(scanner)
* # Basic match values:
* # matched?: true
* # matched_size: 3
* # pre_match: "foobarbaz"
* # matched : "bat"
* # post_match: "bam"
* # Captured match values:
* # size: 1
* # captures: []
* # named_captures: {}
* # values_at: ["bat", nil]
* # []:
* # [0]: "bat"
* # [1]: nil
* put_situation(scanner)
* # Situation:
* # pos: 6
* # charpos: 6
* # rest: "bazbatbam"
* # rest_size: 9
* ```
*
* If the match fails:
*
* - Returns `nil`.
* - Clears all [match values][9].
*