Project

General

Profile

Actions

Feature #18172

closed

MatchData#sublen to return the length of a substring

Added by nobu (Nobuyoshi Nakada) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:105290]

Description

There are many code taking the length of a substring matched by Regexp.
For instance, in rdoc/markup/attribute_manager.rb:

      attr_updated = attrs.set_attrs($`.length + $1.length + $2.length, $3.length, attr)
      if attr_updated
        $1 + NULL * $2.length + $3 + NULL * $2.length + $4

People often tends to use such code (although the first addition can be simpler as $~.begin(3)), that creates and soon drops substrings, just to take the length.

Therefore, how about the new method to calculate the length, MatchData#sublen?

/(\d+)\W(\w+)/ =~ "1:foo"
$~.sublen(1)    #=> 1
$~.sublen(2)    #=> 3

Updated by matz (Yukihiro Matsumoto) over 3 years ago

In Ruby C source code, we use names like sublen but Ruby methods tend to be fully spelled. So I propose MatchData#match(n) and MatchData#match_length(n).

Matz.

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

  • Status changed from Open to Closed

Applied in changeset git|09d724e6f846b4e53e8571d41ca7d3055d732d9f.


[Feature #18172] Add MatchData#match

The method to return the single matched substring corresponding to
the given argument.

Updated by Hanmac (Hans Mackowiak) over 3 years ago

@nobu (Nobuyoshi Nakada), isn't your MatchData#match the same as MatchData#[] ?

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

Hanmac (Hans Mackowiak) wrote in #note-3:

@nobu (Nobuyoshi Nakada), isn't your MatchData#match the same as MatchData#[] ?

Similar, but #match accepts only single index/name, but not a range or an optional length.

Updated by Hanmac (Hans Mackowiak) over 3 years ago

nobu (Nobuyoshi Nakada) wrote in #note-4:

Hanmac (Hans Mackowiak) wrote in #note-3:

@nobu (Nobuyoshi Nakada), isn't your MatchData#match the same as MatchData#[] ?

Similar, but #match accepts only single index/name, but not a range or an optional length.

i just wonder why not use the functions there like done in the other method?

if (FIXNUM_P(idx)) {
  return rb_reg_nth_match(FIX2INT(idx), match);
}
else {
  int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, idx);
  if (num >= 0) {
     return rb_reg_nth_match(num, match);
  }
  else {
    return match_ary_aref(match, idx, Qnil);
  }
}

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

match_ary_aref may return an Array of substrings when idx is a Range.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0