Project

General

Profile

Actions

Feature #836

closed

Patches for StringScanner, adding #size, #captures and #values_at

Added by apeiros (Stefan Rusterholz) over 15 years ago. Updated over 6 years ago.

Status:
Closed
Target version:
[ruby-core:20412]

Description

=begin
The methods are named to be consistent with MatchData.
My C-fu isn't very strong, so a review of the patch would be appreciated.
I attached the revised code and a diff file. If other formats are wished, please tell.
If I need to submit something else with that patch, please tell.

Regards
Stefan
=end


Files

strscan.c (38.2 KB) strscan.c apeiros (Stefan Rusterholz), 12/08/2008 11:27 AM
strscan.diff (3.12 KB) strscan.diff apeiros (Stefan Rusterholz), 12/08/2008 11:27 AM
Actions #1

Updated by nobu (Nobuyoshi Nakada) over 15 years ago

Hi,

At Mon, 8 Dec 2008 11:20:55 +0900,
Stefan Rusterholz wrote in [ruby-core:20412]:

The methods are named to be consistent with MatchData.
My C-fu isn't very strong, so a review of the patch would be appreciated.
I attached the revised code and a diff file. If other formats are wished, please tell.
If I need to submit something else with that patch, please tell.

  • Variable declaration is disallowed after executing statements.
  • call-seq: for captures was `size'.
Index: ext/strscan/strscan.c
===================================================================
--- ext/strscan/strscan.c	(revision 20570)
+++ ext/strscan/strscan.c	(working copy)
@@ -964,4 +964,90 @@ 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.captures                         # -> 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.
  *
@@ -1312,4 +1398,7 @@ Init_strscan(void)
     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);

--
Nobu Nakada

Actions #2

Updated by apeiros (Stefan Rusterholz) over 15 years ago

Hi Nobu

Kind thanks for taking a look at my patch.

On 08.12.2008 at 18:27 Nobuyoshi Nakada wrote:
| Variable declaration is disallowed after executing statements.

Ah, good to know. I hope I will remember next time!
Can I setup extconfig.rb so that gcc will warn me if I did that?

| call-seq: for captures was `size'.

Whoops. "Too close" syndrome I guess :) Thanks for pointing that out.

Actions #3

Updated by shyouhei (Shyouhei Urabe) about 15 years ago

  • Assignee set to nobu (Nobuyoshi Nakada)
Actions #4

Updated by znz (Kazuhiro NISHIYAMA) about 14 years ago

  • Target version set to 2.0.0
Actions #5

Updated by usa (Usaku NAKAMURA) almost 14 years ago

  • Status changed from Open to Assigned

Updated by drbrain (Eric Hodel) over 12 years ago

  • Category set to lib

Updated by mame (Yusuke Endoh) about 12 years ago

Nobu, do you think that this proposal can be accepted?
There is no maintainer for strscan; I leave this decision
to your discretion.

--
Yusuke Endoh

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

Seems fine to me.

Updated by mame (Yusuke Endoh) over 11 years ago

  • Target version changed from 2.0.0 to 2.6
Actions #10

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r60929.


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]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0