24

我正在调用一个返回字节数组的有趣 API,但我想要一个文本流。有没有一种简单的方法可以从字节数组中获取文本流?现在我只是拼凑起来:

(defun bytearray-to-string (bytes)
  (let ((str (make-string (length bytes))))
    (loop for byte across bytes
       for i from 0
       do (setf (aref str i) (code-char byte)))
    str))

然后将结果包装在 with-input-from-string 中,但这不是最好的方法。(另外,它的效率非常低。)

在这种情况下,我知道它始终是 ASCII,因此将其解释为 ASCII 或 UTF-8 都可以。我正在使用支持 Unicode 的 SBCL,但我更喜欢便携式(甚至仅 ASCII)解决方案,而不是 SBCL-Unicode 特定的解决方案。

4

6 回答 6

32

FLEXI-STREAMS ( http://weitz.de/flexi-streams/ ) 具有便携转换功能

(flexi-streams:octets-to-string #(72 101 108 108 111) :external-format :utf-8)

=>

"Hello"

或者,如果你想要一个流:

(flexi-streams:make-flexi-stream
   (flexi-streams:make-in-memory-input-stream
      #(72 101 108 108 111))
   :external-format :utf-8)

将返回一个从字节向量读取文本的流

于 2009-03-01T18:56:38.703 回答
25

此转换有两个可移植库:

  • flexi-streams,已经在另一个答案中提到过。

    该库较旧且具有更多功能,尤其是可扩展流。

  • Babel,一个专门用于字符编码和解码的库

    Babel 相对于 flexi-streams 的主要优势是速度。

为获得最佳性能,如果 Babel 具有您需要的功能,请使用它,否则回退到弹性流。低于说明速度差异的(略微不科学的)微基准。

对于这个测试用例,Babel 的速度提高了337 倍,所需的内存减少了 200 倍。

(asdf:operate 'asdf:load-op :flexi-streams)
(asdf:operate 'asdf:load-op :babel)

(defun flexi-streams-test (bytes n)
  (loop
     repeat n
     collect (flexi-streams:octets-to-string bytes :external-format :utf-8)))

(defun babel-test (bytes n)
  (loop
     repeat n
     collect (babel:octets-to-string bytes :encoding :utf-8)))

(defun test (&optional (data #(72 101 108 108 111))
                       (n 10000))
  (let* ((ub8-vector (coerce data '(simple-array (unsigned-byte 8) (*))))
         (result1 (time (flexi-streams-test ub8-vector n)))
         (result2 (time (babel-test ub8-vector n))))
    (assert (equal result1 result2))))

#|
CL-USER> (test)
Evaluation took:
  1.348 seconds of real time
  1.328083 seconds of user run time
  0.020002 seconds of system run time
  [Run times include 0.12 seconds GC run time.]
  0 calls to %EVAL
  0 page faults and
  126,402,160 bytes consed.
Evaluation took:
  0.004 seconds of real time
  0.004 seconds of user run time
  0.0 seconds of system run time
  0 calls to %EVAL
  0 page faults and
  635,232 bytes consed.
|#
于 2009-03-02T12:32:24.017 回答
15

如果您不必担心 UTF-8 编码(本质上意味着“纯 ASCII”),您可以使用 MAP:

(map 'string #'code-char #(72 101 108 108 111))
于 2009-03-04T15:25:21.557 回答
7

我说使用建议的 flexistream 或 babel 解决方案。

但为了完整性和未来 googlers 到达此页面的利益,我想提一下 sbcl 自己的 sb-ext:octets-to-string:

   SB-EXT:OCTETS-TO-STRING is an external symbol in #<PACKAGE "SB-EXT">.
   Function: #<FUNCTION SB-EXT:OCTETS-TO-STRING>
   Its associated name (as in FUNCTION-LAMBDA-EXPRESSION) is
     SB-EXT:OCTETS-TO-STRING.
   The function's arguments are:  (VECTOR &KEY (EXTERNAL-FORMAT DEFAULT) (START 0)
                                          END)
   Its defined argument types are:
     ((VECTOR (UNSIGNED-BYTE 8)) &KEY (:EXTERNAL-FORMAT T) (:START T) (:END T))
   Its result type is:
     *
于 2009-06-18T06:45:59.467 回答
4

SBCL 支持所谓的灰色流。这些是基于 CLOS 类和通用函数的可扩展流。您可以创建一个从字节数组中获取字符的文本流子类。

于 2009-03-01T17:57:28.487 回答
0

试试这个FORMAT功能。 (FORMAT NIL ...)以字符串形式返回结果。

于 2009-03-01T16:50:23.063 回答