2016-07-06 3 views
2

ルビーの中で壊れていない空間で文字列を膨張させたり収縮させたりするとき、私は奇妙な問題に遭遇しました。通常のスペースで非破壊空間のZlib

文字列が期待どおりに動作:しかし

str = "hello world"; str_zipped = Zlib.deflate str; str == Zlib.inflate(str_zipped) 
=> true 

str = "hello\xA0world"; str_zipped = Zlib.deflate str; str == Zlib.inflate(str_zipped) 
=> false 

は、この期待される動作やバグですか?

答えて

3

ZLibはエンコードを保持しません。あなたの文字列は、おそらくUTF-8でエンコードされ:

str = "hello\xA0world" 
str.encoding 
#=> <Encoding:UTF-8> 

しかし、zlibのはACSIIエンコードされた文字列を返します。

str = "hello\xA0world" 
str_zipped = Zlib.deflate str 
str_utf8 = Zlib.inflate(str_zipped).force_encoding('UTF-8') 
str == str_utf8 
#=> true 

str_zipped = Zlib.deflate str 
str = Zlib.inflate(str_zipped) 
str.encoding 
#=> <Encoding:ASCII-8BIT> 

をしかし、あなたはエンコーディングを修正とき

関連する問題