src/Pure/General/bytes.ML
author wenzelm
Mon, 25 Mar 2019 16:45:08 +0100
changeset 69980 f2e3adfd916f
parent 69446 9cf0b79dfb7f
child 75581 29654a8e9374
permissions -rw-r--r--
tuned signature;

(*  Title:      Pure/General/bytes.ML
    Author:     Makarius

Byte-vector messages.
*)

signature BYTES =
sig
  val read_line: BinIO.instream -> string option
  val read_block: BinIO.instream -> int -> string
end;

structure Bytes: BYTES =
struct

fun read_line stream =
  let
    val result = trim_line o String.implode o rev;
    fun read cs =
      (case BinIO.input1 stream of
        NONE => if null cs then NONE else SOME (result cs)
      | SOME b =>
          (case Byte.byteToChar b of
            #"\n" => SOME (result cs)
          | c => read (c :: cs)));
  in read [] end;

fun read_block stream n =
  Byte.bytesToString (BinIO.inputN (stream, n));

end;