【问题标题】:Zlib with non-breaking space具有不间断空格的 Zlib
【发布时间】:2016-07-06 14:21:49
【问题描述】:

在 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)
=> false

这是预期的行为还是错误?

【问题讨论】:

    标签: ruby whitespace zlib


    【解决方案1】:

    ZLib 不保留编码。您的字符串可能是 UTF-8 编码的:

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

    但是 ZLib 返回一个 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
    

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 2013-01-14
      • 1970-01-01
      相关资源
      最近更新 更多