1BytesLabels(3) OCaml library BytesLabels(3)
2
3
4
6 BytesLabels - Byte sequence operations.
7
9 Module BytesLabels
10
12 Module BytesLabels
13 : sig end
14
15
16 Byte sequence operations.
17
18
19 Since 4.02.0
20
21
22
23
24
25
26 val length : bytes -> int
27
28 Return the length (number of bytes) of the argument.
29
30
31
32 val get : bytes -> int -> char
33
34
35 get s n returns the byte at index n in argument s .
36
37 Raise Invalid_argument if n is not a valid index in s .
38
39
40
41 val set : bytes -> int -> char -> unit
42
43
44 set s n c modifies s in place, replacing the byte at index n with c .
45
46 Raise Invalid_argument if n is not a valid index in s .
47
48
49
50 val create : int -> bytes
51
52
53 create n returns a new byte sequence of length n . The sequence is
54 uninitialized and contains arbitrary bytes.
55
56 Raise Invalid_argument if n < 0 or n > Sys.max_string_length .
57
58
59
60 val make : int -> char -> bytes
61
62
63 make n c returns a new byte sequence of length n , filled with the byte
64 c .
65
66 Raise Invalid_argument if n < 0 or n > Sys.max_string_length .
67
68
69
70 val init : int -> f:(int -> char) -> bytes
71
72
73 init n f returns a fresh byte sequence of length n , with character i
74 initialized to the result of f i .
75
76 Raise Invalid_argument if n < 0 or n > Sys.max_string_length .
77
78
79
80 val empty : bytes
81
82 A byte sequence of size 0.
83
84
85
86 val copy : bytes -> bytes
87
88 Return a new byte sequence that contains the same bytes as the argu‐
89 ment.
90
91
92
93 val of_string : string -> bytes
94
95 Return a new byte sequence that contains the same bytes as the given
96 string.
97
98
99
100 val to_string : bytes -> string
101
102 Return a new string that contains the same bytes as the given byte
103 sequence.
104
105
106
107 val sub : bytes -> pos:int -> len:int -> bytes
108
109
110 sub s start len returns a new byte sequence of length len , containing
111 the subsequence of s that starts at position start and has length len .
112
113 Raise Invalid_argument if start and len do not designate a valid range
114 of s .
115
116
117
118 val sub_string : bytes -> pos:int -> len:int -> string
119
120 Same as sub but return a string instead of a byte sequence.
121
122
123
124 val extend : bytes -> left:int -> right:int -> bytes
125
126
127 extend s left right returns a new byte sequence that contains the bytes
128 of s , with left uninitialized bytes prepended and right uninitialized
129 bytes appended to it. If left or right is negative, then bytes are
130 removed (instead of appended) from the corresponding side of s .
131
132 Raise Invalid_argument if the result length is negative or longer than
133 Sys.max_string_length bytes.
134
135
136 Since 4.05.0
137
138
139
140 val fill : bytes -> pos:int -> len:int -> char -> unit
141
142
143 fill s start len c modifies s in place, replacing len characters with c
144 , starting at start .
145
146 Raise Invalid_argument if start and len do not designate a valid range
147 of s .
148
149
150
151 val blit : src:bytes -> src_pos:int -> dst:bytes -> dst_pos:int ->
152 len:int -> unit
153
154
155 blit src srcoff dst dstoff len copies len bytes from sequence src ,
156 starting at index srcoff , to sequence dst , starting at index dstoff .
157 It works correctly even if src and dst are the same byte sequence, and
158 the source and destination intervals overlap.
159
160 Raise Invalid_argument if srcoff and len do not designate a valid range
161 of src , or if dstoff and len do not designate a valid range of dst .
162
163
164
165 val blit_string : src:string -> src_pos:int -> dst:bytes -> dst_pos:int
166 -> len:int -> unit
167
168
169 blit src srcoff dst dstoff len copies len bytes from string src ,
170 starting at index srcoff , to byte sequence dst , starting at index
171 dstoff .
172
173 Raise Invalid_argument if srcoff and len do not designate a valid range
174 of src , or if dstoff and len do not designate a valid range of dst .
175
176
177 Since 4.05.0
178
179
180
181 val concat : sep:bytes -> bytes list -> bytes
182
183
184 concat sep sl concatenates the list of byte sequences sl , inserting
185 the separator byte sequence sep between each, and returns the result as
186 a new byte sequence.
187
188
189
190 val cat : bytes -> bytes -> bytes
191
192
193 cat s1 s2 concatenates s1 and s2 and returns the result as new byte
194 sequence.
195
196 Raise Invalid_argument if the result is longer than
197 Sys.max_string_length bytes.
198
199
200 Since 4.05.0
201
202
203
204 val iter : f:(char -> unit) -> bytes -> unit
205
206
207 iter f s applies function f in turn to all the bytes of s . It is
208 equivalent to f (get s 0); f (get s 1); ...; f (get s (length s - 1));
209 () .
210
211
212
213 val iteri : f:(int -> char -> unit) -> bytes -> unit
214
215 Same as Bytes.iter , but the function is applied to the index of the
216 byte as first argument and the byte itself as second argument.
217
218
219
220 val map : f:(char -> char) -> bytes -> bytes
221
222
223 map f s applies function f in turn to all the bytes of s and stores the
224 resulting bytes in a new sequence that is returned as the result.
225
226
227
228 val mapi : f:(int -> char -> char) -> bytes -> bytes
229
230
231 mapi f s calls f with each character of s and its index (in increasing
232 index order) and stores the resulting bytes in a new sequence that is
233 returned as the result.
234
235
236
237 val trim : bytes -> bytes
238
239 Return a copy of the argument, without leading and trailing whitespace.
240 The bytes regarded as whitespace are the ASCII characters ' ' , '\012'
241 , '\n' , '\r' , and '\t' .
242
243
244
245 val escaped : bytes -> bytes
246
247 Return a copy of the argument, with special characters represented by
248 escape sequences, following the lexical conventions of OCaml.
249
250
251
252 val index : bytes -> char -> int
253
254
255 index s c returns the index of the first occurrence of byte c in s .
256
257 Raise Not_found if c does not occur in s .
258
259
260
261 val index_opt : bytes -> char -> int option
262
263
264 index_opt s c returns the index of the first occurrence of byte c in s
265 or None if c does not occur in s .
266
267
268 Since 4.05
269
270
271
272 val rindex : bytes -> char -> int
273
274
275 rindex s c returns the index of the last occurrence of byte c in s .
276
277 Raise Not_found if c does not occur in s .
278
279
280
281 val rindex_opt : bytes -> char -> int option
282
283
284 rindex_opt s c returns the index of the last occurrence of byte c in s
285 or None if c does not occur in s .
286
287
288 Since 4.05
289
290
291
292 val index_from : bytes -> int -> char -> int
293
294
295 index_from s i c returns the index of the first occurrence of byte c in
296 s after position i . Bytes.index s c is equivalent to Bytes.index_from
297 s 0 c .
298
299 Raise Invalid_argument if i is not a valid position in s . Raise
300 Not_found if c does not occur in s after position i .
301
302
303
304 val index_from_opt : bytes -> int -> char -> int option
305
306
307 index_from _opts i c returns the index of the first occurrence of byte
308 c in s after position i or None if c does not occur in s after position
309 i . Bytes.index_opt s c is equivalent to Bytes.index_from_opt s 0 c .
310
311 Raise Invalid_argument if i is not a valid position in s .
312
313
314 Since 4.05
315
316
317
318 val rindex_from : bytes -> int -> char -> int
319
320
321 rindex_from s i c returns the index of the last occurrence of byte c in
322 s before position i+1 . rindex s c is equivalent to rindex_from s
323 (Bytes.length s - 1) c .
324
325 Raise Invalid_argument if i+1 is not a valid position in s . Raise
326 Not_found if c does not occur in s before position i+1 .
327
328
329
330 val rindex_from_opt : bytes -> int -> char -> int option
331
332
333 rindex_from_opt s i c returns the index of the last occurrence of byte
334 c in s before position i+1 or None if c does not occur in s before
335 position i+1 . rindex_opt s c is equivalent to rindex_from s
336 (Bytes.length s - 1) c .
337
338 Raise Invalid_argument if i+1 is not a valid position in s .
339
340
341 Since 4.05
342
343
344
345 val contains : bytes -> char -> bool
346
347
348 contains s c tests if byte c appears in s .
349
350
351
352 val contains_from : bytes -> int -> char -> bool
353
354
355 contains_from s start c tests if byte c appears in s after position
356 start . contains s c is equivalent to contains_from s 0 c .
357
358 Raise Invalid_argument if start is not a valid position in s .
359
360
361
362 val rcontains_from : bytes -> int -> char -> bool
363
364
365 rcontains_from s stop c tests if byte c appears in s before position
366 stop+1 .
367
368 Raise Invalid_argument if stop < 0 or stop+1 is not a valid position in
369 s .
370
371
372
373 val uppercase : bytes -> bytes
374
375 Deprecated. Functions operating on Latin-1 character set are depre‐
376 cated.
377
378
379 Return a copy of the argument, with all lowercase letters translated to
380 uppercase, including accented letters of the ISO Latin-1 (8859-1) char‐
381 acter set.
382
383
384
385 val lowercase : bytes -> bytes
386
387 Deprecated. Functions operating on Latin-1 character set are depre‐
388 cated.
389
390
391 Return a copy of the argument, with all uppercase letters translated to
392 lowercase, including accented letters of the ISO Latin-1 (8859-1) char‐
393 acter set.
394
395
396
397 val capitalize : bytes -> bytes
398
399 Deprecated. Functions operating on Latin-1 character set are depre‐
400 cated.
401
402
403 Return a copy of the argument, with the first character set to upper‐
404 case, using the ISO Latin-1 (8859-1) character set..
405
406
407
408 val uncapitalize : bytes -> bytes
409
410 Deprecated. Functions operating on Latin-1 character set are depre‐
411 cated.
412
413
414 Return a copy of the argument, with the first character set to lower‐
415 case, using the ISO Latin-1 (8859-1) character set..
416
417
418
419 val uppercase_ascii : bytes -> bytes
420
421 Return a copy of the argument, with all lowercase letters translated to
422 uppercase, using the US-ASCII character set.
423
424
425 Since 4.05.0
426
427
428
429 val lowercase_ascii : bytes -> bytes
430
431 Return a copy of the argument, with all uppercase letters translated to
432 lowercase, using the US-ASCII character set.
433
434
435 Since 4.05.0
436
437
438
439 val capitalize_ascii : bytes -> bytes
440
441 Return a copy of the argument, with the first character set to upper‐
442 case, using the US-ASCII character set.
443
444
445 Since 4.05.0
446
447
448
449 val uncapitalize_ascii : bytes -> bytes
450
451 Return a copy of the argument, with the first character set to lower‐
452 case, using the US-ASCII character set.
453
454
455 Since 4.05.0
456
457
458 type t = bytes
459
460
461 An alias for the type of byte sequences.
462
463
464
465 val compare : t -> t -> int
466
467 The comparison function for byte sequences, with the same specification
468 as compare . Along with the type t , this function compare allows the
469 module Bytes to be passed as argument to the functors Set.Make and
470 Map.Make .
471
472
473
474 val equal : t -> t -> bool
475
476 The equality function for byte sequences.
477
478
479 Since 4.05.0
480
481
482
483
484 Iterators
485 val to_seq : t -> char Seq.t
486
487 Iterate on the string, in increasing index order. Modifications of the
488 string during iteration will be reflected in the iterator.
489
490
491 Since 4.07
492
493
494
495 val to_seqi : t -> (int * char) Seq.t
496
497 Iterate on the string, in increasing order, yielding indices along
498 chars
499
500
501 Since 4.07
502
503
504
505 val of_seq : char Seq.t -> t
506
507 Create a string from the generator
508
509
510 Since 4.07
511
512
513
514
515 Binary encoding/decoding of integers
516 The functions in this section binary encode and decode integers to and
517 from byte sequences.
518
519 All following functions raise Invalid_argument if the space needed at
520 index i to decode or encode the integer is not available.
521
522 Little-endian (resp. big-endian) encoding means that least (resp. most)
523 significant bytes are stored first. Big-endian is also known as net‐
524 work byte order. Native-endian encoding is either little-endian or
525 big-endian depending on Sys.big_endian .
526
527 32-bit and 64-bit integers are represented by the int32 and int64
528 types, which can be interpreted either as signed or unsigned numbers.
529
530 8-bit and 16-bit integers are represented by the int type, which has
531 more bits than the binary encoding. These extra bits are handled as
532 follows:
533
534 -Functions that decode signed (resp. unsigned) 8-bit or 16-bit integers
535 represented by int values sign-extend (resp. zero-extend) their result.
536
537 -Functions that encode 8-bit or 16-bit integers represented by int val‐
538 ues truncate their input to their least significant bytes.
539
540
541 val get_uint8 : bytes -> int -> int
542
543
544 get_uint8 b i is b 's unsigned 8-bit integer starting at byte index i .
545
546
547 Since 4.08
548
549
550
551 val get_int8 : bytes -> int -> int
552
553
554 get_int8 b i is b 's signed 8-bit integer starting at byte index i .
555
556
557 Since 4.08
558
559
560
561 val get_uint16_ne : bytes -> int -> int
562
563
564 get_uint16_ne b i is b 's native-endian unsigned 16-bit integer start‐
565 ing at byte index i .
566
567
568 Since 4.08
569
570
571
572 val get_uint16_be : bytes -> int -> int
573
574
575 get_uint16_be b i is b 's big-endian unsigned 16-bit integer starting
576 at byte index i .
577
578
579 Since 4.08
580
581
582
583 val get_uint16_le : bytes -> int -> int
584
585
586 get_uint16_le b i is b 's little-endian unsigned 16-bit integer start‐
587 ing at byte index i .
588
589
590 Since 4.08
591
592
593
594 val get_int16_ne : bytes -> int -> int
595
596
597 get_int16_ne b i is b 's native-endian signed 16-bit integer starting
598 at byte index i .
599
600
601 Since 4.08
602
603
604
605 val get_int16_be : bytes -> int -> int
606
607
608 get_int16_be b i is b 's big-endian signed 16-bit integer starting at
609 byte index i .
610
611
612 Since 4.08
613
614
615
616 val get_int16_le : bytes -> int -> int
617
618
619 get_int16_le b i is b 's little-endian signed 16-bit integer starting
620 at byte index i .
621
622
623 Since 4.08
624
625
626
627 val get_int32_ne : bytes -> int -> int32
628
629
630 get_int32_ne b i is b 's native-endian 32-bit integer starting at byte
631 index i .
632
633
634 Since 4.08
635
636
637
638 val get_int32_be : bytes -> int -> int32
639
640
641 get_int32_be b i is b 's big-endian 32-bit integer starting at byte
642 index i .
643
644
645 Since 4.08
646
647
648
649 val get_int32_le : bytes -> int -> int32
650
651
652 get_int32_le b i is b 's little-endian 32-bit integer starting at byte
653 index i .
654
655
656 Since 4.08
657
658
659
660 val get_int64_ne : bytes -> int -> int64
661
662
663 get_int64_ne b i is b 's native-endian 64-bit integer starting at byte
664 index i .
665
666
667 Since 4.08
668
669
670
671 val get_int64_be : bytes -> int -> int64
672
673
674 get_int64_be b i is b 's big-endian 64-bit integer starting at byte
675 index i .
676
677
678 Since 4.08
679
680
681
682 val get_int64_le : bytes -> int -> int64
683
684
685 get_int64_le b i is b 's little-endian 64-bit integer starting at byte
686 index i .
687
688
689 Since 4.08
690
691
692
693 val set_uint8 : bytes -> int -> int -> unit
694
695
696 set_uint8 b i v sets b 's unsigned 8-bit integer starting at byte index
697 i to v .
698
699
700 Since 4.08
701
702
703
704 val set_int8 : bytes -> int -> int -> unit
705
706
707 set_int8 b i v sets b 's signed 8-bit integer starting at byte index i
708 to v .
709
710
711 Since 4.08
712
713
714
715 val set_uint16_ne : bytes -> int -> int -> unit
716
717
718 set_uint16_ne b i v sets b 's native-endian unsigned 16-bit integer
719 starting at byte index i to v .
720
721
722 Since 4.08
723
724
725
726 val set_uint16_be : bytes -> int -> int -> unit
727
728
729 set_uint16_be b i v sets b 's big-endian unsigned 16-bit integer start‐
730 ing at byte index i to v .
731
732
733 Since 4.08
734
735
736
737 val set_uint16_le : bytes -> int -> int -> unit
738
739
740 set_uint16_le b i v sets b 's little-endian unsigned 16-bit integer
741 starting at byte index i to v .
742
743
744 Since 4.08
745
746
747
748 val set_int16_ne : bytes -> int -> int -> unit
749
750
751 set_int16_ne b i v sets b 's native-endian signed 16-bit integer start‐
752 ing at byte index i to v .
753
754
755 Since 4.08
756
757
758
759 val set_int16_be : bytes -> int -> int -> unit
760
761
762 set_int16_be b i v sets b 's big-endian signed 16-bit integer starting
763 at byte index i to v .
764
765
766 Since 4.08
767
768
769
770 val set_int16_le : bytes -> int -> int -> unit
771
772
773 set_int16_le b i v sets b 's little-endian signed 16-bit integer start‐
774 ing at byte index i to v .
775
776
777 Since 4.08
778
779
780
781 val set_int32_ne : bytes -> int -> int32 -> unit
782
783
784 set_int32_ne b i v sets b 's native-endian 32-bit integer starting at
785 byte index i to v .
786
787
788 Since 4.08
789
790
791
792 val set_int32_be : bytes -> int -> int32 -> unit
793
794
795 set_int32_be b i v sets b 's big-endian 32-bit integer starting at byte
796 index i to v .
797
798
799 Since 4.08
800
801
802
803 val set_int32_le : bytes -> int -> int32 -> unit
804
805
806 set_int32_le b i v sets b 's little-endian 32-bit integer starting at
807 byte index i to v .
808
809
810 Since 4.08
811
812
813
814 val set_int64_ne : bytes -> int -> int64 -> unit
815
816
817 set_int64_ne b i v sets b 's native-endian 64-bit integer starting at
818 byte index i to v .
819
820
821 Since 4.08
822
823
824
825 val set_int64_be : bytes -> int -> int64 -> unit
826
827
828 set_int64_be b i v sets b 's big-endian 64-bit integer starting at byte
829 index i to v .
830
831
832 Since 4.08
833
834
835
836 val set_int64_le : bytes -> int -> int64 -> unit
837
838
839 set_int64_le b i v sets b 's little-endian 64-bit integer starting at
840 byte index i to v .
841
842
843 Since 4.08
844
845
846
847
848
849OCamldoc 2019-07-30 BytesLabels(3)