[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

encoding problem with tr() and hash keys

Do One

2/21/2009 11:26:00 AM

Please help to understand solution to this problem (ruby 1.9.1):

In utf-8 environment I do:

irb(main):121:0> h = {"a" => 1, "\u0101" => 2}
=> {"a"=>1, "ā"=>2}
irb(main):122:0> h.key? "a".tr("z", "\u0101")
=> false <--- wrong!
irb(main):123:0> h.key? "\u0101".tr("z", "\u0101")
=> true

So after I change utf-8 string without extended chars in it with tr(),
where second character set is having extended chars, new string is not
found in hash.

Boths string are same in Marshal encoding:

irb(main):124:0> Marshal.dump "a".tr("\u0101", "\u0101")
=> "\x04\bI\"\x06a\x06:\rencoding\"\nUTF-8"
irb(main):126:0> Marshal.dump "a"
=> "\x04\bI\"\x06a\x06:\rencoding\"\nUTF-8"


Question is how I should code using tr() that new string will be found
in hash?

And I think this is bug in ruby, because it is completely not expected
behavior.
--
Posted via http://www.ruby-....

16 Answers

7stud --

2/21/2009 4:37:00 PM

0

Do One wrote:
> Please help to understand solution to this problem (ruby 1.9.1):
>
> In utf-8 environment I do:
>
> irb(main):121:0> h = {"a" => 1, "\u0101" => 2}
> => {"a"=>1, "ā"=>2}
> irb(main):122:0> h.key? "a".tr("z", "\u0101")
> => false <--- wrong!
>


h = {"a" => 1, "b" => 2}

p "a".tr("z", "\u0101") #"a"

puts h.key?("a".tr("z", "x")) #true

ruby 1.8.2

--
Posted via http://www.ruby-....

7stud --

2/21/2009 4:41:00 PM

0

7stud -- wrote:
> h = {"a" => 1, "b" => 2}
>
> p "a".tr("z", "\u0101") #"a"
>
> puts h.key?("a".tr("z", "x")) #true
>
> ruby 1.8.2

Whoops. Make that:


h = {"a" => 1, "\u0101" => 2}

p "a".tr("z", "\u0101") #=>"a"

puts h.key?("a".tr("z", "\u0101")) #=>true

--
Posted via http://www.ruby-....

Do One

2/22/2009 3:51:00 AM

0

Problem described is under modern ruby 1.9.1 in utf-8 environment.

7stud -- wrote:
>> ruby 1.8.2
>
> Whoops. Make that:

ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]
irb(main):001:0> h = {"a" => 1, "\u0101" => 2}
=> {"a"=>1, "u0101"=>2}

See? It even dont understand unicode escape sequence \uXXXX.


Do One wrote:
> Please help to understand solution to this problem (ruby 1.9.1):
>
> In utf-8 environment I do:
>
> irb(main):121:0> h = {"a" => 1, "\u0101" => 2}
> => {"a"=>1, "ā"=>2}
> irb(main):122:0> h.key? "a".tr("z", "\u0101")
> => false <--- wrong!
> irb(main):123:0> h.key? "\u0101".tr("z", "\u0101")
> => true
>
> So after I change utf-8 string without extended chars in it with tr(),
> where second character set is having extended chars, new string is not
> found in hash.
>
> Boths string are same in Marshal encoding:
>
> irb(main):124:0> Marshal.dump "a".tr("\u0101", "\u0101")
> => "\x04\bI\"\x06a\x06:\rencoding\"\nUTF-8"
> irb(main):126:0> Marshal.dump "a"
> => "\x04\bI\"\x06a\x06:\rencoding\"\nUTF-8"
>
>
> Question is how I should code using tr() that new string will be found
> in hash?
>
> And I think this is bug in ruby, because it is completely not expected
> behavior.

--
Posted via http://www.ruby-....

Brian Candler

2/23/2009 1:12:00 PM

0

Do One wrote:
> Please help to understand solution to this problem (ruby 1.9.1):
>
> In utf-8 environment I do:
>
> irb(main):121:0> h = {"a" => 1, "\u0101" => 2}
> => {"a"=>1, "ā"=>2}
> irb(main):122:0> h.key? "a".tr("z", "\u0101")
> => false <--- wrong!
> irb(main):123:0> h.key? "\u0101".tr("z", "\u0101")
> => true

Perhaps describe your environment in more detail? It works for me:

$ irb19
irb(main):001:0> h = {"a" => 1, "\u0101" => 2}
=> {"a"=>1, "ā"=>2}
irb(main):002:0> h.key?("a")
=> true
irb(main):003:0> h.key?("\u0101")
=> true
irb(main):004:0> h.key?("a".tr("z", "\u0101"))
=> true
irb(main):005:0> h.key? "a".tr("z", "\u0101")
=> true
irb(main):006:0> h.key? "z".tr("z", "\u0101")
=> true
irb(main):007:0>

This is Ubuntu Hardy, ruby 1.9.1 (2008-12-01 revision 20438)
[i686-linux] compiled from source. I think this is 1.9.1-preview2 rather
than 1.9.1-p0.

To eliminate problems with encoding, maybe try writing this as a script
and running it from the command line:

p h = {"a" => 1, "\u0101" => 2}
p h.key?("a")
p h.key?("\u0101")
p h.key?("a".tr("z", "\u0101"))
p h.key? "a".tr("z", "\u0101")
p h.key? "z".tr("z", "\u0101")

ruby19 test.rb
ruby19 -Ku test.rb
ruby19 --encoding UTF-8:UTF-8 test.rb

to see if this makes any difference. On my machine at least, the -K and
--encoding flags are not recognised by irb.
--
Posted via http://www.ruby-....

Brian Candler

2/23/2009 2:01:00 PM

0

Just built 1.9.1p0, and there's no difference here (all 'true'
responses)
--
Posted via http://www.ruby-....

ThoML

2/23/2009 2:43:00 PM

0

> Just built 1.9.1p0, and there's no difference here (all 'true'
> responses)

AFAIK it was fixed here:
http://groups.google.com/group/ruby-core-google/browse_frm/thread/504ea4...

Do One

2/24/2009 6:08:00 AM

0

Yes it was fixed yesterday with two consecutive patches, first one was
not fixing it completely, but before I found how to reproduce a bug it
is got fixed second time. (ruby 1.9.2 svn trunk)

> Perhaps describe your environment in more detail? It works for me:

How to reproduce a bug (to understand its traps) -

1. utf-8 env:

$ ruby -v
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]
$ export LC_CTYPE=en_US.utf-8
$ irb
irb(main):001:0> {"a" => 1}.key? "a".tr("z", "\u0101")
=> false

Reproduced. Without utf-8 env you just don't see it:

$ export LC_CTYPE=en_US
$ irb
irb(main):001:0> {"a" => 1}.key? "a".tr("z", "\u0101")
=> true

2. Even if your env is not utf-8 but your script have "encoding: utf-8"
magic comment then bug will be there:

$ cat a.rb
#encoding: utf-8
p ({"a" => 1}).key?("a".tr("z", "\u0101"))
$ ruby a.rb
false

3. Or you are using -KU switch:

$ ruby -KU -e 'p ({"a" => 1}).key?("a".tr("z", "\u0101"))'
false


I stuck on this by parsing word lists where some words having
diacritical marks, some words getting worked out differently then
others, code was correct and it was just plain crazy.
--
Posted via http://www.ruby-....

Brian Candler

2/24/2009 9:16:00 AM

0

Do One wrote:
> Yes it was fixed yesterday with two consecutive patches, first one was
> not fixing it completely, but before I found how to reproduce a bug it
> is got fixed second time. (ruby 1.9.2 svn trunk)

It looks like this craziness is core behaviour for ruby 1.9,
unfortunately.

Notice that in your script which reproduces the problem, the encodings
of the two strings match. Results shown are for ruby 1.9.1p0 (2009-01-30
revision 21907) [i686-linux]

#encoding: utf-8
a = "a"
b = a.tr("z", "\u0101")
h = {a => 1}
p h.key?(a) #true
p h.key?(b) #false !!

p a #"a"
p b #"a"
p a.encoding #<Encoding:UTF-8>
p b.encoding #<Encoding:UTF-8>

p a == b #true
p a.hash #137519702
p b.hash #137519703 AHA!

So two strings, with identical byte sequences and identical encodings,
calculate different hashes. So there must be some hidden internal state
in the string which affects the calculation of the hash. I presume this
is the flag ENC_CODERANGE_7BIT.

It's hard to test whether this flag has been set correctly, if
String#encoding doesn't show it, so you have to use indirect methods
like String#hash.

But now I think I understand the problem, it's easy to find more
examples of the same brokenness. Here's one:

#encoding: utf-8
a = "a"
b = "aÃ?"
b = b.delete("Ã?")
h = {a => 1}
p h.key?(a) #true
p h.key?(b) #false !!

p a #"a"
p b #"a"
p a.encoding #<Encoding:UTF-8>
p b.encoding #<Encoding:UTF-8>

p a == b #true
p a.hash #-590825394
p b.hash #-590825393


I wonder just how many other string methods are broken in this way? And
how many extension writers are going to set this hidden flag correctly
in their strings, if even the ruby core developers don't always do it?

It looks like this flag is a bad optimisation.

* It needs recalculating every time a string is modified (thus negating
the benefits of the optimisation)

* It introduces hidden state, which affects behaviour but cannot be
directly tested

* If the state is not set correctly *every* time a string is generated
or modified - and this includes in all extension modules - then things
break.

Regards,

Brian.
--
Posted via http://www.ruby-....

Do One

2/24/2009 12:54:00 PM

0

Brian Candler wrote:
> But now I think I understand the problem, it's easy to find more
> examples of the same brokenness. Here's one:
>
> #encoding: utf-8
> a = "a"
> b = "aÃ?"
> b = b.delete("Ã?")
> h = {a => 1}
> p h.key?(a) #true
> p h.key?(b) #false !!

This is still false even in "fixed" 1.9.2dev. Probably you should report
it. :)


> I wonder just how many other string methods are broken in this way? And
> how many extension writers are going to set this hidden flag correctly
> in their strings, if even the ruby core developers don't always do it?

Scary.
--
Posted via http://www.ruby-....

Brian Candler

2/24/2009 1:18:00 PM

0

Do One wrote:
> Brian Candler wrote:
>> But now I think I understand the problem, it's easy to find more
>> examples of the same brokenness. Here's one:
>>
>> #encoding: utf-8
>> a = "a"
>> b = "aÃ?"
>> b = b.delete("Ã?")
>> h = {a => 1}
>> p h.key?(a) #true
>> p h.key?(b) #false !!
>
> This is still false even in "fixed" 1.9.2dev. Probably you should report
> it. :)

It's Not My Problem[TM], because I don't use 1.9 and have no intention
of doing so for the foreseeable future. The semantics of Strings are now
so complex that they are not even documented (except as
reverse-engineered by some third parties for commercial books) - so how
can you complain when they do something you don't expect?

Ruby <=1.8.6 is an old friend. But for me, Ruby >=1.9 is more like a
Rottweiler. I'm sure Rottweilers can make great companions to the right
sort of owners.

B.
--
Posted via http://www.ruby-....