Buffer(3)
๐ Buffer(3) โ OCaml library, but make it group chat ๐
okay so bestie, this is Buffer(3) from the OCaml library, and it is literally the extendo-buffer of your dreams โจ
๐ NAME ๐
Buffer โ Extensible buffers. periodt ๐
๐ฆ Module ๐ฆ
The module is Buffer, and the docs go Module Buffer : sig end โ iconic, no notes ๐
๐ Documentation ๐
Extensible buffers. ๐ฆ
This module implements buffers that automatically expand as necessary. It gives you accumulative concatenation of strings in linear time โ instead of quadratic time when strings are concatenated pairwise. highkey a glow-up fr fr ๐ For example:
let concat_strings ss =
let b = Buffer.create 16 in
List.iter (Buffer.add_string b) ss;
Buffer.contents b
โ ๏ธ Alert unsynchronized_access. Unsynchronized accesses to buffers are a programming error. not me warning you bestie ๐ญ
โจ Capacity and reallocation strategy โจ
Internally, a buffer uses a backing store โ a byte sequence โ whose size, called the capacity, is greater or equal to the number of characters stored in the buffer (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. slay, amortized ๐
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. tradeoffs, bestie ๐ธ
๐ 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). ๐ซถ
type tโ The abstract type of buffers. main character energy ngl ๐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. no cap, just a copy ๐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. RaisesInvalid_argumentifoffandlendo not designate a valid range ofb. ๐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 RaisesInvalid_argumentifsrcoffandlendo not designate a valid range ofsrc, or ifdstoffandlendo not designate a valid range ofdst. ๐val nth : t -> int -> charโ Get then-th character of the buffer. RaisesInvalid_argumentif 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. (Remember: it preserves the backing store, so no memory is freed.) ๐งผ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 RaisesInvalid_argumentiflen < 0orlen > length b. โ๏ธ
๐งต Appending ๐งต
Note: alladd_*operations can raiseFailureif the internal byte sequence of the buffer would need to grow beyondSys.max_string_length. ๐ซ
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. RaisesInvalid_argumentifofsandlendo not designate a valid range ofs. ๐ธ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 RaisesInvalid_argumentifofsandlendo not designate a valid range ofs. ๐ง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. RaisesEnd_of_fileif the channel contains fewer thanncharacters. In this case, the characters are still added to the buffer, so as to avoid loss of data. RaisesInvalid_argumentiflen < 0orlen > 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. ๐โจ
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 ๐
๐
this cutie was served by OCamldoc on 2026-06-22 โ source/version/date all accounted for, bestie. bye! ๐ซถโจ
๐ฆ 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