package iostream

  1. Overview
  2. Docs
Generic, composable IO input and output streams

Install

dune-project
 Dependency

Authors

Maintainers

Sources

iostream-0.2.1.tbz
sha256=09cd438f755d46bf10e986be0d3dd2ab243b1f5455c3c7410dcde8de521615a9
sha512=c5f4c251ba33386dd027d2cf340268d14fdbcd3119be6bac3586fd9ad1e9f90992ef12e8fd9fbf2442c6747560d84fd2461656ea639f818e53d0b08e82dd8e9a

doc/src/iostream/slice.ml.html

Source file slice.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(** Byte slice or buffer. *)

type t = {
  bytes: bytes;  (** Bytes *)
  mutable off: int;  (** Offset in bytes *)
  mutable len: int;  (** Length of the slice. Empty slice has [len=0] *)
}
(** A slice of bytes.
    The valid bytes in the slice are [bytes[off], bytes[off+1], …, bytes[off+len-1]]
    (i.e [len] bytes starting at offset [off]). *)

let empty : t = { bytes = Bytes.create 0; off = 0; len = 0 }

let create size : t =
  let size = max 16 size in
  if size > Sys.max_string_length then
    invalid_arg "Slice.create: size is too big";
  { bytes = Bytes.create size; off = 0; len = 0 }

let[@inline] of_bytes bs : t = { bytes = bs; off = 0; len = 0 }
let[@inline] bytes self = self.bytes
let[@inline] off self = self.off
let[@inline] len self = self.len

(** Consume the first [n] bytes from the slice, making it [n] bytes
    shorter. This modifies the slice in place. *)
let[@inline] consume (self : t) n : unit =
  if n < 0 || n > self.len then invalid_arg "In_buf.consume_buf";
  self.off <- self.off + n;
  self.len <- self.len - n

(** find index of [c] in slice, or raise [Not_found] *)
let find_index_exn (self : t) c : int =
  let j = Bytes.index_from self.bytes self.off c in
  if j < self.off + self.len then
    j
  else
    raise Not_found