~ubuntu-branches/ubuntu/dapper/ruby1.9/dapper

« back to all changes in this revision

Viewing changes to ext/strscan/strscan.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2006-05-08 22:23:12 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060508222312-w2wqeaz030ifi59j
Tags: 1.9.0+20060423-3ubuntu1
* Resynchronized with Debian.
* Only change from Debian is the addition of
  debian/patches/903_sparc_fix_define.patch to fix illegal instructions
  at runtime on sparc. (change from 1.9.0+20050921-1ubuntu1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 
3
 
    strscan.c
4
 
 
5
 
    Copyright (c) 1999-2004 Minero Aoki
 
2
    $Id: strscan.c,v 1.22 2005/10/06 11:11:23 aamine Exp $
 
3
 
 
4
    Copyright (c) 1999-2005 Minero Aoki
6
5
 
7
6
    This program is free software.
8
7
    You can distribute/modify this program under the terms of
9
8
    the Ruby License. For details, see the file COPYING.
10
 
 
11
 
    $Id: strscan.c,v 1.20 2005/03/04 10:40:09 nobu Exp $
12
 
 
13
9
*/
14
10
 
15
 
 
16
11
#include "ruby.h"
17
12
#include "re.h"
18
13
 
154
149
    return infect(rb_str_new(S_PBEG(p) + beg_i, len), p);
155
150
}
156
151
 
157
 
 
158
152
/* =======================================================================
159
153
                               Constructor
160
154
   ======================================================================= */
161
155
 
162
 
 
163
156
static void
164
157
strscan_mark(p)
165
158
    struct strscanner *p;
172
165
    struct strscanner *p;
173
166
{
174
167
    onig_region_free(&(p->regs), 0);
175
 
    memset(p, sizeof(struct strscanner), 0);
176
168
    free(p);
177
169
}
178
170
 
251
243
    return vself;
252
244
}
253
245
 
254
 
 
255
246
/* =======================================================================
256
247
                          Instance Methods
257
248
   ======================================================================= */
379
370
 * value is zero.  In the 'terminated' position (i.e. the string is exhausted),
380
371
 * this value is the length of the string.
381
372
 *
382
 
 * In short, it's a 1-based index into the string.
 
373
 * In short, it's a 0-based index into the string.
383
374
 *
384
375
 *   s = StringScanner.new('test string')
385
376
 *   s.pos               # -> 0
423
414
    return INT2NUM(i);
424
415
}
425
416
 
426
 
 
427
417
/* I should implement this function? */
428
418
#define strscan_prepare_re(re)  /* none */
429
419
 
475
465
}
476
466
 
477
467
/*
478
 
 * call-seq:
479
 
 *   scanner.scan(pattern) => String
480
 
 *
481
 
 */
482
 
 
483
 
/*
484
 
 * call-seq: scan(pattern)
 
468
 * call-seq: scan(pattern) => String
485
469
 *
486
470
 * Tries to match with +pattern+ at the current position. If there's a match,
487
471
 * the scanner advances the "scan pointer" and returns the matched string.
583
567
    return strscan_do_scan(self, re, RTEST(s), RTEST(f), 1);
584
568
}
585
569
 
586
 
 
587
570
/*
588
571
 * call-seq: scan_until(pattern)
589
572
 *
692
675
 
693
676
/*
694
677
 * Scans one character and returns it.
 
678
 * This method is multibyte character sensitive.
695
679
 *
696
 
 *   s = StringScanner.new('ab')
 
680
 *   s = StringScanner.new("ab")
697
681
 *   s.getch           # => "a"
698
682
 *   s.getch           # => "b"
699
683
 *   s.getch           # => nil
 
684
 *
 
685
 *   $KCODE = 'EUC'
 
686
 *   s = StringScanner.new("\244\242")
 
687
 *   s.getch           # => "\244\242"   # Japanese hira-kana "A" in EUC-JP
 
688
 *   s.getch           # => nil
700
689
 */
701
690
static VALUE
702
691
strscan_getch(self)
711
700
        return Qnil;
712
701
 
713
702
    len = mbclen(*CURPTR(p));
714
 
    if (p->curr + len > S_LEN(p))
 
703
    if (p->curr + len > S_LEN(p)) {
715
704
        len = S_LEN(p) - p->curr;
 
705
    }
716
706
    p->prev = p->curr;
717
707
    p->curr += len;
718
708
    MATCHED(p);
722
712
}
723
713
 
724
714
/*
725
 
 * Scans one byte and returns it.  Similar to, but not the same as, #getch.
726
 
 *
727
 
 *  s = StringScanner.new('ab')
728
 
 *  s.get_byte         # => "a"
729
 
 *  s.get_byte         # => "b"
730
 
 *  s.get_byte         # => nil
 
715
 * Scans one byte and returns it.
 
716
 * This method is not multibyte character sensitive.
 
717
 * See also: #getch.
 
718
 *
 
719
 *   s = StringScanner.new('ab')
 
720
 *   s.get_byte         # => "a"
 
721
 *   s.get_byte         # => "b"
 
722
 *   s.get_byte         # => nil
 
723
 *
 
724
 *   $KCODE = 'EUC'
 
725
 *   s = StringScanner.new("\244\242")
 
726
 *   s.get_byte         # => "\244"
 
727
 *   s.get_byte         # => "\242"
 
728
 *   s.get_byte         # => nil
731
729
 */
732
730
static VALUE
733
731
strscan_get_byte(self)
810
808
 *   s.unscan
811
809
 *   s.scan(/../)         # => "te"
812
810
 *   s.scan(/\d/)         # => nil
813
 
 *   s.unscan             # ScanError: can't unscan: prev match had failed
 
811
 *   s.unscan             # ScanError: unscan failed: previous match record not exist
814
812
 */
815
813
static VALUE
816
814
strscan_unscan(self)
820
818
 
821
819
    GET_SCANNER(self, p);
822
820
    if (! MATCHED_P(p))
823
 
        rb_raise(ScanError, "can't unscan: prev match had failed");
824
 
 
 
821
        rb_raise(ScanError, "unscan failed: previous match record not exist");
825
822
    p->curr = p->prev;
826
823
    CLEAR_MATCH_STATUS(p);
827
824
    return self;
868
865
    struct strscanner *p;
869
866
 
870
867
    GET_SCANNER(self, p);
871
 
    if (EOS_P(p))
872
 
        return Qtrue;
873
 
    else
874
 
        return Qfalse;
 
868
    return EOS_P(p) ? Qtrue : Qfalse;
875
869
}
876
870
 
877
871
/*
901
895
    struct strscanner *p;
902
896
 
903
897
    GET_SCANNER(self, p);
904
 
    if (EOS_P(p))
905
 
        return Qfalse;
906
 
    else
907
 
        return Qtrue;
 
898
    return EOS_P(p) ? Qfalse : Qtrue;
908
899
}
909
900
 
910
901
/*
923
914
    struct strscanner *p;
924
915
 
925
916
    GET_SCANNER(self, p);
926
 
    if (MATCHED_P(p))
927
 
        return Qtrue;
928
 
    else
929
 
        return Qfalse;
 
917
    return MATCHED_P(p) ? Qtrue : Qfalse;
930
918
}
931
919
 
932
920
/*
944
932
 
945
933
    GET_SCANNER(self, p);
946
934
    if (! MATCHED_P(p)) return Qnil;
947
 
 
948
935
    return extract_range(p, p->prev + p->regs.beg[0],
949
936
                            p->prev + p->regs.end[0]);
950
937
}
967
954
 
968
955
    GET_SCANNER(self, p);
969
956
    if (! MATCHED_P(p)) return Qnil;
970
 
 
971
957
    return INT2NUM(p->regs.end[0] - p->regs.beg[0]);
972
958
}
973
959
 
1035
1021
 
1036
1022
    GET_SCANNER(self, p);
1037
1023
    if (! MATCHED_P(p)) return Qnil;
1038
 
 
1039
1024
    return extract_range(p, 0, p->prev + p->regs.beg[0]);
1040
1025
}
1041
1026
 
1056
1041
 
1057
1042
    GET_SCANNER(self, p);
1058
1043
    if (! MATCHED_P(p)) return Qnil;
1059
 
 
1060
1044
    return extract_range(p, p->prev + p->regs.end[0], S_LEN(p));
1061
1045
}
1062
1046
 
1091
1075
    if (EOS_P(p)) {
1092
1076
        return INT2FIX(0);
1093
1077
    }
1094
 
 
1095
1078
    i = S_LEN(p) - p->curr;
1096
1079
    return INT2FIX(i);
1097
1080
}
1261
1244
 * === Advancing the Scan Pointer
1262
1245
 *
1263
1246
 * - #getch
1264
 
 * - #getbyte
 
1247
 * - #get_byte
1265
1248
 * - #scan
1266
1249
 * - #scan_until
1267
1250
 * - #skip
1277
1260
 *
1278
1261
 * === Finding Where we Are
1279
1262
 *
1280
 
 * - #bol?
 
1263
 * - #beginning_of_line? (#bol?)
1281
1264
 * - #eos?
1282
1265
 * - #rest?
1283
1266
 * - #rest_size
1322
1305
    tmp = rb_str_new2(STRSCAN_VERSION);
1323
1306
    rb_obj_freeze(tmp);
1324
1307
    rb_const_set(StringScanner, rb_intern("Version"), tmp);
1325
 
    tmp = rb_str_new2("$Id: strscan.c,v 1.20 2005/03/04 10:40:09 nobu Exp $");
 
1308
    tmp = rb_str_new2("$Id: strscan.c,v 1.22 2005/10/06 11:11:23 aamine Exp $");
1326
1309
    rb_obj_freeze(tmp);
1327
1310
    rb_const_set(StringScanner, rb_intern("Id"), tmp);
1328
1311
    
1363
1346
    rb_define_method(StringScanner, "unscan",      strscan_unscan,      0);
1364
1347
 
1365
1348
    rb_define_method(StringScanner, "beginning_of_line?", strscan_bol_p, 0);
1366
 
    rb_define_method(StringScanner, "bol?",        strscan_bol_p,       0);
 
1349
    rb_alias(StringScanner, rb_intern("bol?"), rb_intern("beginning_of_line?"));
1367
1350
    rb_define_method(StringScanner, "eos?",        strscan_eos_p,       0);
1368
1351
    rb_define_method(StringScanner, "empty?",      strscan_empty_p,     0);
1369
1352
    rb_define_method(StringScanner, "rest?",       strscan_rest_p,      0);