不间断的 Zlib space
Zlib with non-breaking space
我 运行 在 ruby.
中用不间断 space 膨胀和收缩字符串时遇到了一个奇怪的问题
具有常规 space 的字符串按预期运行:
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>
但是 ZLib returns 一个 ACSII 编码的字符串:
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
我 运行 在 ruby.
中用不间断 space 膨胀和收缩字符串时遇到了一个奇怪的问题具有常规 space 的字符串按预期运行:
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>
但是 ZLib returns 一个 ACSII 编码的字符串:
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