Stdlib.Buffer(3)
πΈ NAME πΈ
okay bestie, welcome to Stdlib.Buffer(3) from the OCaml library manual β¨
Stdlib.Buffer - no description β yep, that's literally what it says, we move π
π§ Module π§
Module Buffer : (module Stdlib__Buffer)
π Documentation π
π Capacity and reallocation strategy π
Internally, a buffer uses a backing store (a byte sequence) whose size is called the capacity, and that capacity is greater than or equal to the number of characters stored in the buffer (aka its length) π«Ά
The implementation uses a standard exponential reallocation strategy, which guarantees amortized constant-time operation. In particular, the total size of all backing stores allocated over the lifetime of a buffer is at worst proportional to the total number of characters added. no cap, that's the whole point β¨
Buffer.clear preserves the current backing store (so no memory is freed), while Buffer.reset frees the current backing store, releasing memory at the price of potentially having to reallocate later on. it's giving tradeoff energy π
π¨ Unsynchronized accesses π¨
Unsynchronized accesses to a buffer may lead to an invalid buffer state. Thus, concurrent accesses to a buffer must be synchronized (for instance with a Mutex.t ). bestie please do not rawdog concurrent buffer access, it will not slay π
type tβ The abstract type of buffers πval create : int -> tβcreate nreturns a fresh buffer, initially empty. Thenparameter is the initial size of the internal byte sequence that holds the buffer contents. That byte sequence is automatically reallocated when more thanncharacters are stored in the buffer, but shrinks back toncharacters whenresetis called. For best performance,nshould be of the same order of magnitude as the number of characters that are expected to be stored in the buffer (for instance,80for a buffer that holds one output line). Nothing bad will happen if the buffer grows beyond that limit, however. In doubt, taken = 16for instance. Ifnis not between1andSys.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 lenreturns a copy oflenbytes from the current contents of the bufferb, starting at offsetoff.
Raises Invalid_argument if off and len do not designate a valid range of b .
val blit : t -> int -> bytes -> int -> int -> unitβBuffer.blit src srcoff dst dstoff lencopieslencharacters from the current contents of the buffersrc, starting at offsetsrcofftodst, starting at characterdstoff.
Since 3.11.2
Raises Invalid_argument if srcoff and len do not designate a valid range of src , or if dstoff and len do not designate a valid range of dst .
val nth : t -> int -> charβ Get then-th character of the buffer.
Raises 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. that's it, that's the function π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 lengthnthat was allocated byBuffer.create n. For long-lived buffers that may have grown a lot,resetallows faster reclamation of the space used by the buffer π¦val output_buffer : out_channel -> t -> unitβoutput_buffer oc bwrites the current contents of bufferbon the output channeloc.val truncate : t -> int -> unitβtruncate b lentruncates the length ofbtolen. Note: the internal byte sequence is not shortened.
Since 4.05
Raises Invalid_argument if len < 0 or len > length b .
π Appending π
Note: alladd_*operations can raiseFailureif the internal byte sequence of the buffer would need to grow beyondSys.max_string_length. keep that in mind, bestie π
val add_char : t -> char -> unitβadd_char b cappends the charactercat the end of bufferb.val add_utf_8_uchar : t -> Uchar.t -> unitβadd_utf_8_uchar b uappends the UTF-8 encoding ofuat the end of bufferb.
Since 4.06 π§
val add_utf_16le_uchar : t -> Uchar.t -> unitβadd_utf_16le_uchar b uappends the UTF-16LE encoding ofuat the end of bufferb.
Since 4.06
val add_utf_16be_uchar : t -> Uchar.t -> unitβadd_utf_16be_uchar b uappends the UTF-16BE encoding ofuat the end of bufferb.
Since 4.06
val add_string : t -> string -> unitβadd_string b sappends the stringsat the end of bufferb.val add_bytes : t -> bytes -> unitβadd_bytes b sappends the byte sequencesat the end of bufferb.
Since 4.02
val add_substring : t -> string -> int -> int -> unitβadd_substring b s ofs lentakeslencharacters from offsetofsin stringsand appends them at the end of bufferb.
Raises Invalid_argument if ofs and len do not designate a valid range of s .
val add_subbytes : t -> bytes -> int -> int -> unitβadd_subbytes b s ofs lentakeslencharacters from offsetofsin byte sequencesand appends them at the end of bufferb.
Since 4.02
Raises Invalid_argument if ofs and len do not designate a valid range of s .
val add_substitute : t -> (string -> string) -> string -> unitβadd_substitute b f sappends the string patternsat the end of bufferbwith substitution. The substitution process looks for variable references in the pattern and substitutes each variable reference with its value, as obtained by applying the mappingfto the variable name. Inside the string pattern, a variable reference is a non-escaped$immediately followed by a variable name, which is one of the following:-anon empty sequence of alphanumeric or_characters,-anarbitrary sequence of characters enclosed by a pair of matching parentheses or curly brackets. An escaped$character is a$that immediately follows a backslash character; the two characters together stand for a plain$.
val add_buffer : t -> t -> unitβadd_buffer b1 b2appends the current contents of bufferb2at the end of bufferb1.b2is not modified.val add_channel : t -> in_channel -> int -> unitβadd_channel b ic nreads at mostncharacters from the input channelicand stores them at the end of bufferb.
Raises End_of_file if the channel contains fewer than n characters. In this case, the characters are still added to the buffer, so as to avoid loss of data.
Raises Invalid_argument if len < 0 or len > Sys.max_string_length .
π¦ Buffers and Sequences π¦
val to_seq : t -> char Seq.tβ Iterate on the buffer, in increasing order.
The behavior is not specified if the buffer is modified during iteration.
Since 4.07 β
val to_seqi : t -> (int * char) Seq.tβ Iterate on the buffer, in increasing order, yielding indices along chars.
The behavior is not specified if the buffer is modified during iteration.
Since 4.07
val add_seq : t -> char Seq.t -> unitβ Add chars to the buffer.
Since 4.07
val of_seq : char Seq.t -> tβ Create a buffer from the generator.
Since 4.07
π₯ Binary encoding of integers π₯
The functions in this section append binary encodings of integers to buffers π
Little-endian (resp. big-endian) encoding means that least (resp. most) significant bytes are stored first. Big-endian is also known as network byte order. Native-endian encoding is either little-endian or big-endian depending on Sys.big_endian .
32 -bit and 64 -bit integers are represented by the int32 and int64 types, which can be interpreted either as signed or unsigned numbers.
8 -bit and 16 -bit integers are represented by the int type, which has more bits than the binary encoding. Functions that encode these values truncate their inputs to their least significant bytes. okayyy truncation, we see you π
val add_uint8 : t -> int -> unitβadd_uint8 b iappends a binary unsigned 8-bit integeritob.
Since 4.08
val add_int8 : t -> int -> unitβadd_int8 b iappends a binary signed 8-bit integeritob.
Since 4.08
val add_uint16_ne : t -> int -> unitβadd_uint16_ne b iappends a binary native-endian unsigned 16-bit integeritob.
Since 4.08
val add_uint16_be : t -> int -> unitβadd_uint16_be b iappends a binary big-endian unsigned 16-bit integeritob.
Since 4.08
val add_uint16_le : t -> int -> unitβadd_uint16_le b iappends a binary little-endian unsigned 16-bit integeritob.
Since 4.08
val add_int16_ne : t -> int -> unitβadd_int16_ne b iappends a binary native-endian signed 16-bit integeritob.
Since 4.08
val add_int16_be : t -> int -> unitβadd_int16_be b iappends a binary big-endian signed 16-bit integeritob.
Since 4.08
val add_int16_le : t -> int -> unitβadd_int16_le b iappends a binary little-endian signed 16-bit integeritob.
Since 4.08
val add_int32_ne : t -> int32 -> unitβadd_int32_ne b iappends a binary native-endian 32-bit integeritob.
Since 4.08
val add_int32_be : t -> int32 -> unitβadd_int32_be b iappends a binary big-endian 32-bit integeritob.
Since 4.08
val add_int32_le : t -> int32 -> unitβadd_int32_le b iappends a binary little-endian 32-bit integeritob.
Since 4.08
val add_int64_ne : t -> int64 -> unitβadd_int64_ne b iappends a binary native-endian 64-bit integeritob.
Since 4.08
val add_int64_be : t -> int64 -> unitβadd_int64_be b iappends a binary big-endian 64-bit integeritob.
Since 4.08
val add_int64_le : t -> int64 -> unitβadd_int64_le b iappends a binary little-endian 64-bit integeritob.
Since 4.08
big kiss, this page was served by OCamldoc on 2026-06-22, for Stdlib.Buffer(3) π«Άπ periodt.
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms