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> 

을하지만 당신은 인코딩을 해결