Project

General

Profile

Actions

Bug #4340

closed

Encoding of result string for String#gsub is not consistent

Added by drbrain (Eric Hodel) about 13 years ago. Updated almost 13 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.3dev (2011-01-26 trunk 30659) [x86_64-darwin10.6.0]
Backport:
[ruby-core:34959]

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

string.c.gsub.encoding.patch (339 Bytes) string.c.gsub.encoding.patch Set destination string encoding to source string encoding for String#gsub drbrain (Eric Hodel), 02/02/2011 08:43 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0