summaryrefslogtreecommitdiff
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
parent0d82c354c205ffcb9749141b36027f6ed9e5333d (diff)
downloadbundler-256c88861beb452d790d74c7677a7e4217bc170b.tar.gz
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
-rw-r--r--NEWS3
-rw-r--r--ext/strscan/strscan.c89
-rw-r--r--test/strscan/test_stringscanner.rb22
3 files changed, 114 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 52f28ffe7f..7c2b4b3ce2 100644
--- a/NEWS
+++ b/NEWS
@@ -216,6 +216,9 @@ with all sufficient information, see the ChangeLog file or Redmine
* StringIO
* StringIO#write accepts multiple arguments
+* StringScanner
+ * Add StringScanner#size, StringScanner#captures, StringScanner#values_at [Feature #836]
+
* WEBrick
* Add Server Name Indication (SNI) support [Feature #13729]
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c
index 75cff3716f..c70b94dd8b 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);
diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb
index 9fe6ad7125..3423f9cfed 100644
--- a/test/strscan/test_stringscanner.rb
+++ b/test/strscan/test_stringscanner.rb
@@ -728,4 +728,26 @@ class TestStringScanner < Test::Unit::TestCase
assert_nil(s[:c])
assert_nil(s["c"])
end
+
+ def test_size
+ s = StringScanner.new("Fri Dec 12 1975 14:39")
+ s.scan(/(\w+) (\w+) (\d+) /)
+ assert_equal(4, s.size)
+ end
+
+ def test_captures
+ s = StringScanner.new("Fri Dec 12 1975 14:39")
+ s.scan(/(\w+) (\w+) (\d+) /)
+ assert_equal(["Fri", "Dec", "12"], s.captures)
+ s.scan(/(\w+) (\w+) (\d+) /)
+ assert_nil(s.captures)
+ end
+
+ def test_values_at
+ s = StringScanner.new("Fri Dec 12 1975 14:39")
+ s.scan(/(\w+) (\w+) (\d+) /)
+ assert_equal(["Fri Dec 12 ", "12", nil, "Dec"], s.values_at(0, -1, 5, 2))
+ s.scan(/(\w+) (\w+) (\d+) /)
+ assert_nil(s.values_at(0, -1, 5, 2))
+ end
end