Project

General

Profile

Actions

Bug #5351

closed

String#start_with? resets pattern matching results

Added by ivanku (Ivan Kuznetsov) over 12 years ago. Updated over 12 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2011.03
[ruby-core:39671]

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

Actions

Also available in: Atom PDF

Like0
Like0Like0