summaryrefslogtreecommitdiff
path: root/ext/strscan/strscan.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-29 07:57:48 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-29 07:57:48 +0000
commit256c88861beb452d790d74c7677a7e4217bc170b (patch)
tree8b992570061fc56456a48a6a8208e2f42414ea8f /ext/strscan/strscan.c
parent0d82c354c205ffcb9749141b36027f6ed9e5333d (diff)
strscan.c: add MatchData-like methods
* ext/strscan/strscan.c: added `size`, `captures` and `values_at` to StringScanner, shorthands of accessing the matched data. based on the patch by apeiros (Stefan Rusterholz) at [ruby-core:20412]. [Feature #836] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/strscan/strscan.c')
-rw-r--r--ext/strscan/strscan.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c
index 75cff37..c70b94d 100644
--- a/ext/strscan/strscan.c
+++ b/ext/strscan/strscan.c
@@ -1053,6 +1053,92 @@ strscan_aref(VALUE self, VALUE idx)
}
/*
+ * call-seq: size
+ *
+ * Return the amount of subgroups in the most recent match.
+ * The full match counts as a subgroup.
+ *
+ * s = StringScanner.new("Fri Dec 12 1975 14:39")
+ * s.scan(/(\w+) (\w+) (\d+) /) # -> "Fri Dec 12 "
+ * s.size # -> 4
+ */
+static VALUE
+strscan_size(VALUE self)
+{
+ struct strscanner *p;
+
+ GET_SCANNER(self, p);
+ if (! MATCHED_P(p)) return Qnil;
+ return INT2FIX(p->regs.num_regs);
+}
+
+/*
+ * call-seq: captures
+ *
+ * Returns the subgroups in the most recent match (not including the full match).
+ * If nothing was priorly matched, it returns nil.
+ *
+ * s = StringScanner.new("Fri Dec 12 1975 14:39")
+ * s.scan(/(\w+) (\w+) (\d+) /) # -> "Fri Dec 12 "
+ * s.captures # -> ["Fri", "Dec", "12"]
+ * s.scan(/(\w+) (\w+) (\d+) /) # -> nil
+ * s.captures # -> nil
+ */
+static VALUE
+strscan_captures(VALUE self)
+{
+ struct strscanner *p;
+ int i, num_regs;
+ VALUE new_ary;
+
+ GET_SCANNER(self, p);
+ if (! MATCHED_P(p)) return Qnil;
+
+ num_regs = p->regs.num_regs;
+ new_ary = rb_ary_new2(num_regs);
+
+ for (i = 1; i < num_regs; i++) {
+ VALUE str = extract_range(p, p->prev + p->regs.beg[i],
+ p->prev + p->regs.end[i]);
+ rb_ary_push(new_ary, str);
+ }
+
+ return new_ary;
+}
+
+/*
+ * call-seq:
+ * scanner.values_at( i1, i2, ... iN ) -> an_array
+ *
+ * Returns the subgroups in the most recent match at the given indices.
+ * If nothing was priorly matched, it returns nil.
+ *
+ * s = StringScanner.new("Fri Dec 12 1975 14:39")
+ * s.scan(/(\w+) (\w+) (\d+) /) # -> "Fri Dec 12 "
+ * s.values_at 0, -1, 5, 2 # -> ["Fri Dec 12 ", "12", nil, "Dec"]
+ * s.scan(/(\w+) (\w+) (\d+) /) # -> nil
+ * s.values_at 0, -1, 5, 2 # -> nil
+ */
+
+static VALUE
+strscan_values_at(int argc, VALUE *argv, VALUE self)
+{
+ struct strscanner *p;
+ long i;
+ VALUE new_ary;
+
+ GET_SCANNER(self, p);
+ if (! MATCHED_P(p)) return Qnil;
+
+ new_ary = rb_ary_new2(argc);
+ for (i = 0; i<argc; i++) {
+ rb_ary_push(new_ary, strscan_aref(self, argv[i]));
+ }
+
+ return new_ary;
+}
+
+/*
* Return the <i><b>pre</b>-match</i> (in the regular expression sense) of the last scan.
*
* s = StringScanner.new('test string')
@@ -1392,6 +1478,9 @@ Init_strscan(void)
rb_define_method(StringScanner, "[]", strscan_aref, 1);
rb_define_method(StringScanner, "pre_match", strscan_pre_match, 0);
rb_define_method(StringScanner, "post_match", strscan_post_match, 0);
+ rb_define_method(StringScanner, "size", strscan_size, 0);
+ rb_define_method(StringScanner, "captures", strscan_captures, 0);
+ rb_define_method(StringScanner, "values_at", strscan_values_at, -1);
rb_define_method(StringScanner, "rest", strscan_rest, 0);
rb_define_method(StringScanner, "rest_size", strscan_rest_size, 0);