Feature #836

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

Added by Stefan Rusterholz over 5 years ago. Updated over 1 year ago.

[ruby-core:20412]
Status:Assigned
Priority:Normal
Assignee:Nobuyoshi Nakada
Category:lib
Target version:next minor

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 5 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;
    +

  • numregs = p->regs.numregs;

  • newary = rbarynew2(numregs);
    +

  • 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.valuesat( i1, i2, ... iN ) -> anarray
  • *

    • 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
    +strscanvaluesat(int argc, VALUE *argv, VALUE self)
    +{

  • struct strscanner *p;

  • long i;

  • VALUE new_ary;
    +

  • GET_SCANNER(self, p);

  • if (! MATCHED_P(p)) return Qnil;
    +

  • newary = rbary_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 @@ Initstrscan(void) rbdefinemethod(StringScanner, "prematch", strscanprematch, 0); rbdefinemethod(StringScanner, "postmatch", strscanpost_match, 0);
  • rbdefinemethod(StringScanner, "size", strscan_size, 0);

  • rbdefinemethod(StringScanner, "captures", strscan_captures, 0);

  • rbdefinemethod(StringScanner, "valuesat", strscanvalues_at, -1);

    rbdefinemethod(StringScanner, "rest", strscan_rest, 0);

    Nobu Nakada

=end

#2 Updated by Stefan Rusterholz over 5 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 about 5 years ago

  • Assignee set to Nobuyoshi Nakada

=begin

=end

#4 Updated by Kazuhiro NISHIYAMA about 4 years ago

  • Target version set to 2.0.0

=begin

=end

#5 Updated by Usaku NAKAMURA almost 4 years ago

  • Status changed from Open to Assigned

=begin

=end

#6 Updated by Eric Hodel over 2 years ago

  • Category set to lib

#7 Updated by Yusuke Endoh about 2 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 over 1 year ago

Seems fine to me.

#9 Updated by Yusuke Endoh over 1 year ago

  • Target version changed from 2.0.0 to next minor

Also available in: Atom PDF