[3683] MacRuby/branches/icu/re.cpp
Revision: 3683 http://trac.macosforge.org/projects/ruby/changeset/3683 Author: lsansonetti@apple.com Date: 2010-03-03 12:37:56 -0800 (Wed, 03 Mar 2010) Log Message: ----------- added #last_match Modified Paths: -------------- MacRuby/branches/icu/re.cpp Modified: MacRuby/branches/icu/re.cpp =================================================================== --- MacRuby/branches/icu/re.cpp 2010-03-03 20:31:56 UTC (rev 3682) +++ MacRuby/branches/icu/re.cpp 2010-03-03 20:37:56 UTC (rev 3683) @@ -265,6 +265,48 @@ /* * call-seq: + * Regexp.last_match => matchdata + * Regexp.last_match(n) => str + * + * The first form returns the <code>MatchData</code> object generated by the + * last successful pattern match. Equivalent to reading the global variable + * <code>$~</code>. The second form returns the <i>n</i>th field in this + * <code>MatchData</code> object. + * <em>n</em> can be a string or symbol to reference a named capture. + * + * /c(.)t/ =~ 'cat' #=> 0 + * Regexp.last_match #=> #<MatchData "cat" 1:"a"> + * Regexp.last_match(0) #=> "cat" + * Regexp.last_match(1) #=> "a" + * Regexp.last_match(2) #=> nil + * + * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "var = val" + * Regexp.last_match #=> #<MatchData "var = val" lhs:"var" rhs:"val"> + * Regexp.last_match(:lhs) #=> "var" + * Regexp.last_match(:rhs) #=> "val" + */ + +static VALUE match_getter(void); +static int match_backref_number(VALUE match, VALUE backref, bool check); + +static VALUE +regexp_last_match(VALUE klass, SEL sel, int argc, VALUE *argv) +{ + VALUE nth; + + if (argc > 0 && rb_scan_args(argc, argv, "01", &nth) == 1) { + VALUE match = rb_backref_get(); + if (NIL_P(match)) { + return Qnil; + } + const int n = match_backref_number(match, nth, true); + return rb_reg_nth_match(n, match); + } + return match_getter(); +} + +/* + * call-seq: * Regexp.try_convert(obj) -> re or nil * * Try to convert <i>obj</i> into a Regexp, using to_regexp method. @@ -892,9 +934,9 @@ (void *)regexp_quote, 1); #if 0 rb_objc_define_method(*(VALUE *)rb_cRegexp, "union", rb_reg_s_union_m, -2); +#endif rb_objc_define_method(*(VALUE *)rb_cRegexp, "last_match", - rb_reg_s_last_match, -1); -#endif + (void *)regexp_last_match, -1); rb_objc_define_method(*(VALUE *)rb_cRegexp, "try_convert", (void *)regexp_try_convert, 1);
participants (1)
-
source_changes@macosforge.org