Project

General

Profile

Feature #836 ยป strscan.diff

apeiros (Stefan Rusterholz), 12/08/2008 11:27 AM

View differences:

strscan.c 2008-12-08 03:18:47.000000000 +0100
963 963
}
964 964

  
965 965
/*
966
 * call-seq: size
967
 *
968
 * Return the amount of subgroups in the most recent match.
969
 * The full match counts as a subgroup.
970
 *
971
 *   s = StringScanner.new("Fri Dec 12 1975 14:39")
972
 *   s.scan(/(\w+) (\w+) (\d+) /)       # -> "Fri Dec 12 "
973
 *   s.size                             # -> 4
974
 */
975
static VALUE
976
strscan_size(VALUE self)
977
{
978
    struct strscanner *p;
979

  
980
    GET_SCANNER(self, p);
981
    if (! MATCHED_P(p))        return Qnil;
982
    return INT2FIX(p->regs.num_regs);
983
}
984

  
985
/*
986
 * call-seq: size
987
 *
988
 * Returns the subgroups in the most recent match (not including the full match).
989
 * If nothing was priorly matched, it returns nil.
990
 *
991
 *   s = StringScanner.new("Fri Dec 12 1975 14:39")
992
 *   s.scan(/(\w+) (\w+) (\d+) /)       # -> "Fri Dec 12 "
993
 *   s.captures                         # -> ["Fri", "Dec", "12"]
994
 *   s.scan(/(\w+) (\w+) (\d+) /)       # -> nil
995
 *   s.captures                         # -> nil
996
 */
997
static VALUE
998
strscan_captures(VALUE self)
999
{
1000
    struct strscanner *p;
1001

  
1002
    GET_SCANNER(self, p);
1003
    if (! MATCHED_P(p))        return Qnil;
1004
    
1005
    int   i;
1006
    int   num_regs = p->regs.num_regs;
1007
    VALUE result   = rb_ary_new2(num_regs);
1008

  
1009
    for(i=1; i<num_regs; i++) {
1010
      rb_ary_push(
1011
        result,
1012
        extract_range(p, p->prev + p->regs.beg[i], p->prev + p->regs.end[i])
1013
      );
1014
    }
1015
    
1016
    return result;
1017
}
1018

  
1019
/*
1020
 *  call-seq:
1021
 *     scanner.values_at( i1, i2, ... iN )   -> an_array
1022
 *  
1023
 * Returns the subgroups in the most recent match at the given indices.
1024
 * If nothing was priorly matched, it returns nil.
1025
 *
1026
 *   s = StringScanner.new("Fri Dec 12 1975 14:39")
1027
 *   s.scan(/(\w+) (\w+) (\d+) /)       # -> "Fri Dec 12 "
1028
 *   s.values_at 0, -1, 5, 2            # -> ["Fri Dec 12 ", "12", nil, "Dec"]
1029
 *   s.scan(/(\w+) (\w+) (\d+) /)       # -> nil
1030
 *   s.captures                         # -> nil
1031
 */
1032

  
1033
static VALUE
1034
strscan_values_at(int argc, VALUE *argv, VALUE self)
1035
{
1036
    struct strscanner *p;
1037

  
1038
    GET_SCANNER(self, p);
1039
    if (! MATCHED_P(p))        return Qnil;
1040

  
1041
    long i;
1042
    VALUE new_ary = rb_ary_new2(argc);
1043

  
1044
    for (i=0; i<argc; i++) {
1045
        rb_ary_push(new_ary, strscan_aref(self, argv[i]));
1046
    }
1047

  
1048
    return new_ary;
1049
}
1050

  
1051
/*
966 1052
 * Return the <i><b>pre</b>-match</i> (in the regular expression sense) of the last scan.
967 1053
 *
968 1054
 *   s = StringScanner.new('test string')
......
1311 1397
    rb_define_method(StringScanner, "[]",          strscan_aref,        1);
1312 1398
    rb_define_method(StringScanner, "pre_match",   strscan_pre_match,   0);
1313 1399
    rb_define_method(StringScanner, "post_match",  strscan_post_match,  0);
1400
    rb_define_method(StringScanner, "size",        strscan_size,        0);
1401
    rb_define_method(StringScanner, "captures",    strscan_captures,    0);
1402
    rb_define_method(StringScanner, "values_at",   strscan_values_at,   -1);
1314 1403

  
1315 1404
    rb_define_method(StringScanner, "rest",        strscan_rest,        0);
1316 1405
    rb_define_method(StringScanner, "rest_size",   strscan_rest_size,   0);