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

Updated by ruby412 (Alan Walkings) over 12 years ago

Found the same issue with Ruby Enterprise Edition 2011.04
http://informatique.edublogs.org/

Actions #2

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]
Actions

Also available in: Atom PDF

Like0
Like0Like0