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