src/Pure/General/bytes.ML
changeset 75603 fc8d64a578e4
parent 75602 7a0d4c126f79
child 75615 4494cd69f97f
equal deleted inserted replaced
75602:7a0d4c126f79 75603:fc8d64a578e4
    10 
    10 
    11 signature BYTES =
    11 signature BYTES =
    12 sig
    12 sig
    13   val chunk_size: int
    13   val chunk_size: int
    14   type T
    14   type T
       
    15   val eq: T * T -> bool
    15   val length: T -> int
    16   val length: T -> int
    16   val contents: T -> string list
    17   val contents: T -> string list
    17   val contents_blob: T -> XML.body
    18   val contents_blob: T -> XML.body
    18   val content: T -> string
    19   val content: T -> string
    19   val is_empty: T -> bool
    20   val is_empty: T -> bool
    25   val exists_string: (string -> bool) -> T -> bool
    26   val exists_string: (string -> bool) -> T -> bool
    26   val forall_string: (string -> bool) -> T -> bool
    27   val forall_string: (string -> bool) -> T -> bool
    27   val last_string: T -> string option
    28   val last_string: T -> string option
    28   val trim_line: T -> T
    29   val trim_line: T -> T
    29   val append: T -> T -> T
    30   val append: T -> T -> T
       
    31   val appends: T list -> T
    30   val string: string -> T
    32   val string: string -> T
    31   val newline: T
    33   val newline: T
    32   val buffer: Buffer.T -> T
    34   val buffer: Buffer.T -> T
    33   val space_explode: string -> T -> string list
    35   val space_explode: string -> T -> string list
    34   val split_lines: T -> string list
    36   val split_lines: T -> string list
    53 with
    55 with
    54 
    56 
    55 fun length (Bytes {m, n, ...}) = m + n;
    57 fun length (Bytes {m, n, ...}) = m + n;
    56 
    58 
    57 val compact = implode o rev;
    59 val compact = implode o rev;
       
    60 
       
    61 fun eq (Bytes {buffer, chunks, m, n}, Bytes {buffer = buffer', chunks = chunks', m = m', n = n'}) =
       
    62   m = m' andalso n = n' andalso chunks = chunks' andalso
       
    63   (buffer = buffer' orelse compact buffer = compact buffer);
    58 
    64 
    59 fun contents (Bytes {buffer, chunks, ...}) =
    65 fun contents (Bytes {buffer, chunks, ...}) =
    60   rev (chunks |> not (null buffer) ? cons (compact buffer));
    66   rev (chunks |> not (null buffer) ? cons (compact buffer));
    61 
    67 
    62 val contents_blob = contents #> XML.blob;
    68 val contents_blob = contents #> XML.blob;
   133 
   139 
   134 fun append bytes1 bytes2 =  (*left-associative*)
   140 fun append bytes1 bytes2 =  (*left-associative*)
   135   if is_empty bytes1 then bytes2
   141   if is_empty bytes1 then bytes2
   136   else if is_empty bytes2 then bytes1
   142   else if is_empty bytes2 then bytes1
   137   else bytes1 |> fold add (contents bytes2);
   143   else bytes1 |> fold add (contents bytes2);
       
   144 
       
   145 val appends = build o fold (fn b => fn a => append a b);
   138 
   146 
   139 val string = build o add;
   147 val string = build o add;
   140 
   148 
   141 val newline = string "\n";
   149 val newline = string "\n";
   142 
   150