1Buffer(3)                        OCaml library                       Buffer(3)
2
3
4

NAME

6       Buffer - Extensible buffers.
7

Module

9       Module   Buffer
10

Documentation

12       Module Buffer
13        : sig end
14
15
16       Extensible buffers.
17
18       This  module implements buffers that automatically expand as necessary.
19       It provides accumulative concatenation of strings in linear  time  (in‐
20       stead  of  quadratic  time when strings are concatenated pairwise). For
21       example:
22
23
24            let concat_strings ss =
25              let b = Buffer.create 16 in
26                List.iter (Buffer.add_string b) ss;
27                Buffer.contents b
28
29
30
31
32       Alert unsynchronized_access.  Unsynchronized accesses to buffers are  a
33       programming error.
34
35
36
37
38
39
40
41       Unsynchronized accesses
42
43       Unsynchronized  accesses  to  a  buffer  may  lead to an invalid buffer
44       state.  Thus, concurrent accesses to a buffer must be synchronized (for
45       instance with a Mutex.t ).
46
47       type t
48
49
50       The abstract type of buffers.
51
52
53
54       val create : int -> t
55
56
57       create  n  returns a fresh buffer, initially empty.  The n parameter is
58       the initial size of the internal byte sequence that  holds  the  buffer
59       contents.  That  byte  sequence  is automatically reallocated when more
60       than n characters are stored in the buffer, but shrinks back to n char‐
61       acters  when reset is called.  For best performance, n should be of the
62       same order of magnitude as the number of characters that  are  expected
63       to  be  stored  in the buffer (for instance, 80 for a buffer that holds
64       one output line).  Nothing bad will happen if the buffer  grows  beyond
65       that  limit,  however. In doubt, take n = 16 for instance.  If n is not
66       between 1 and Sys.max_string_length , it will be clipped to that inter‐
67       val.
68
69
70
71       val contents : t -> string
72
73       Return a copy of the current contents of the buffer.  The buffer itself
74       is unchanged.
75
76
77
78       val to_bytes : t -> bytes
79
80       Return a copy of the current contents of the buffer.  The buffer itself
81       is unchanged.
82
83
84       Since 4.02
85
86
87
88       val sub : t -> int -> int -> string
89
90
91       Buffer.sub  b off len returns a copy of len bytes from the current con‐
92       tents of the buffer b , starting at offset off .
93
94
95       Raises Invalid_argument if off and len do not designate a  valid  range
96       of b .
97
98
99
100       val blit : t -> int -> bytes -> int -> int -> unit
101
102
103       Buffer.blit  src  srcoff  dst dstoff len copies len characters from the
104       current contents of the buffer src , starting at offset srcoff to dst ,
105       starting at character dstoff .
106
107
108       Since 3.11.2
109
110
111       Raises  Invalid_argument  if  srcoff  and  len do not designate a valid
112       range of src , or if dstoff and len do not designate a valid  range  of
113       dst .
114
115
116
117       val nth : t -> int -> char
118
119       Get the n-th character of the buffer.
120
121
122       Raises Invalid_argument if index out of bounds
123
124
125
126       val length : t -> int
127
128       Return the number of characters currently contained in the buffer.
129
130
131
132       val clear : t -> unit
133
134       Empty the buffer.
135
136
137
138       val reset : t -> unit
139
140       Empty  the buffer and deallocate the internal byte sequence holding the
141       buffer contents, replacing it with the initial internal  byte  sequence
142       of  length  n  that  was allocated by Buffer.create n .  For long-lived
143       buffers that may have grown a lot, reset allows faster  reclamation  of
144       the space used by the buffer.
145
146
147
148       val output_buffer : out_channel -> t -> unit
149
150
151       output_buffer  oc b writes the current contents of buffer b on the out‐
152       put channel oc .
153
154
155
156       val truncate : t -> int -> unit
157
158
159       truncate b len truncates the length of b to len Note: the internal byte
160       sequence is not shortened.
161
162
163       Since 4.05.0
164
165
166       Raises Invalid_argument if len < 0 or len > length b .
167
168
169
170
171   Appending
172       Note:  all  add_* operations can raise Failure if the internal byte se‐
173       quence of the buffer would need to grow beyond Sys.max_string_length .
174
175       val add_char : t -> char -> unit
176
177
178       add_char b c appends the character c at the end of buffer b .
179
180
181
182       val add_utf_8_uchar : t -> Uchar.t -> unit
183
184
185       add_utf_8_uchar b u appends the UTF-8 encoding of u at the end of  buf‐
186       fer b .
187
188
189       Since 4.06.0
190
191
192
193       val add_utf_16le_uchar : t -> Uchar.t -> unit
194
195
196       add_utf_16le_uchar b u appends the UTF-16LE encoding of u at the end of
197       buffer b .
198
199
200       Since 4.06.0
201
202
203
204       val add_utf_16be_uchar : t -> Uchar.t -> unit
205
206
207       add_utf_16be_uchar b u appends the UTF-16BE encoding of u at the end of
208       buffer b .
209
210
211       Since 4.06.0
212
213
214
215       val add_string : t -> string -> unit
216
217
218       add_string b s appends the string s at the end of buffer b .
219
220
221
222       val add_bytes : t -> bytes -> unit
223
224
225       add_bytes b s appends the byte sequence s at the end of buffer b .
226
227
228       Since 4.02
229
230
231
232       val add_substring : t -> string -> int -> int -> unit
233
234
235       add_substring  b  s  ofs  len  takes  len characters from offset ofs in
236       string s and appends them at the end of buffer b .
237
238
239       Raises Invalid_argument if ofs and len do not designate a  valid  range
240       of s .
241
242
243
244       val add_subbytes : t -> bytes -> int -> int -> unit
245
246
247       add_subbytes  b  s ofs len takes len characters from offset ofs in byte
248       sequence s and appends them at the end of buffer b .
249
250
251       Since 4.02
252
253
254       Raises Invalid_argument if ofs and len do not designate a  valid  range
255       of s .
256
257
258
259       val add_substitute : t -> (string -> string) -> string -> unit
260
261
262       add_substitute  b f s appends the string pattern s at the end of buffer
263       b with substitution.  The substitution process looks for variables into
264       the  pattern  and  substitutes  each variable name by its value, as ob‐
265       tained by applying the mapping f  to  the  variable  name.  Inside  the
266       string  pattern,  a  variable  name immediately follows a non-escaped $
267       character and is one of the following:
268
269       -a non empty sequence of alphanumeric or _ characters,
270
271       -an arbitrary sequence of characters enclosed by  a  pair  of  matching
272       parentheses  or curly brackets.  An escaped $ character is a $ that im‐
273       mediately follows a backslash character; it then stands for a plain $ .
274
275
276
277       Raises Not_found if the closing character of a  parenthesized  variable
278       cannot be found.
279
280
281
282       val add_buffer : t -> t -> unit
283
284
285       add_buffer  b1  b2 appends the current contents of buffer b2 at the end
286       of buffer b1 .  b2 is not modified.
287
288
289
290       val add_channel : t -> in_channel -> int -> unit
291
292
293       add_channel b ic n reads at most n characters from the input channel ic
294       and stores them at the end of buffer b .
295
296
297       Raises  End_of_file if the channel contains fewer than n characters. In
298       this case, the characters are still added to the buffer, so as to avoid
299       loss of data.
300
301
302       Raises Invalid_argument if len < 0 or len > Sys.max_string_length .
303
304
305
306
307   Buffers and Sequences
308       val to_seq : t -> char Seq.t
309
310       Iterate on the buffer, in increasing order.
311
312       The  behavior  is not specified if the buffer is modified during itera‐
313       tion.
314
315
316       Since 4.07
317
318
319
320       val to_seqi : t -> (int * char) Seq.t
321
322       Iterate on the buffer, in  increasing  order,  yielding  indices  along
323       chars.
324
325       The  behavior  is not specified if the buffer is modified during itera‐
326       tion.
327
328
329       Since 4.07
330
331
332
333       val add_seq : t -> char Seq.t -> unit
334
335       Add chars to the buffer
336
337
338       Since 4.07
339
340
341
342       val of_seq : char Seq.t -> t
343
344       Create a buffer from the generator
345
346
347       Since 4.07
348
349
350
351
352   Binary encoding of integers
353       The functions in this section append binary encodings  of  integers  to
354       buffers.
355
356       Little-endian (resp. big-endian) encoding means that least (resp. most)
357       significant bytes are stored first.  Big-endian is also known  as  net‐
358       work  byte  order.   Native-endian  encoding is either little-endian or
359       big-endian depending on Sys.big_endian .
360
361       32-bit and 64-bit integers are  represented  by  the  int32  and  int64
362       types, which can be interpreted either as signed or unsigned numbers.
363
364       8-bit  and  16-bit  integers are represented by the int type, which has
365       more bits than the binary encoding.  Functions that encode these values
366       truncate their inputs to their least significant bytes.
367
368       val add_uint8 : t -> int -> unit
369
370
371       add_uint8 b i appends a binary unsigned 8-bit integer i to b .
372
373
374       Since 4.08
375
376
377
378       val add_int8 : t -> int -> unit
379
380
381       add_int8 b i appends a binary signed 8-bit integer i to b .
382
383
384       Since 4.08
385
386
387
388       val add_uint16_ne : t -> int -> unit
389
390
391       add_uint16_ne  b i appends a binary native-endian unsigned 16-bit inte‐
392       ger i to b .
393
394
395       Since 4.08
396
397
398
399       val add_uint16_be : t -> int -> unit
400
401
402       add_uint16_be b i appends a binary big-endian unsigned 16-bit integer i
403       to b .
404
405
406       Since 4.08
407
408
409
410       val add_uint16_le : t -> int -> unit
411
412
413       add_uint16_le  b i appends a binary little-endian unsigned 16-bit inte‐
414       ger i to b .
415
416
417       Since 4.08
418
419
420
421       val add_int16_ne : t -> int -> unit
422
423
424       add_int16_ne b i appends a binary native-endian signed 16-bit integer i
425       to b .
426
427
428       Since 4.08
429
430
431
432       val add_int16_be : t -> int -> unit
433
434
435       add_int16_be b i appends a binary big-endian signed 16-bit integer i to
436       b .
437
438
439       Since 4.08
440
441
442
443       val add_int16_le : t -> int -> unit
444
445
446       add_int16_le b i appends a binary little-endian signed 16-bit integer i
447       to b .
448
449
450       Since 4.08
451
452
453
454       val add_int32_ne : t -> int32 -> unit
455
456
457       add_int32_ne b i appends a binary native-endian 32-bit integer i to b .
458
459
460       Since 4.08
461
462
463
464       val add_int32_be : t -> int32 -> unit
465
466
467       add_int32_be b i appends a binary big-endian 32-bit integer i to b .
468
469
470       Since 4.08
471
472
473
474       val add_int32_le : t -> int32 -> unit
475
476
477       add_int32_le b i appends a binary little-endian 32-bit integer i to b .
478
479
480       Since 4.08
481
482
483
484       val add_int64_ne : t -> int64 -> unit
485
486
487       add_int64_ne b i appends a binary native-endian 64-bit integer i to b .
488
489
490       Since 4.08
491
492
493
494       val add_int64_be : t -> int64 -> unit
495
496
497       add_int64_be b i appends a binary big-endian 64-bit integer i to b .
498
499
500       Since 4.08
501
502
503
504       val add_int64_le : t -> int64 -> unit
505
506
507       add_int64_ne b i appends a binary little-endian 64-bit integer i to b .
508
509
510       Since 4.08
511
512
513
514
515
516OCamldoc                          2023-07-20                         Buffer(3)
Impressum