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.
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
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.
Status changed from Open to Closed
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.
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 );
}
}
match_ary_aref
may return an Array
of substrings when idx
is a Range
.
Also available in: Atom
PDF
Like 0
Like 0 Like 0 Like 0 Like 0 Like 0 Like 0