val get : bytes -> int -> char
get s n
returns the byte at indexn
in arguments
.Raise
Invalid_argument
ifn
is not a valid index ins
.
val set : bytes -> int -> char -> unit
set s n c
modifiess
in place, replacing the byte at indexn
withc
.Raise
Invalid_argument
ifn
is not a valid index ins
.
val create : int -> bytes
create n
returns a new byte sequence of lengthn
. The sequence is uninitialized and contains arbitrary bytes.Raise
Invalid_argument
ifn < 0
orn >
Sys.max_string_length
.
val make : int -> char -> bytes
make n c
returns a new byte sequence of lengthn
, filled with the bytec
.Raise
Invalid_argument
ifn < 0
orn >
Sys.max_string_length
.
val init : int -> (int -> char) -> bytes
Bytes.init n f
returns a fresh byte sequence of lengthn
, with characteri
initialized to the result off i
(in increasing index order).Raise
Invalid_argument
ifn < 0
orn >
Sys.max_string_length
.
val of_string : string -> bytes
Return a new byte sequence that contains the same bytes as the given string.
val to_string : bytes -> string
Return a new string that contains the same bytes as the given byte sequence.
val sub : bytes -> int -> int -> bytes
sub s start len
returns a new byte sequence of lengthlen
, containing the subsequence ofs
that starts at positionstart
and has lengthlen
.Raise
Invalid_argument
ifstart
andlen
do not designate a valid range ofs
.
val sub_string : bytes -> int -> int -> string
Same as
sub
but return a string instead of a byte sequence.
val extend : bytes -> int -> int -> bytes
extend s left right
returns a new byte sequence that contains the bytes ofs
, withleft
uninitialized bytes prepended andright
uninitialized bytes appended to it. Ifleft
orright
is negative, then bytes are removed (instead of appended) from the corresponding side ofs
.Raise
Invalid_argument
if the result length is negative or longer thanSys.max_string_length
bytes.
val fill : bytes -> int -> int -> char -> unit
fill s start len c
modifiess
in place, replacinglen
characters withc
, starting atstart
.Raise
Invalid_argument
ifstart
andlen
do not designate a valid range ofs
.
val blit : bytes -> int -> bytes -> int -> int -> unit
blit src srcoff dst dstoff len
copieslen
bytes from sequencesrc
, starting at indexsrcoff
, to sequencedst
, starting at indexdstoff
. It works correctly even ifsrc
anddst
are the same byte sequence, and the source and destination intervals overlap.Raise
Invalid_argument
ifsrcoff
andlen
do not designate a valid range ofsrc
, or ifdstoff
andlen
do not designate a valid range ofdst
.
val blit_string : string -> int -> bytes -> int -> int -> unit
blit src srcoff dst dstoff len
copieslen
bytes from stringsrc
, starting at indexsrcoff
, to byte sequencedst
, starting at indexdstoff
.Raise
Invalid_argument
ifsrcoff
andlen
do not designate a valid range ofsrc
, or ifdstoff
andlen
do not designate a valid range ofdst
.
val concat : bytes -> bytes list -> bytes
concat sep sl
concatenates the list of byte sequencessl
, inserting the separator byte sequencesep
between each, and returns the result as a new byte sequence.Raise
Invalid_argument
if the result is longer thanSys.max_string_length
bytes.
val cat : bytes -> bytes -> bytes
cat s1 s2
concatenatess1
ands2
and returns the result as new byte sequence.Raise
Invalid_argument
if the result is longer thanSys.max_string_length
bytes.
val iter : (char -> unit) -> bytes -> unit
iter f s
applies functionf
in turn to all the bytes ofs
. It is equivalent tof (get s 0); f (get s 1); ...; f (get s (length s - 1)); ()
.
val iteri : (int -> char -> unit) -> bytes -> unit
Same as
Bytes.iter
, but the function is applied to the index of the byte as first argument and the byte itself as second argument.
val map : (char -> char) -> bytes -> bytes
map f s
applies functionf
in turn to all the bytes ofs
(in increasing index order) and stores the resulting bytes in a new sequence that is returned as the result.
val mapi : (int -> char -> char) -> bytes -> bytes
mapi f s
callsf
with each character ofs
and its index (in increasing index order) and stores the resulting bytes in a new sequence that is returned as the result.
val trim : bytes -> bytes
Return a copy of the argument, without leading and trailing whitespace. The bytes regarded as whitespace are the ASCII characters
' '
,'\012'
,'\n'
,'\r'
, and'\t'
.
val escaped : bytes -> bytes
Return a copy of the argument, with special characters represented by escape sequences, following the lexical conventions of OCaml. All characters outside the ASCII printable range (32..126) are escaped, as well as backslash and double-quote.
Raise
Invalid_argument
if the result is longer thanSys.max_string_length
bytes.
val index : bytes -> char -> int
index s c
returns the index of the first occurrence of bytec
ins
.Raise
Not_found
ifc
does not occur ins
.
val index_opt : bytes -> char -> int option
index_opt s c
returns the index of the first occurrence of bytec
ins
orNone
ifc
does not occur ins
.- since
- 4.05
val rindex : bytes -> char -> int
rindex s c
returns the index of the last occurrence of bytec
ins
.Raise
Not_found
ifc
does not occur ins
.
val rindex_opt : bytes -> char -> int option
rindex_opt s c
returns the index of the last occurrence of bytec
ins
orNone
ifc
does not occur ins
.- since
- 4.05
val index_from : bytes -> int -> char -> int
index_from s i c
returns the index of the first occurrence of bytec
ins
after positioni
.Bytes.index s c
is equivalent toBytes.index_from s 0 c
.Raise
Invalid_argument
ifi
is not a valid position ins
. RaiseNot_found
ifc
does not occur ins
after positioni
.
val index_from_opt : bytes -> int -> char -> int option
index_from _opts i c
returns the index of the first occurrence of bytec
ins
after positioni
orNone
ifc
does not occur ins
after positioni
.Bytes.index_opt s c
is equivalent toBytes.index_from_opt s 0 c
.Raise
Invalid_argument
ifi
is not a valid position ins
.- since
- 4.05
val rindex_from : bytes -> int -> char -> int
rindex_from s i c
returns the index of the last occurrence of bytec
ins
before positioni+1
.rindex s c
is equivalent torindex_from s (Bytes.length s - 1) c
.Raise
Invalid_argument
ifi+1
is not a valid position ins
. RaiseNot_found
ifc
does not occur ins
before positioni+1
.
val rindex_from_opt : bytes -> int -> char -> int option
rindex_from_opt s i c
returns the index of the last occurrence of bytec
ins
before positioni+1
orNone
ifc
does not occur ins
before positioni+1
.rindex_opt s c
is equivalent torindex_from s (Bytes.length s - 1) c
.Raise
Invalid_argument
ifi+1
is not a valid position ins
.- since
- 4.05
val contains_from : bytes -> int -> char -> bool
contains_from s start c
tests if bytec
appears ins
after positionstart
.contains s c
is equivalent tocontains_from s 0 c
.Raise
Invalid_argument
ifstart
is not a valid position ins
.
val rcontains_from : bytes -> int -> char -> bool
rcontains_from s stop c
tests if bytec
appears ins
before positionstop+1
.Raise
Invalid_argument
ifstop < 0
orstop+1
is not a valid position ins
.
val uppercase : bytes -> bytes
Return a copy of the argument, with all lowercase letters translated to uppercase, including accented letters of the ISO Latin-1 (8859-1) character set.
- deprecated
Functions operating on Latin-1 character set are deprecated.
val lowercase : bytes -> bytes
Return a copy of the argument, with all uppercase letters translated to lowercase, including accented letters of the ISO Latin-1 (8859-1) character set.
- deprecated
Functions operating on Latin-1 character set are deprecated.
val capitalize : bytes -> bytes
Return a copy of the argument, with the first character set to uppercase, using the ISO Latin-1 (8859-1) character set..
- deprecated
Functions operating on Latin-1 character set are deprecated.
val uncapitalize : bytes -> bytes
Return a copy of the argument, with the first character set to lowercase, using the ISO Latin-1 (8859-1) character set..
- deprecated
Functions operating on Latin-1 character set are deprecated.
val uppercase_ascii : bytes -> bytes
Return a copy of the argument, with all lowercase letters translated to uppercase, using the US-ASCII character set.
- since
- 4.03.0
val lowercase_ascii : bytes -> bytes
Return a copy of the argument, with all uppercase letters translated to lowercase, using the US-ASCII character set.
- since
- 4.03.0
val capitalize_ascii : bytes -> bytes
Return a copy of the argument, with the first character set to uppercase, using the US-ASCII character set.
- since
- 4.03.0
val uncapitalize_ascii : bytes -> bytes
Return a copy of the argument, with the first character set to lowercase, using the US-ASCII character set.
- since
- 4.03.0
val compare : t -> t -> int
The comparison function for byte sequences, with the same specification as
Stdlib.compare
. Along with the typet
, this functioncompare
allows the moduleBytes
to be passed as argument to the functorsSet.Make
andMap.Make
.
Unsafe conversions (for advanced users)
val unsafe_to_string : bytes -> string
Unsafely convert a byte sequence into a string.
To reason about the use of
unsafe_to_string
, it is convenient to consider an "ownership" discipline. A piece of code that manipulates some data "owns" it; there are several disjoint ownership modes, including:- Unique ownership: the data may be accessed and mutated
- Shared ownership: the data has several owners, that may only access it, not mutate it.
Unique ownership is linear: passing the data to another piece of code means giving up ownership (we cannot write the data again). A unique owner may decide to make the data shared (giving up mutation rights on it), but shared data may not become uniquely-owned again.
unsafe_to_string s
can only be used when the caller owns the byte sequences
-- either uniquely or as shared immutable data. The caller gives up ownership ofs
, and gains ownership of the returned string.There are two valid use-cases that respect this ownership discipline:
1. Creating a string by initializing and mutating a byte sequence that is never changed after initialization is performed.
let string_init len f : string = let s = Bytes.create len in for i = 0 to len - 1 do Bytes.set s i (f i) done; Bytes.unsafe_to_string s
This function is safe because the byte sequence
s
will never be accessed or mutated afterunsafe_to_string
is called. Thestring_init
code gives up ownership ofs
, and returns the ownership of the resulting string to its caller.Note that it would be unsafe if
s
was passed as an additional parameter to the functionf
as it could escape this way and be mutated in the future --string_init
would give up ownership ofs
to pass it tof
, and could not callunsafe_to_string
safely.We have provided the
String.init
,String.map
andString.mapi
functions to cover most cases of building new strings. You should prefer those overto_string
orunsafe_to_string
whenever applicable.2. Temporarily giving ownership of a byte sequence to a function that expects a uniquely owned string and returns ownership back, so that we can mutate the sequence again after the call ended.
let bytes_length (s : bytes) = String.length (Bytes.unsafe_to_string s)
In this use-case, we do not promise that
s
will never be mutated after the call tobytes_length s
. TheString.length
function temporarily borrows unique ownership of the byte sequence (and sees it as astring
), but returns this ownership back to the caller, which may assume thats
is still a valid byte sequence after the call. Note that this is only correct because we know thatString.length
does not capture its argument -- it could escape by a side-channel such as a memoization combinator.The caller may not mutate
s
while the string is borrowed (it has temporarily given up ownership). This affects concurrent programs, but also higher-order functions: ifString.length
returned a closure to be called later,s
should not be mutated until this closure is fully applied and returns ownership.
val unsafe_of_string : string -> bytes
Unsafely convert a shared string to a byte sequence that should not be mutated.
The same ownership discipline that makes
unsafe_to_string
correct applies tounsafe_of_string
: you may use it if you were the owner of thestring
value, and you will own the returnbytes
in the same mode.In practice, unique ownership of string values is extremely difficult to reason about correctly. You should always assume strings are shared, never uniquely owned.
For example, string literals are implicitly shared by the compiler, so you never uniquely own them.
let incorrect = Bytes.unsafe_of_string "hello" let s = Bytes.of_string "hello"
The first declaration is incorrect, because the string literal
"hello"
could be shared by the compiler with other parts of the program, and mutatingincorrect
is a bug. You must always use the second version, which performs a copy and is thus correct.Assuming unique ownership of strings that are not string literals, but are (partly) built from string literals, is also incorrect. For example, mutating
unsafe_of_string ("foo" ^ s)
could mutate the shared string"foo"
-- assuming a rope-like representation of strings. More generally, functions operating on strings will assume shared ownership, they do not preserve unique ownership. It is thus incorrect to assume unique ownership of the result ofunsafe_of_string
.The only case we have reasonable confidence is safe is if the produced
bytes
is shared -- used as an immutable byte sequence. This is possibly useful for incremental migration of low-level programs that manipulate immutable sequences of bytes (for exampleMarshal.from_bytes
) and previously used thestring
type for this purpose.
Iterators
val to_seq : t -> char Stdlib.Seq.t
Iterate on the string, in increasing index order. Modifications of the string during iteration will be reflected in the iterator.
- since
- 4.07
val to_seqi : t -> (int * char) Stdlib.Seq.t
Iterate on the string, in increasing order, yielding indices along chars
- since
- 4.07
val of_seq : char Stdlib.Seq.t -> t
Create a string from the generator
- since
- 4.07
Binary encoding/decoding of integers
val get_uint8 : bytes -> int -> int
get_uint8 b i
isb
's unsigned 8-bit integer starting at byte indexi
.- since
- 4.08
val get_int8 : bytes -> int -> int
get_int8 b i
isb
's signed 8-bit integer starting at byte indexi
.- since
- 4.08
val get_uint16_ne : bytes -> int -> int
get_uint16_ne b i
isb
's native-endian unsigned 16-bit integer starting at byte indexi
.- since
- 4.08
val get_uint16_be : bytes -> int -> int
get_uint16_be b i
isb
's big-endian unsigned 16-bit integer starting at byte indexi
.- since
- 4.08
val get_uint16_le : bytes -> int -> int
get_uint16_le b i
isb
's little-endian unsigned 16-bit integer starting at byte indexi
.- since
- 4.08
val get_int16_ne : bytes -> int -> int
get_int16_ne b i
isb
's native-endian signed 16-bit integer starting at byte indexi
.- since
- 4.08
val get_int16_be : bytes -> int -> int
get_int16_be b i
isb
's big-endian signed 16-bit integer starting at byte indexi
.- since
- 4.08
val get_int16_le : bytes -> int -> int
get_int16_le b i
isb
's little-endian signed 16-bit integer starting at byte indexi
.- since
- 4.08
val get_int32_ne : bytes -> int -> int32
get_int32_ne b i
isb
's native-endian 32-bit integer starting at byte indexi
.- since
- 4.08
val get_int32_be : bytes -> int -> int32
get_int32_be b i
isb
's big-endian 32-bit integer starting at byte indexi
.- since
- 4.08
val get_int32_le : bytes -> int -> int32
get_int32_le b i
isb
's little-endian 32-bit integer starting at byte indexi
.- since
- 4.08
val get_int64_ne : bytes -> int -> int64
get_int64_ne b i
isb
's native-endian 64-bit integer starting at byte indexi
.- since
- 4.08
val get_int64_be : bytes -> int -> int64
get_int64_be b i
isb
's big-endian 64-bit integer starting at byte indexi
.- since
- 4.08
val get_int64_le : bytes -> int -> int64
get_int64_le b i
isb
's little-endian 64-bit integer starting at byte indexi
.- since
- 4.08
val set_uint8 : bytes -> int -> int -> unit
set_uint8 b i v
setsb
's unsigned 8-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int8 : bytes -> int -> int -> unit
set_int8 b i v
setsb
's signed 8-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_uint16_ne : bytes -> int -> int -> unit
set_uint16_ne b i v
setsb
's native-endian unsigned 16-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_uint16_be : bytes -> int -> int -> unit
set_uint16_be b i v
setsb
's big-endian unsigned 16-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_uint16_le : bytes -> int -> int -> unit
set_uint16_le b i v
setsb
's little-endian unsigned 16-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int16_ne : bytes -> int -> int -> unit
set_int16_ne b i v
setsb
's native-endian signed 16-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int16_be : bytes -> int -> int -> unit
set_int16_be b i v
setsb
's big-endian signed 16-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int16_le : bytes -> int -> int -> unit
set_int16_le b i v
setsb
's little-endian signed 16-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int32_ne : bytes -> int -> int32 -> unit
set_int32_ne b i v
setsb
's native-endian 32-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int32_be : bytes -> int -> int32 -> unit
set_int32_be b i v
setsb
's big-endian 32-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int32_le : bytes -> int -> int32 -> unit
set_int32_le b i v
setsb
's little-endian 32-bit integer starting at byte indexi
tov
.- since
- 4.08
val set_int64_ne : bytes -> int -> int64 -> unit
set_int64_ne b i v
setsb
's native-endian 64-bit integer starting at byte indexi
tov
.- since
- 4.08