1 (***********************************************************************)
5 (* Pierre Weis and Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
7 (* Copyright 1999 Institut National de Recherche en Informatique et *)
8 (* en Automatique. All rights reserved. This file is distributed *)
9 (* under the terms of the GNU Library General Public License, with *)
10 (* the special exception on linking described in file ../LICENSE. *)
12 (***********************************************************************)
14 (* $Id: buffer.mli 9340 2009-09-16 15:52:46Z xclerc $ *)
16 (** Extensible string buffers.
18 This module implements string buffers that automatically expand
19 as necessary. It provides accumulative concatenation of strings
20 in quasi-linear time (instead of quadratic time when strings are
21 concatenated pairwise).
25 (** The abstract type of buffers. *)
28 (** [create n] returns a fresh buffer, initially empty.
29 The [n] parameter is the initial size of the internal string
30 that holds the buffer contents. That string is automatically
31 reallocated when more than [n] characters are stored in the buffer,
32 but shrinks back to [n] characters when [reset] is called.
33 For best performance, [n] should be of the same order of magnitude
34 as the number of characters that are expected to be stored in
35 the buffer (for instance, 80 for a buffer that holds one output
36 line). Nothing bad will happen if the buffer grows beyond that
37 limit, however. In doubt, take [n = 16] for instance.
38 If [n] is not between 1 and {!Sys.max_string_length}, it will
39 be clipped to that interval. *)
41 val contents : t -> string
42 (** Return a copy of the current contents of the buffer.
43 The buffer itself is unchanged. *)
45 val sub : t -> int -> int -> string
46 (** [Buffer.sub b off len] returns (a copy of) the substring of the
47 current contents of the buffer [b] starting at offset [off] of length
48 [len] bytes. May raise [Invalid_argument] if out of bounds request. The
49 buffer itself is unaffected. *)
51 val blit : t -> int -> string -> int -> int -> unit
52 (** [Buffer.blit src srcoff dst dstoff len] copies [len] characters from
53 the current contents of the buffer [src], starting at offset [srcoff]
54 to string [dst], starting at character [dstoff].
56 Raise [Invalid_argument] if [srcoff] and [len] do not designate a valid
57 substring of [src], or if [dstoff] and [len] do not designate a valid
58 substring of [dst]. *)
60 val nth : t -> int -> char
61 (** get the n-th character of the buffer. Raise [Invalid_argument] if
62 index out of bounds *)
65 (** Return the number of characters currently contained in the buffer. *)
68 (** Empty the buffer. *)
71 (** Empty the buffer and deallocate the internal string holding the
72 buffer contents, replacing it with the initial internal string
73 of length [n] that was allocated by {!Buffer.create} [n].
74 For long-lived buffers that may have grown a lot, [reset] allows
75 faster reclamation of the space used by the buffer. *)
77 val add_char : t -> char -> unit
78 (** [add_char b c] appends the character [c] at the end of the buffer [b]. *)
80 val add_string : t -> string -> unit
81 (** [add_string b s] appends the string [s] at the end of the buffer [b]. *)
83 val add_substring : t -> string -> int -> int -> unit
84 (** [add_substring b s ofs len] takes [len] characters from offset
85 [ofs] in string [s] and appends them at the end of the buffer [b]. *)
87 val add_substitute : t -> (string -> string) -> string -> unit
88 (** [add_substitute b f s] appends the string pattern [s] at the end
89 of the buffer [b] with substitution.
90 The substitution process looks for variables into
91 the pattern and substitutes each variable name by its value, as
92 obtained by applying the mapping [f] to the variable name. Inside the
93 string pattern, a variable name immediately follows a non-escaped
94 [$] character and is one of the following:
95 - a non empty sequence of alphanumeric or [_] characters,
96 - an arbitrary sequence of characters enclosed by a pair of
97 matching parentheses or curly brackets.
98 An escaped [$] character is a [$] that immediately follows a backslash
99 character; it then stands for a plain [$].
100 Raise [Not_found] if the closing character of a parenthesized variable
103 val add_buffer : t -> t -> unit
104 (** [add_buffer b1 b2] appends the current contents of buffer [b2]
105 at the end of buffer [b1]. [b2] is not modified. *)
107 val add_channel : t -> in_channel -> int -> unit
108 (** [add_channel b ic n] reads exactly [n] character from the
109 input channel [ic] and stores them at the end of buffer [b].
110 Raise [End_of_file] if the channel contains fewer than [n]
113 val output_buffer : out_channel -> t -> unit
114 (** [output_buffer oc b] writes the current contents of buffer [b]
115 on the output channel [oc]. *)