LoginSignup
1
1

More than 5 years have passed since last update.

文字列をバイト列のstreamとして扱う

Posted at

文字列をバイト列にし、それをstreamとして扱いたい場面があったのでその方法をメモしておく。

以下のような関数のテストがしたかった

read-string.lisp
(defmethod get-data-from-post-data (query)
  "clackサーバのenvironmentからPostのデータを読み取って適切な文字コードに変換して返す"
  (let ((stream (getf query :RAW-BODY))
    (temp ""))
    (setf temp (concatenate 'string 
                temp 
                (jp:decode (getf query :RAW-BODY-BUFFER) :guess)))
    (do ((line (read-line stream nil)
               (read-line stream nil)))
        ((null line))
      (setf temp (concatenate 'string
                      temp 
                      (jp:decode (kmrcl:string-to-usb8-array line)  :guess)
                      (list #\newline))))
    temp))

clackではqueryの:raw-bodyに送信されてきたデータが入っている。
その型は、バイト列のstream(flexi-stream)である。

この関数のテストをしようとしたら
文字列からバイト列のstreamを作成する必要がある。

create-stream.lisp
(flexi-streams:make-flexi-stream (flexi-streams:make-in-memory-input-stream (flexi-streams:string-to-octets 
"こんにちわ
日本語でもだいじょうぶ
&author=test" :external-format :utf-8)))

上記のようにすることで、文字列をバイト列のstreamに変換できた。
これを見つけるのに2時間も時間を使ってしまった...。

in-memory-input-streamで作っただけだとread-lineするとエラーになってしまう。
in-memory-input-streamからflexi-streamに変換することでread-lineできるstreamに
なることに気が付かなかった...。

とりあえず、これで様々な入力のテスト実行ができたのでガシガシ開発進めていきたい。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1