val create : int -> tcreate 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, 80 for 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 between 1 andSys.max_string_length, it will be clipped to that interval.
val contents : t -> stringReturn a copy of the current contents of the buffer. The buffer itself is unchanged.
val to_bytes : t -> bytesReturn a copy of the current contents of the buffer. The buffer itself is unchanged.
- since
- 4.02
val sub : t -> int -> int -> stringBuffer.sub b off lenreturns a copy oflenbytes from the current contents of the bufferb, starting at offsetoff.Raise
Invalid_argumentifsrcoffandlendo not designate a valid range ofb.
val blit : t -> int -> bytes -> int -> int -> unitBuffer.blit src srcoff dst dstoff lencopieslencharacters from the current contents of the buffersrc, starting at offsetsrcofftodst, starting at characterdstoff.Raise
Invalid_argumentifsrcoffandlendo not designate a valid range ofsrc, or ifdstoffandlendo not designate a valid range ofdst.- since
- 3.11.2
val nth : t -> int -> charGet the n-th character of the buffer. Raise
Invalid_argumentif index out of bounds
val length : t -> intReturn the number of characters currently contained in the buffer.
val clear : t -> unitEmpty the buffer.
val reset : t -> unitEmpty the buffer and deallocate the internal byte sequence holding the buffer contents, replacing it with the initial internal byte sequence of length
nthat was allocated byBuffer.createn. For long-lived buffers that may have grown a lot,resetallows faster reclamation of the space used by the buffer.
val add_char : t -> char -> unitadd_char b cappends the charactercat the end of bufferb.
val add_utf_8_uchar : t -> Stdlib.Uchar.t -> unitadd_utf_8_uchar b uappends the UTF-8 encoding ofuat the end of bufferb.- since
- 4.06.0
val add_utf_16le_uchar : t -> Stdlib.Uchar.t -> unitadd_utf_16le_uchar b uappends the UTF-16LE encoding ofuat the end of bufferb.- since
- 4.06.0
val add_utf_16be_uchar : t -> Stdlib.Uchar.t -> unitadd_utf_16be_uchar b uappends the UTF-16BE encoding ofuat the end of bufferb.- since
- 4.06.0
val add_string : t -> string -> unitadd_string b sappends the stringsat the end of bufferb.
val add_bytes : t -> bytes -> unitadd_bytes b sappends the byte sequencesat the end of bufferb.- since
- 4.02
val add_substring : t -> string -> int -> int -> unitadd_substring b s ofs lentakeslencharacters from offsetofsin stringsand appends them at the end of bufferb.
val add_subbytes : t -> bytes -> int -> int -> unitadd_subbytes b s ofs lentakeslencharacters from offsetofsin byte sequencesand appends them at the end of bufferb.- since
- 4.02
val add_substitute : t -> (string -> string) -> string -> unitadd_substitute b f sappends the string patternsat the end of bufferbwith substitution. The substitution process looks for variables into the pattern and substitutes each variable name by its value, as obtained by applying the mappingfto 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_foundif the closing character of a parenthesized variable cannot be found.
- a non empty sequence of alphanumeric or
val add_buffer : t -> t -> unitadd_buffer b1 b2appends the current contents of bufferb2at the end of bufferb1.b2is not modified.
val add_channel : t -> Stdlib.in_channel -> int -> unitadd_channel b ic nreads at mostncharacters from the input channelicand stores them at the end of bufferb. RaiseEnd_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.
val output_buffer : Stdlib.out_channel -> t -> unitoutput_buffer oc bwrites the current contents of bufferbon the output channeloc.
val truncate : t -> int -> unittruncate b lentruncates the length ofbtolenNote: the internal byte sequence is not shortened. RaiseInvalid_argumentiflen < 0orlen > length b.- since
- 4.05.0
Iterators
val to_seq : t -> char Stdlib.Seq.tIterate 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.tIterate 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 -> unitAdd chars to the buffer
- since
- 4.07
val of_seq : char Stdlib.Seq.t -> tCreate a buffer from the generator
- since
- 4.07
Binary encoding of integers
val add_uint8 : t -> int -> unitadd_uint8 b iappends a binary unsigned 8-bit integeritob.- since
- 4.08
val add_int8 : t -> int -> unitadd_int8 b iappends a binary signed 8-bit integeritob.- since
- 4.08
val add_uint16_ne : t -> int -> unitadd_uint16_ne b iappends a binary native-endian unsigned 16-bit integeritob.- since
- 4.08
val add_uint16_be : t -> int -> unitadd_uint16_be b iappends a binary big-endian unsigned 16-bit integeritob.- since
- 4.08
val add_uint16_le : t -> int -> unitadd_uint16_le b iappends a binary little-endian unsigned 16-bit integeritob.- since
- 4.08
val add_int16_ne : t -> int -> unitadd_int16_ne b iappends a binary native-endian signed 16-bit integeritob.- since
- 4.08
val add_int16_be : t -> int -> unitadd_int16_be b iappends a binary big-endian signed 16-bit integeritob.- since
- 4.08
val add_int16_le : t -> int -> unitadd_int16_le b iappends a binary little-endian signed 16-bit integeritob.- since
- 4.08
val add_int32_ne : t -> int32 -> unitadd_int32_ne b iappends a binary native-endian 32-bit integeritob.- since
- 4.08
val add_int32_be : t -> int32 -> unitadd_int32_be b iappends a binary big-endian 32-bit integeritob.- since
- 4.08
val add_int32_le : t -> int32 -> unitadd_int32_le b iappends a binary little-endian 32-bit integeritob.- since
- 4.08
val add_int64_ne : t -> int64 -> unitadd_int64_ne b iappends a binary native-endian 64-bit integeritob.- since
- 4.08
val add_int64_be : t -> int64 -> unitadd_int64_be b iappends a binary big-endian 64-bit integeritob.- since
- 4.08
val add_int64_le : t -> int64 -> unitadd_int64_ne b iappends a binary little-endian 64-bit integeritob.- since
- 4.08