1

通过在 emacs 中调用 google dictionary api, http://www.google.com/dictionary/json? callback=cb&q=word&sl=en&tl=en&restrict=pr%%2Cde&client=te 我可以得到如下响应

"entries": [{
    "type": "example",
    "terms": [{
        "type": "text",
        "text": "his grandfather\x27s \x3cem\x3ewords\x3c/em\x3e had been meant kindly",
        "language": "en"
    }]
}]

如您所见,“文本”中有转义的 unicode。我想将它们转换为如下所示的功能。

(defun unescape-string (string)
    "Return unescape unicode string"
    ...
)
(unescape-string "his grandfather\x27s \x3cem\x3ewords\x3c/em\x3e")
=> "his grandfathers's <em>words</em>"

(insert #x27)'
(insert #x27)'
(insert #x3c)<
(insert #x3e)>

这是我尝试过的

但是,我想我不知道如何用相应的 unicode 替换 '\x123' 到缓冲区或字符串中。

提前致谢

4

1 回答 1

2

似乎是最简单的方法:

(read (princ "\"his grandfather\\x27s \\x3cem\\x3ewords\\x3c/em\\x3e had been meant kindly\""))
;; "his grandfather's ώm>words</em> had been meant kindly"

此外,Emacs 解析\x3ce而不是\x3c. 我不确定这是错误还是预期行为。我一直认为它不应该在之后阅读超过两个字符x......

如果您仍想使用read+princ组合,则需要添加反斜杠以防止 Emacs 解析更多字符,例如:\x3c\e. 或者这是我能想出的快速方法:

(defun replace-c-escape-codes (input)
  (replace-regexp-in-string 
   "\\\\x[[:xdigit:]][[:xdigit:]]"
   (lambda (match)
     (make-string 1 (string-to-number (substring match 2) 16)))
   input))

(replace-c-escape-codes "his grandfather\\x27s \\x3cem\\x3ewords\\x3c/em\\x3e")
"his grandfather's <em>words</em>"
于 2013-10-29T07:34:57.957 回答