【问题标题】:sbcl encoding error only when executed from prompt?仅在从提示符执行时出现 sbcl 编码错误?
【发布时间】:2013-07-15 23:00:27
【问题描述】:

我有一个代码,如果从 emacs 中的 slime 提示符执行,则运行时没有错误。如果我从提示符启动 sbcl,则会收到错误消息:

* (ei:proc-file "BRAvESP000.log" "lixo")

debugger invoked on a SB-INT:STREAM-ENCODING-ERROR:
  :UTF-8 stream encoding error on
  #<SB-SYS:FD-STREAM for "file /Users/arademaker/work/IBM/scolapp/lixo"
    {10049E8FF3}>:

    the character with code 55357 cannot be encoded.

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [OUTPUT-NOTHING    ] Skip output of this character.
  1: [OUTPUT-REPLACEMENT] Output replacement string.
  2: [ABORT             ] Exit debugger, returning to top level.

(SB-IMPL::STREAM-ENCODING-ERROR-AND-HANDLE #<SB-SYS:FD-STREAM for "file /Users/arademaker/work/IBM/scolapp/lixo" {10049E8FF3}> 55357)
0]

问题在于,在这两种情况下,我都使用相同的 sbcl 1.1.8 和同一台机器,Mac OS 10.8.4。任何的想法?

代码:

(defun proc-file (filein fileout &key (fn-convert #'identity))
  (with-open-file (fout fileout
                   :direction :output
                   :if-exists :supersede
                   :external-format :utf8)
    (with-open-file (fin filein :external-format :utf8)
      (loop for line = (read-line fin nil)
        while line
        do 
        (handler-case
        (let* ((line (ppcre:regex-replace "^.*{jsonTweet=" line "{\"jsonTweet\":"))
               (data (gethash "jsonTweet" (yason:parse line))))
          (yason:encode (funcall fn-convert (yason:parse data)) fout)
          (format fout "~%"))
          (end-of-file ()
        (format *standard-output* "Error[~a]: ~a~%" filein line)))))))

【问题讨论】:

  • 我建议您首先假设这不是 yason 问题——我们会很快找出是否是——然后将以下内容添加到您的代码中:(format *standard-output* "~&amp;~{~x~^ ~}" (map 'list 'char-code line))。失败情况下的最后一行是否与 SLIME 环境中对应的行相同?
  • 可能在多个目录中存在名为 BRAvESP000.log 的文件,如果您在 SLIME 中或手动启动 SBCL,则当前目录会有所不同。尝试绝对路径。
  • 如果字符代码没有错误,则属于代理对的Unicode范围。这些不是 UTF-8 编码的字符,它们保留用于 UTF-16。这是我的猜测:网页设计中有一个现代传统,即使用私人平面字符和特殊字体作为图标(如各种箭头、子弹等)。 Twitter 尤其如此(例如 Github 也是如此)。这是 HTML 页面节省加载图像的一种方式(因为这些是特殊字体的矢量轮廓)。我想 Emacs 在发送之前会处理它们。
  • 但 SBCL 本身没有。我认为如果你只是删除那些,或者用不那么冒犯的东西代替,你会没事的。

标签: unicode utf-8 lisp common-lisp sbcl


【解决方案1】:

这几乎可以肯定是 yason 中的一个错误。 JSON 要求如果非 BMP 字符被转义,则通过代理对来完成。下面是一个简单的 U+10000 示例(在 json 中可选转义为“\ud800\udc00”;我使用 babel,因为 babel 的转换较少):

(map 'list #'char-code (yason:parse "\"\\ud800\\udc00\"")) 
  => (55296 56320)

unicode 代码点 55296(十进制)是代理对的开头,除非在 UTF-16 中作为代理对,否则不应出现。幸运的是,它可以通过使用 babel 将字符串编码为 UTF-16 并再次返回来轻松解决:

(babel:octets-to-string (babel:string-to-octets (yason:parse "\"\\ud800\\udc00\"") :encoding :utf-16le) :encoding :utf-16le)
  => "?"

您应该可以通过更改此行来解决此问题:

(yason:encode (funcall fn-convert (yason:parse data)) fout)

使用中间字符串,将其转换为 UTF-16 并返回。

(write-sequence
 (babel:octets-to-string
  (babel:string-to-octets
   (with-output-to-string (outs)
    (yason:encode (funcall fn-convert (yason:parse data)) outs))
   :encoding :utf-16le)
  :encoding :utf-16le)
 fout)

我提交了一个被 yason 接受的补丁来解决这个问题:

https://github.com/hanshuebner/yason/commit/4a9bdaae652b7ceea79984e0349a992a5458a0dc

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多