1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
Module Buffer: extensible string buffers
</TITLE>
</HEAD>
<BODY >
<A HREF="manual032.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual034.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H2>17.3 Module <TT>Buffer</TT>: extensible string buffers</H2><A NAME="s:Buffer"></A>
<A NAME="@manual183"></A><BLOCKQUOTE>
This module implements string buffers that automatically expand
as necessary. It provides accumulative concatenation of strings
in quasi-linear time (instead of quadratic time when strings are
concatenated pairwise).
</BLOCKQUOTE>
<PRE>
type t
</PRE>
<BLOCKQUOTE>
The abstract type of buffers.
</BLOCKQUOTE>
<PRE>
val create : int -> t
</PRE>
<A NAME="@manual184"></A><BLOCKQUOTE>
<CODE>create n</CODE> returns a fresh buffer, initially empty.
The <CODE>n</CODE> parameter is the initial size of the internal string
that holds the buffer contents. That string is automatically
reallocated when more than <CODE>n</CODE> characters are stored in the buffer,
but shrinks back to <CODE>n</CODE> characters when <CODE>reset</CODE> is called.
For best performance, <CODE>n</CODE> 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, take <CODE>n = 16</CODE> for instance.
If <CODE>n</CODE> is not between 1 and <CODE>Sys.max_string_length</CODE>, it will
be clipped to that interval.
</BLOCKQUOTE>
<PRE>
val contents : t -> string
</PRE>
<A NAME="@manual185"></A><BLOCKQUOTE>
Return a copy of the current contents of the buffer.
The buffer itself is unchanged.
</BLOCKQUOTE>
<PRE>
val length : t -> int
</PRE>
<A NAME="@manual186"></A><BLOCKQUOTE>
Return the number of characters currently contained in the buffer.
</BLOCKQUOTE>
<PRE>
val clear : t -> unit
</PRE>
<A NAME="@manual187"></A><BLOCKQUOTE>
Empty the buffer.
</BLOCKQUOTE>
<PRE>
val reset : t -> unit
</PRE>
<A NAME="@manual188"></A><BLOCKQUOTE>
Empty the buffer and deallocate the internal string holding the
buffer contents, replacing it with the initial internal string
of length <CODE>n</CODE> that was allocated by <CODE>create n</CODE>.
For long-lived buffers that may have grown a lot, <CODE>reset</CODE> allows
faster reclaimation of the space used by the buffer.
</BLOCKQUOTE>
<PRE>
val add_char : t -> char -> unit
</PRE>
<A NAME="@manual189"></A><BLOCKQUOTE>
<CODE>add_char b c</CODE> appends the character <CODE>c</CODE> at the end of
the buffer <CODE>b</CODE>.
</BLOCKQUOTE>
<PRE>
val add_string : t -> string -> unit
</PRE>
<A NAME="@manual190"></A><BLOCKQUOTE>
<CODE>add_string b s</CODE> appends the string <CODE>s</CODE> at the end of
the buffer <CODE>b</CODE>.
</BLOCKQUOTE>
<PRE>
val add_substring : t -> string -> int -> int -> unit
</PRE>
<A NAME="@manual191"></A><BLOCKQUOTE>
<CODE>add_substring b s ofs len</CODE> takes <CODE>len</CODE> characters from offset
<CODE>ofs</CODE> in string <CODE>s</CODE> and appends them at the end of the buffer <CODE>b</CODE>.
</BLOCKQUOTE>
<PRE>
val add_buffer : t -> t -> unit
</PRE>
<A NAME="@manual192"></A><BLOCKQUOTE>
<CODE>add_buffer b1 b2</CODE> appends the current contents of buffer <CODE>b2</CODE>
at the end of buffer <CODE>b1</CODE>. <CODE>b2</CODE> is not modified.
</BLOCKQUOTE>
<PRE>
val add_channel : t -> in_channel -> int -> unit
</PRE>
<A NAME="@manual193"></A><BLOCKQUOTE>
<CODE>add_channel b ic n</CODE> reads exactly <CODE>n</CODE> character from the
input channel <CODE>ic</CODE> and stores them at the end of buffer <CODE>b</CODE>.
Raise <CODE>End_of_file</CODE> if the channel contains fewer than <CODE>n</CODE>
characters.
</BLOCKQUOTE>
<PRE>
val output_buffer : out_channel -> t -> unit
</PRE>
<A NAME="@manual194"></A><BLOCKQUOTE>
<CODE>output_buffer oc b</CODE> writes the current contents of buffer <CODE>b</CODE>
on the output channel <CODE>oc</CODE>.
</BLOCKQUOTE>
<HR>
<A HREF="manual032.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual034.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|