Bug #1553

String#index Ignores Out-of-Range Offset When Given Regexp Matching the Empty String

Added by runpaint (Run Paint Run Run) almost 3 years ago. Updated about 1 year ago.

[ruby-core:23660]
Status:Closed Start date:06/01/2009
Priority:Normal Due date:
Assignee:- % Done:

100%

Category:core
Target version:1.9.2
ruby -v:ruby 1.9.2dev (2009-05-28 trunk 23601) [i686-linux]

Description

On Ruby 1.9 String#index(Regexp, Fixnum) ignores the Fixnum offset when the Regexp matches the empty string and the offset represents a position outside the string. In this case it returns the position of the last match in the string, which is obviously its final character.

I expect this invocation to return nil because the documentation states "If the second parameter is present, it specifies the position in the string to begin the search." If the position is outside of the string the search shouldn't even begin, and thus must return nil. This expectation is reinforced by a non-empty, non-matching Regexp paired with an out-of-range offset returning nil.

1.8.7 returns nil for this scenario.

  $ ruby -ve 'p "ruby".index(//,7)'
  ruby 1.9.2dev (2009-05-28 trunk 23601) [i686-linux]
  4

  $ ruby1.8 -ve 'p "ruby".index(//,7)'
  ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
  nil

  $ ruby -ve 'p "ruby".index(/y/,7)'
  ruby 1.9.2dev (2009-05-28 trunk 23601) [i686-linux]
  nil

  $ ruby1.8 -ve 'p "ruby".index(/y/,7)'
  ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
  nil

Associated revisions

Revision 24103
Added by naruse (Yui NARUSE) almost 3 years ago

Check pos and strlen on Regexp#index(str, pos) [ruby-core:23660]

History

Updated by yugui (Yuki Sonoda) almost 3 years ago

  • Target version changed from 2.0.0 to 1.9.2

Updated by naruse (Yui NARUSE) almost 3 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100
Applied in changeset r24103.

Also available in: Atom PDF