Bug #4340
closedEncoding of result string for String#gsub is not consistent
Description
=begin
Depending upon where the replacement occurs, the encoding of the result of String#gsub is not consistent.
When the replacement happens at the beginning of the string the encoding of the result is the encoding of the replacement string.
When the replacement happens elsewhere in the string the encoding of the result is the result of the original string.
With String#sub the encoding of the result is the encoding of the original string always.
$ cat t.rb
puts 'using gsub'
hello_world = 'Hello World!'
hello_world.force_encoding Encoding::UTF_8
everybody = 'Everybody'
everybody.force_encoding Encoding::US_ASCII
hello_everybody = hello_world.gsub(/World/, 'Everybody')
p hello_everybody
p hello_everybody.encoding
hi = 'Hi'
hi.force_encoding Encoding::US_ASCII
hi_world = hello_world.gsub(/Hello/, 'Hi')
p hi_world
p hi_world.encoding
puts 'using sub'
hello_world = 'Hello World!'
hello_world.force_encoding Encoding::UTF_8
everybody = 'Everybody'
everybody.force_encoding Encoding::US_ASCII
hello_everybody = hello_world.sub(/World/, 'Everybody')
p hello_everybody
p hello_everybody.encoding
hi = 'Hi'
hi.force_encoding Encoding::US_ASCII
hi_world = hello_world.sub(/Hello/, 'Hi')
p hi_world
p hi_world.encoding
$ ruby19 -v t.rb
ruby 1.9.3dev (2011-01-26 trunk 30659) [x86_64-darwin10.6.0]
using gsub
"Hello Everybody!"
#Encoding:UTF-8
"Hi World!"
#Encoding:US-ASCII
using sub
"Hello Everybody!"
#Encoding:UTF-8
"Hi World!"
#Encoding:UTF-8
=end
Files