Feature #836

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

Added by Stefan Rusterholz over 6 years ago. Updated almost 3 years ago.

[ruby-core:20412]
Status:Assigned
Priority:Normal
Assignee:Nobuyoshi Nakada

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

strscan.c Magnifier (38.2 KB) Stefan Rusterholz, 12/08/2008 11:27 AM

strscan.diff Magnifier (3.12 KB) Stefan Rusterholz, 12/08/2008 11:27 AM

History

#1 Updated by Nobuyoshi Nakada over 6 years ago

=begin
Hi,

At Mon, 8 Dec 2008 11:20:55 +0900,
Stefan Rusterholz wrote in :

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 pre-match (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

=end

#2 Updated by Stefan Rusterholz over 6 years ago

=begin
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.

=end

#3 Updated by Shyouhei Urabe over 6 years ago

  • Assignee set to Nobuyoshi Nakada

=begin

=end

#4 Updated by Kazuhiro NISHIYAMA over 5 years ago

  • Target version set to 2.0.0

=begin

=end

#5 Updated by Usaku NAKAMURA over 5 years ago

  • Status changed from Open to Assigned

=begin

=end

#6 Updated by Eric Hodel about 4 years ago

  • Category set to lib

#7 Updated by Yusuke Endoh over 3 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 mame@tsg.ne.jp

#8 Updated by Nobuyoshi Nakada almost 3 years ago

Seems fine to me.

#9 Updated by Yusuke Endoh almost 3 years ago

  • Target version changed from 2.0.0 to next minor

Also available in: Atom PDF