val create : int -> t
create n
returns a fresh buffer, initially empty. Then
parameter is the initial size of the internal byte sequence that holds the buffer contents. That byte sequence is automatically reallocated when more thann
characters are stored in the buffer, but shrinks back ton
characters whenreset
is called. For best performance,n
should be of the same order of magnitude as the number of characters that are expected to be stored in the buffer (for instance, 80 for a buffer that holds one output line). Nothing bad will happen if the buffer grows beyond that limit, however. In doubt, taken = 16
for instance. Ifn
is not between 1 andSys.max_string_length
, it will be clipped to that interval.
val contents : t -> string
Return a copy of the current contents of the buffer. The buffer itself is unchanged.
val to_bytes : t -> bytes
Return a copy of the current contents of the buffer. The buffer itself is unchanged.
- since
- 4.02
val sub : t -> int -> int -> string
Buffer.sub b off len
returns a copy oflen
bytes from the current contents of the bufferb
, starting at offsetoff
.Raise
Invalid_argument
ifsrcoff
andlen
do not designate a valid range ofb
.
val blit : t -> int -> bytes -> int -> int -> unit
Buffer.blit src srcoff dst dstoff len
copieslen
characters from the current contents of the buffersrc
, starting at offsetsrcoff
todst
, starting at characterdstoff
.Raise
Invalid_argument
ifsrcoff
andlen
do not designate a valid range ofsrc
, or ifdstoff
andlen
do not designate a valid range ofdst
.- since
- 3.11.2
val nth : t -> int -> char
Get the n-th character of the buffer. Raise
Invalid_argument
if index out of bounds
val length : t -> int
Return the number of characters currently contained in the buffer.
val clear : t -> unit
Empty the buffer.
val reset : t -> unit
Empty the buffer and deallocate the internal byte sequence holding the buffer contents, replacing it with the initial internal byte sequence of length
n
that was allocated byBuffer.create
n
. For long-lived buffers that may have grown a lot,reset
allows faster reclamation of the space used by the buffer.
val add_char : t -> char -> unit
add_char b c
appends the characterc
at the end of bufferb
.
val add_utf_8_uchar : t -> Stdlib.Uchar.t -> unit
add_utf_8_uchar b u
appends the UTF-8 encoding ofu
at the end of bufferb
.- since
- 4.06.0
val add_utf_16le_uchar : t -> Stdlib.Uchar.t -> unit
add_utf_16le_uchar b u
appends the UTF-16LE encoding ofu
at the end of bufferb
.- since
- 4.06.0
val add_utf_16be_uchar : t -> Stdlib.Uchar.t -> unit
add_utf_16be_uchar b u
appends the UTF-16BE encoding ofu
at the end of bufferb
.- since
- 4.06.0
val add_string : t -> string -> unit
add_string b s
appends the strings
at the end of bufferb
.
val add_bytes : t -> bytes -> unit
add_bytes b s
appends the byte sequences
at the end of bufferb
.- since
- 4.02
val add_substring : t -> string -> int -> int -> unit
add_substring b s ofs len
takeslen
characters from offsetofs
in strings
and appends them at the end of bufferb
.
val add_subbytes : t -> bytes -> int -> int -> unit
add_subbytes b s ofs len
takeslen
characters from offsetofs
in byte sequences
and appends them at the end of bufferb
.- since
- 4.02
val add_substitute : t -> (string -> string) -> string -> unit
add_substitute b f s
appends the string patterns
at the end of bufferb
with substitution. The substitution process looks for variables into the pattern and substitutes each variable name by its value, as obtained by applying the mappingf
to the variable name. Inside the string pattern, a variable name immediately follows a non-escaped$
character and is one of the following:- a non empty sequence of alphanumeric or
_
characters, - an arbitrary sequence of characters enclosed by a pair of matching parentheses or curly brackets. An escaped
$
character is a$
that immediately follows a backslash character; it then stands for a plain$
. RaiseNot_found
if the closing character of a parenthesized variable cannot be found.
- a non empty sequence of alphanumeric or
val add_buffer : t -> t -> unit
add_buffer b1 b2
appends the current contents of bufferb2
at the end of bufferb1
.b2
is not modified.
val add_channel : t -> Stdlib.in_channel -> int -> unit
add_channel b ic n
reads at mostn
characters from the input channelic
and stores them at the end of bufferb
. RaiseEnd_of_file
if the channel contains fewer thann
characters. In this case, the characters are still added to the buffer, so as to avoid loss of data.
val output_buffer : Stdlib.out_channel -> t -> unit
output_buffer oc b
writes the current contents of bufferb
on the output channeloc
.
val truncate : t -> int -> unit
truncate b len
truncates the length ofb
tolen
Note: the internal byte sequence is not shortened. RaiseInvalid_argument
iflen < 0
orlen > length b
.- since
- 4.05.0
Iterators
val to_seq : t -> char Stdlib.Seq.t
Iterate on the buffer, in increasing order. Modification of the buffer during iteration is undefined behavior.
- since
- 4.07
val to_seqi : t -> (int * char) Stdlib.Seq.t
Iterate on the buffer, in increasing order, yielding indices along chars. Modification of the buffer during iteration is undefined behavior.
- since
- 4.07
val add_seq : t -> char Stdlib.Seq.t -> unit
Add chars to the buffer
- since
- 4.07
val of_seq : char Stdlib.Seq.t -> t
Create a buffer from the generator
- since
- 4.07
Binary encoding of integers
val add_uint8 : t -> int -> unit
add_uint8 b i
appends a binary unsigned 8-bit integeri
tob
.- since
- 4.08
val add_int8 : t -> int -> unit
add_int8 b i
appends a binary signed 8-bit integeri
tob
.- since
- 4.08
val add_uint16_ne : t -> int -> unit
add_uint16_ne b i
appends a binary native-endian unsigned 16-bit integeri
tob
.- since
- 4.08
val add_uint16_be : t -> int -> unit
add_uint16_be b i
appends a binary big-endian unsigned 16-bit integeri
tob
.- since
- 4.08
val add_uint16_le : t -> int -> unit
add_uint16_le b i
appends a binary little-endian unsigned 16-bit integeri
tob
.- since
- 4.08
val add_int16_ne : t -> int -> unit
add_int16_ne b i
appends a binary native-endian signed 16-bit integeri
tob
.- since
- 4.08
val add_int16_be : t -> int -> unit
add_int16_be b i
appends a binary big-endian signed 16-bit integeri
tob
.- since
- 4.08
val add_int16_le : t -> int -> unit
add_int16_le b i
appends a binary little-endian signed 16-bit integeri
tob
.- since
- 4.08
val add_int32_ne : t -> int32 -> unit
add_int32_ne b i
appends a binary native-endian 32-bit integeri
tob
.- since
- 4.08
val add_int32_be : t -> int32 -> unit
add_int32_be b i
appends a binary big-endian 32-bit integeri
tob
.- since
- 4.08
val add_int32_le : t -> int32 -> unit
add_int32_le b i
appends a binary little-endian 32-bit integeri
tob
.- since
- 4.08
val add_int64_ne : t -> int64 -> unit
add_int64_ne b i
appends a binary native-endian 64-bit integeri
tob
.- since
- 4.08
val add_int64_be : t -> int64 -> unit
add_int64_be b i
appends a binary big-endian 64-bit integeri
tob
.- since
- 4.08
val add_int64_le : t -> int64 -> unit
add_int64_ne b i
appends a binary little-endian 64-bit integeri
tob
.- since
- 4.08