Bug #5351
closedString#start_with? resets pattern matching results
Description
String#start_with? backported into 1.8.7 (https://github.com/ruby/ruby/commit/7d848354f8e25a652ccaabbd54e106b44e7da308) has an undocumented behaviour - it resets pattern matching results
Example code:
"Hello".sub!(%r(^(.))) {}
puts "$1 after sub! = '#{$1}'"
"Hello".start_with?("H")
puts "$1 after start_with? = '#{$1}'"
Ruby 1.8.7:
$1 after sub! = 'H'
$1 after start_with? = ''
Ruby 1.9.X:
$1 after sub! = 'H'
$1 after start_with? = 'H'
In Ruby 1.8.7 rb_str_start_with for some reason calls rb_reg_search. In 1.9.X solution is more elegant - memcmp is used: https://github.com/ruby/ruby/blob/ruby_1_9_3/string.c#L7170
Updated by ruby412 (Alan Walkings) over 12 years ago
Found the same issue with Ruby Enterprise Edition 2011.04
http://informatique.edublogs.org/
Updated by nobu (Nobuyoshi Nakada) over 12 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r33527.
Ivan, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
- string.c (rb_str_partition, rb_str_rpartition)
(rb_str_start_with, rb_str_end_with): preserve the last match
data. [ruby-core:39671] [Bug #5351]