Ruby 具有不间断空间的Zlib
在ruby中,当使用不间断空格对字符串进行充气和放气时,我遇到了一个奇怪的问题 带有规则空格的字符串的行为符合预期:Ruby 具有不间断空间的Zlib,ruby,whitespace,zlib,Ruby,Whitespace,Zlib,在ruby中,当使用不间断空格对字符串进行充气和放气时,我遇到了一个奇怪的问题 带有规则空格的字符串的行为符合预期: 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) =>
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
这是预期的行为还是错误?ZLib没有保留编码。您的字符串可能是UTF-8编码的:
str = "hello\xA0world"
str.encoding
#=> <Encoding:UTF-8>
str_zipped = Zlib.deflate str
str = Zlib.inflate(str_zipped)
str.encoding
#=> <Encoding:ASCII-8BIT>
str = "hello\xA0world"
str_zipped = Zlib.deflate str
str_utf8 = Zlib.inflate(str_zipped).force_encoding('UTF-8')
str == str_utf8
#=> true