一次一个字节地解析 Common Lisp 中已知长度的 UTF-8 字符串
Parsing UTF-8 string of known length in Common Lisp one byte at a time
我正在用 Common Lisp 编写一个程序来编辑生成的二进制文件
由使用 NBT 格式的 Minecraft 开发,在此处记录:
http://minecraft.gamepedia.com/NBT_format?cookieSetup=true
(我知道存在这样的工具,例如 NBTEditor 和 MCEdit,但它们都不是用 Common Lisp 编写的,我认为这个项目将成为一个很好的学习练习)。
到目前为止,我还没有设法自己实现的唯一事情之一
是一个函数,用于读取已知长度的 UTF-8 字符串,该字符串包含使用多个八位字节(即非 ASCII 字符)表示的字符。在 NBT 格式中,每个字符串都是 UTF-8 编码的,并且前面有一个短整数(两个八位字节)n
表示字符串的长度。因此,假设字符串中只存在 ASCII 字符,我可以简单地从流中读取一个 n
八位字节序列,然后使用如下方法将其转换为字符串:
(defun read-utf-8-string (string-length byte-stream)
(let ((seq (make-array string-length :element-type '(unsigned-byte 8)
:fill-pointer t)))
(setf (fill-pointer seq) (read-sequence seq byte-stream))
(flexi-streams:octets-to-string seq :external-format :utf-8)))
但是如果一个或多个字符的字符代码大于
255,它被编码为两个或更多字节,如本例所示:
(flexi-streams:string-to-octets "wife" :external-format :utf-8)
==> #(119 105 102 101)
(flexi-streams:string-to-octets "жена" :external-format :utf-8)
==> #(208 182 208 181 208 189 208 176)
两个字符串的长度相同,但俄语单词的每个字符
被编码成八位字节数的两倍,所以总的大小
字符串是英文字符串的两倍。因此,如果使用读取序列,知道字符串长度无济于事。即使尺寸
字符串(即编码它所需的八位字节数)是已知的,仍然无法知道哪些八位字节要单独转换为字符形式,哪些要组合在一起进行转换。因此,我没有滚动自己的函数,而是试图找到一种方法来让实现 (Clozure CL) 或外部库为我完成工作。不幸的是,这也有问题,因为我的解析器依赖于对所有读取函数使用相同的文件流,如下所示:
(with-open-file (stream "test.dat" :direction :input
:element-type '(unsigned-byte 8))
;;Read entire contents of NBT file from stream here)
这将我限制为 :element-type '(unsigned-byte 8)
,因此禁止我指定字符编码和使用 read-char(或等效),如下所示:
(with-open-file (stream "test.dat" :external-format :utf-8)
...)
:element-type
必须是 '(unsigned-byte 8)
这样我才能读写各种大小的整数和浮点数。为了避免必须手动
将八位字节序列转换为字符串,我首先想知道是否有一种方法可以在文件打开时将元素类型更改为字符类型,这让我在这里进行了讨论:
https://groups.google.com/forum/#!searchin/comp.lang.lisp/binary$20write$20read/comp.lang.lisp/N0IESNPSPCU/Qmcvtk0HkC0J
显然,某些 CL 实现(例如 SBCL)默认使用二价流,因此可以在同一流上同时使用 read-byte 和 read-char;如果我采用这种方法,我仍然需要能够为流 (:utf-8
) 指定一个 :external-format
,尽管这种格式只适用于读取字符,而不适用于读取原始字节.
为了简洁起见,我在上面的示例中使用了 flexi-streams 中的几个函数,但到目前为止,我的代码仅使用内置流类型,我还没有使用 flexi-streams 本身。 这个问题适合 flexi-streams 吗?有一个额外的抽象层可以让我从同一个流中互换地读取原始字节和 UTF-8 字符将是理想的。
非常感谢熟悉 flexi-streams(或其他相关方法)的人提供的任何建议。
谢谢。
这是我写的东西:
首先,我们想知道给定第一个字节,某些字符的实际编码长度。
(defun utf-8-number-of-bytes (first-byte)
"returns the length of the utf-8 code in number of bytes, based on the first byte.
The length can be a number between 1 and 4."
(declare (fixnum first-byte))
(cond ((= 0 (ldb (byte 1 7) first-byte)) 1)
((= #b110 (ldb (byte 3 5) first-byte)) 2)
((= #b1110 (ldb (byte 4 4) first-byte)) 3)
((= #b11110 (ldb (byte 5 3) first-byte)) 4)
(t (error "unknown number of utf-8 bytes for ~a" first-byte))))
然后我们解码:
(defun utf-8-decode-unicode-character-code-from-stream (stream)
"Decodes byte values, from a binary byte stream, which describe a character
encoded using UTF-8.
Returns the character code and the number of bytes read."
(let* ((first-byte (read-byte stream))
(number-of-bytes (utf-8-number-of-bytes first-byte)))
(declare (fixnum first-byte number-of-bytes))
(ecase number-of-bytes
(1 (values (ldb (byte 7 0) first-byte)
1))
(2 (values (logior (ash (ldb (byte 5 0) first-byte) 6)
(ldb (byte 6 0) (read-byte stream)))
2))
(3 (values (logior (ash (ldb (byte 5 0) first-byte) 12)
(ash (ldb (byte 6 0) (read-byte stream)) 6)
(ldb (byte 6 0) (read-byte stream)))
3))
(4 (values (logior (ash (ldb (byte 3 0) first-byte) 18)
(ash (ldb (byte 6 0) (read-byte stream)) 12)
(ash (ldb (byte 6 0) (read-byte stream)) 6)
(ldb (byte 6 0) (read-byte stream)))
4))
(t (error "wrong UTF-8 encoding for file position ~a of stream ~s"
(file-position stream)
stream)))))
你知道有多少个字符。 N
个字符。您可以为 N 个字符分配支持 unicode 的字符串。所以你调用函数 N
次。然后,对于每个结果,将结果转换为字符并将其放入字符串中。
我正在用 Common Lisp 编写一个程序来编辑生成的二进制文件 由使用 NBT 格式的 Minecraft 开发,在此处记录: http://minecraft.gamepedia.com/NBT_format?cookieSetup=true (我知道存在这样的工具,例如 NBTEditor 和 MCEdit,但它们都不是用 Common Lisp 编写的,我认为这个项目将成为一个很好的学习练习)。
到目前为止,我还没有设法自己实现的唯一事情之一
是一个函数,用于读取已知长度的 UTF-8 字符串,该字符串包含使用多个八位字节(即非 ASCII 字符)表示的字符。在 NBT 格式中,每个字符串都是 UTF-8 编码的,并且前面有一个短整数(两个八位字节)n
表示字符串的长度。因此,假设字符串中只存在 ASCII 字符,我可以简单地从流中读取一个 n
八位字节序列,然后使用如下方法将其转换为字符串:
(defun read-utf-8-string (string-length byte-stream)
(let ((seq (make-array string-length :element-type '(unsigned-byte 8)
:fill-pointer t)))
(setf (fill-pointer seq) (read-sequence seq byte-stream))
(flexi-streams:octets-to-string seq :external-format :utf-8)))
但是如果一个或多个字符的字符代码大于 255,它被编码为两个或更多字节,如本例所示:
(flexi-streams:string-to-octets "wife" :external-format :utf-8)
==> #(119 105 102 101)
(flexi-streams:string-to-octets "жена" :external-format :utf-8)
==> #(208 182 208 181 208 189 208 176)
两个字符串的长度相同,但俄语单词的每个字符 被编码成八位字节数的两倍,所以总的大小 字符串是英文字符串的两倍。因此,如果使用读取序列,知道字符串长度无济于事。即使尺寸 字符串(即编码它所需的八位字节数)是已知的,仍然无法知道哪些八位字节要单独转换为字符形式,哪些要组合在一起进行转换。因此,我没有滚动自己的函数,而是试图找到一种方法来让实现 (Clozure CL) 或外部库为我完成工作。不幸的是,这也有问题,因为我的解析器依赖于对所有读取函数使用相同的文件流,如下所示:
(with-open-file (stream "test.dat" :direction :input
:element-type '(unsigned-byte 8))
;;Read entire contents of NBT file from stream here)
这将我限制为 :element-type '(unsigned-byte 8)
,因此禁止我指定字符编码和使用 read-char(或等效),如下所示:
(with-open-file (stream "test.dat" :external-format :utf-8)
...)
:element-type
必须是 '(unsigned-byte 8)
这样我才能读写各种大小的整数和浮点数。为了避免必须手动
将八位字节序列转换为字符串,我首先想知道是否有一种方法可以在文件打开时将元素类型更改为字符类型,这让我在这里进行了讨论:
https://groups.google.com/forum/#!searchin/comp.lang.lisp/binary$20write$20read/comp.lang.lisp/N0IESNPSPCU/Qmcvtk0HkC0J
显然,某些 CL 实现(例如 SBCL)默认使用二价流,因此可以在同一流上同时使用 read-byte 和 read-char;如果我采用这种方法,我仍然需要能够为流 (:utf-8
) 指定一个 :external-format
,尽管这种格式只适用于读取字符,而不适用于读取原始字节.
为了简洁起见,我在上面的示例中使用了 flexi-streams 中的几个函数,但到目前为止,我的代码仅使用内置流类型,我还没有使用 flexi-streams 本身。 这个问题适合 flexi-streams 吗?有一个额外的抽象层可以让我从同一个流中互换地读取原始字节和 UTF-8 字符将是理想的。
非常感谢熟悉 flexi-streams(或其他相关方法)的人提供的任何建议。
谢谢。
这是我写的东西:
首先,我们想知道给定第一个字节,某些字符的实际编码长度。
(defun utf-8-number-of-bytes (first-byte)
"returns the length of the utf-8 code in number of bytes, based on the first byte.
The length can be a number between 1 and 4."
(declare (fixnum first-byte))
(cond ((= 0 (ldb (byte 1 7) first-byte)) 1)
((= #b110 (ldb (byte 3 5) first-byte)) 2)
((= #b1110 (ldb (byte 4 4) first-byte)) 3)
((= #b11110 (ldb (byte 5 3) first-byte)) 4)
(t (error "unknown number of utf-8 bytes for ~a" first-byte))))
然后我们解码:
(defun utf-8-decode-unicode-character-code-from-stream (stream)
"Decodes byte values, from a binary byte stream, which describe a character
encoded using UTF-8.
Returns the character code and the number of bytes read."
(let* ((first-byte (read-byte stream))
(number-of-bytes (utf-8-number-of-bytes first-byte)))
(declare (fixnum first-byte number-of-bytes))
(ecase number-of-bytes
(1 (values (ldb (byte 7 0) first-byte)
1))
(2 (values (logior (ash (ldb (byte 5 0) first-byte) 6)
(ldb (byte 6 0) (read-byte stream)))
2))
(3 (values (logior (ash (ldb (byte 5 0) first-byte) 12)
(ash (ldb (byte 6 0) (read-byte stream)) 6)
(ldb (byte 6 0) (read-byte stream)))
3))
(4 (values (logior (ash (ldb (byte 3 0) first-byte) 18)
(ash (ldb (byte 6 0) (read-byte stream)) 12)
(ash (ldb (byte 6 0) (read-byte stream)) 6)
(ldb (byte 6 0) (read-byte stream)))
4))
(t (error "wrong UTF-8 encoding for file position ~a of stream ~s"
(file-position stream)
stream)))))
你知道有多少个字符。 N
个字符。您可以为 N 个字符分配支持 unicode 的字符串。所以你调用函数 N
次。然后,对于每个结果,将结果转换为字符并将其放入字符串中。