1Stdlib.BytesLabels(3) OCaml library Stdlib.BytesLabels(3)
2
3
4
6 Stdlib.BytesLabels - no description
7
9 Module Stdlib.BytesLabels
10
12 Module BytesLabels
13 : (module Stdlib__bytesLabels)
14
15
16
17
18
19
20
21
22 val length : bytes -> int
23
24 Return the length (number of bytes) of the argument.
25
26
27
28 val get : bytes -> int -> char
29
30
31 get s n returns the byte at index n in argument s .
32
33
34 Raises Invalid_argument if n is not a valid index in s .
35
36
37
38 val set : bytes -> int -> char -> unit
39
40
41 set s n c modifies s in place, replacing the byte at index n with c .
42
43
44 Raises Invalid_argument if n is not a valid index in s .
45
46
47
48 val create : int -> bytes
49
50
51 create n returns a new byte sequence of length n . The sequence is
52 uninitialized and contains arbitrary bytes.
53
54
55 Raises Invalid_argument if n < 0 or n > Sys.max_string_length .
56
57
58
59 val make : int -> char -> bytes
60
61
62 make n c returns a new byte sequence of length n , filled with the byte
63 c .
64
65
66 Raises 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 (in increasing index order).
75
76
77 Raises Invalid_argument if n < 0 or n > Sys.max_string_length .
78
79
80
81 val empty : bytes
82
83 A byte sequence of size 0.
84
85
86
87 val copy : bytes -> bytes
88
89 Return a new byte sequence that contains the same bytes as the argu‐
90 ment.
91
92
93
94 val of_string : string -> bytes
95
96 Return a new byte sequence that contains the same bytes as the given
97 string.
98
99
100
101 val to_string : bytes -> string
102
103 Return a new string that contains the same bytes as the given byte se‐
104 quence.
105
106
107
108 val sub : bytes -> pos:int -> len:int -> bytes
109
110
111 sub s ~pos ~len returns a new byte sequence of length len , containing
112 the subsequence of s that starts at position pos and has length len .
113
114
115 Raises Invalid_argument if pos and len do not designate a valid range
116 of s .
117
118
119
120 val sub_string : bytes -> pos:int -> len:int -> string
121
122 Same as BytesLabels.sub but return a string instead of a byte sequence.
123
124
125
126 val extend : bytes -> left:int -> right:int -> bytes
127
128
129 extend s ~left ~right returns a new byte sequence that contains the
130 bytes of s , with left uninitialized bytes prepended and right unini‐
131 tialized bytes appended to it. If left or right is negative, then bytes
132 are removed (instead of appended) from the corresponding side of s .
133
134
135 Since 4.05.0 in BytesLabels
136
137
138 Raises Invalid_argument if the result length is negative or longer than
139 Sys.max_string_length bytes.
140
141
142
143 val fill : bytes -> pos:int -> len:int -> char -> unit
144
145
146 fill s ~pos ~len c modifies s in place, replacing len characters with c
147 , starting at pos .
148
149
150 Raises Invalid_argument if pos and len do not designate a valid range
151 of s .
152
153
154
155 val blit : src:bytes -> src_pos:int -> dst:bytes -> dst_pos:int ->
156 len:int -> unit
157
158
159 blit ~src ~src_pos ~dst ~dst_pos ~len copies len bytes from sequence
160 src , starting at index src_pos , to sequence dst , starting at index
161 dst_pos . It works correctly even if src and dst are the same byte se‐
162 quence, and the source and destination intervals overlap.
163
164
165 Raises Invalid_argument if src_pos and len do not designate a valid
166 range of src , or if dst_pos and len do not designate a valid range of
167 dst .
168
169
170
171 val blit_string : src:string -> src_pos:int -> dst:bytes -> dst_pos:int
172 -> len:int -> unit
173
174
175 blit ~src ~src_pos ~dst ~dst_pos ~len copies len bytes from string src
176 , starting at index src_pos , to byte sequence dst , starting at index
177 dst_pos .
178
179
180 Since 4.05.0 in BytesLabels
181
182
183 Raises Invalid_argument if src_pos and len do not designate a valid
184 range of src , or if dst_pos and len do not designate a valid range of
185 dst .
186
187
188
189 val concat : sep:bytes -> bytes list -> bytes
190
191
192 concat ~sep sl concatenates the list of byte sequences sl , inserting
193 the separator byte sequence sep between each, and returns the result as
194 a new byte sequence.
195
196
197 Raises Invalid_argument if the result is longer than
198 Sys.max_string_length bytes.
199
200
201
202 val cat : bytes -> bytes -> bytes
203
204
205 cat s1 s2 concatenates s1 and s2 and returns the result as a new byte
206 sequence.
207
208
209 Since 4.05.0 in BytesLabels
210
211
212 Raises Invalid_argument if the result is longer than
213 Sys.max_string_length bytes.
214
215
216
217 val iter : f:(char -> unit) -> bytes -> unit
218
219
220 iter ~f s applies function f in turn to all the bytes of s . It is
221 equivalent to f (get s 0); f (get s 1); ...; f (get s
222 (length s - 1)); () .
223
224
225
226 val iteri : f:(int -> char -> unit) -> bytes -> unit
227
228 Same as BytesLabels.iter , but the function is applied to the index of
229 the byte as first argument and the byte itself as second argument.
230
231
232
233 val map : f:(char -> char) -> bytes -> bytes
234
235
236 map ~f s applies function f in turn to all the bytes of s (in increas‐
237 ing index order) and stores the resulting bytes in a new sequence that
238 is returned as the result.
239
240
241
242 val mapi : f:(int -> char -> char) -> bytes -> bytes
243
244
245 mapi ~f s calls f with each character of s and its index (in increasing
246 index order) and stores the resulting bytes in a new sequence that is
247 returned as the result.
248
249
250
251 val trim : bytes -> bytes
252
253 Return a copy of the argument, without leading and trailing whitespace.
254 The bytes regarded as whitespace are the ASCII characters ' ' , '\012'
255 , '\n' , '\r' , and '\t' .
256
257
258
259 val escaped : bytes -> bytes
260
261 Return a copy of the argument, with special characters represented by
262 escape sequences, following the lexical conventions of OCaml. All
263 characters outside the ASCII printable range (32..126) are escaped, as
264 well as backslash and double-quote.
265
266
267 Raises Invalid_argument if the result is longer than
268 Sys.max_string_length bytes.
269
270
271
272 val index : bytes -> char -> int
273
274
275 index s c returns the index of the first occurrence of byte c in s .
276
277
278 Raises Not_found if c does not occur in s .
279
280
281
282 val index_opt : bytes -> char -> int option
283
284
285 index_opt s c returns the index of the first occurrence of byte c in s
286 or None if c does not occur in s .
287
288
289 Since 4.05
290
291
292
293 val rindex : bytes -> char -> int
294
295
296 rindex s c returns the index of the last occurrence of byte c in s .
297
298
299 Raises Not_found if c does not occur in s .
300
301
302
303 val rindex_opt : bytes -> char -> int option
304
305
306 rindex_opt s c returns the index of the last occurrence of byte c in s
307 or None if c does not occur in s .
308
309
310 Since 4.05
311
312
313
314 val index_from : bytes -> int -> char -> int
315
316
317 index_from s i c returns the index of the first occurrence of byte c in
318 s after position i . index s c is equivalent to index_from s 0 c .
319
320
321 Raises Invalid_argument if i is not a valid position in s .
322
323
324 Raises Not_found if c does not occur in s after position i .
325
326
327
328 val index_from_opt : bytes -> int -> char -> int option
329
330
331 index_from_opt s i c returns the index of the first occurrence of byte
332 c in s after position i or None if c does not occur in s after position
333 i . index_opt s c is equivalent to index_from_opt s 0 c .
334
335
336 Since 4.05
337
338
339 Raises Invalid_argument if i is not a valid position in s .
340
341
342
343 val rindex_from : bytes -> int -> char -> int
344
345
346 rindex_from s i c returns the index of the last occurrence of byte c in
347 s before position i+1 . rindex s c is equivalent to rindex_from s
348 (length s - 1) c .
349
350
351 Raises Invalid_argument if i+1 is not a valid position in s .
352
353
354 Raises Not_found if c does not occur in s before position i+1 .
355
356
357
358 val rindex_from_opt : bytes -> int -> char -> int option
359
360
361 rindex_from_opt s i c returns the index of the last occurrence of byte
362 c in s before position i+1 or None if c does not occur in s before po‐
363 sition i+1 . rindex_opt s c is equivalent to rindex_from s (length s -
364 1) c .
365
366
367 Since 4.05
368
369
370 Raises Invalid_argument if i+1 is not a valid position in s .
371
372
373
374 val contains : bytes -> char -> bool
375
376
377 contains s c tests if byte c appears in s .
378
379
380
381 val contains_from : bytes -> int -> char -> bool
382
383
384 contains_from s start c tests if byte c appears in s after position
385 start . contains s c is equivalent to contains_from
386 s 0 c .
387
388
389 Raises Invalid_argument if start is not a valid position in s .
390
391
392
393 val rcontains_from : bytes -> int -> char -> bool
394
395
396 rcontains_from s stop c tests if byte c appears in s before position
397 stop+1 .
398
399
400 Raises Invalid_argument if stop < 0 or stop+1 is not a valid position
401 in s .
402
403
404
405 val uppercase : bytes -> bytes
406
407 Deprecated. Functions operating on Latin-1 character set are depre‐
408 cated.
409
410
411 Return a copy of the argument, with all lowercase letters translated to
412 uppercase, including accented letters of the ISO Latin-1 (8859-1) char‐
413 acter set.
414
415
416
417 val lowercase : bytes -> bytes
418
419 Deprecated. Functions operating on Latin-1 character set are depre‐
420 cated.
421
422
423 Return a copy of the argument, with all uppercase letters translated to
424 lowercase, including accented letters of the ISO Latin-1 (8859-1) char‐
425 acter set.
426
427
428
429 val capitalize : bytes -> bytes
430
431 Deprecated. Functions operating on Latin-1 character set are depre‐
432 cated.
433
434
435 Return a copy of the argument, with the first character set to upper‐
436 case, using the ISO Latin-1 (8859-1) character set.
437
438
439
440 val uncapitalize : bytes -> bytes
441
442 Deprecated. Functions operating on Latin-1 character set are depre‐
443 cated.
444
445
446 Return a copy of the argument, with the first character set to lower‐
447 case, using the ISO Latin-1 (8859-1) character set.
448
449
450
451 val uppercase_ascii : bytes -> bytes
452
453 Return a copy of the argument, with all lowercase letters translated to
454 uppercase, using the US-ASCII character set.
455
456
457 Since 4.05.0
458
459
460
461 val lowercase_ascii : bytes -> bytes
462
463 Return a copy of the argument, with all uppercase letters translated to
464 lowercase, using the US-ASCII character set.
465
466
467 Since 4.05.0
468
469
470
471 val capitalize_ascii : bytes -> bytes
472
473 Return a copy of the argument, with the first character set to upper‐
474 case, using the US-ASCII character set.
475
476
477 Since 4.05.0
478
479
480
481 val uncapitalize_ascii : bytes -> bytes
482
483 Return a copy of the argument, with the first character set to lower‐
484 case, using the US-ASCII character set.
485
486
487 Since 4.05.0
488
489
490 type t = bytes
491
492
493 An alias for the type of byte sequences.
494
495
496
497 val compare : t -> t -> int
498
499 The comparison function for byte sequences, with the same specification
500 as compare . Along with the type t , this function compare allows the
501 module Bytes to be passed as argument to the functors Set.Make and
502 Map.Make .
503
504
505
506 val equal : t -> t -> bool
507
508 The equality function for byte sequences.
509
510
511 Since 4.05.0
512
513
514
515
516 Unsafe conversions (for advanced users)
517 This section describes unsafe, low-level conversion functions between
518 bytes and string . They do not copy the internal data; used improperly,
519 they can break the immutability invariant on strings provided by the
520 -safe-string option. They are available for expert library authors, but
521 for most purposes you should use the always-correct BytesLa‐
522 bels.to_string and BytesLabels.of_string instead.
523
524 val unsafe_to_string : bytes -> string
525
526 Unsafely convert a byte sequence into a string.
527
528 To reason about the use of unsafe_to_string , it is convenient to con‐
529 sider an "ownership" discipline. A piece of code that manipulates some
530 data "owns" it; there are several disjoint ownership modes, including:
531
532 -Unique ownership: the data may be accessed and mutated
533
534 -Shared ownership: the data has several owners, that may only access
535 it, not mutate it.
536
537 Unique ownership is linear: passing the data to another piece of code
538 means giving up ownership (we cannot write the data again). A unique
539 owner may decide to make the data shared (giving up mutation rights on
540 it), but shared data may not become uniquely-owned again.
541
542
543 unsafe_to_string s can only be used when the caller owns the byte se‐
544 quence s -- either uniquely or as shared immutable data. The caller
545 gives up ownership of s , and gains ownership of the returned string.
546
547 There are two valid use-cases that respect this ownership discipline:
548
549 1. Creating a string by initializing and mutating a byte sequence that
550 is never changed after initialization is performed.
551
552
553 let string_init len f : string =
554 let s = Bytes.create len in
555 for i = 0 to len - 1 do Bytes.set s i (f i) done;
556 Bytes.unsafe_to_string s
557
558
559 This function is safe because the byte sequence s will never be ac‐
560 cessed or mutated after unsafe_to_string is called. The string_init
561 code gives up ownership of s , and returns the ownership of the result‐
562 ing string to its caller.
563
564 Note that it would be unsafe if s was passed as an additional parameter
565 to the function f as it could escape this way and be mutated in the fu‐
566 ture -- string_init would give up ownership of s to pass it to f , and
567 could not call unsafe_to_string safely.
568
569 We have provided the String.init , String.map and String.mapi functions
570 to cover most cases of building new strings. You should prefer those
571 over to_string or unsafe_to_string whenever applicable.
572
573 2. Temporarily giving ownership of a byte sequence to a function that
574 expects a uniquely owned string and returns ownership back, so that we
575 can mutate the sequence again after the call ended.
576
577
578 let bytes_length (s : bytes) =
579 String.length (Bytes.unsafe_to_string s)
580
581
582 In this use-case, we do not promise that s will never be mutated after
583 the call to bytes_length s . The String.length function temporarily
584 borrows unique ownership of the byte sequence (and sees it as a string
585 ), but returns this ownership back to the caller, which may assume that
586 s is still a valid byte sequence after the call. Note that this is only
587 correct because we know that String.length does not capture its argu‐
588 ment -- it could escape by a side-channel such as a memoization combi‐
589 nator.
590
591 The caller may not mutate s while the string is borrowed (it has tempo‐
592 rarily given up ownership). This affects concurrent programs, but also
593 higher-order functions: if String.length returned a closure to be
594 called later, s should not be mutated until this closure is fully ap‐
595 plied and returns ownership.
596
597
598
599 val unsafe_of_string : string -> bytes
600
601 Unsafely convert a shared string to a byte sequence that should not be
602 mutated.
603
604 The same ownership discipline that makes unsafe_to_string correct ap‐
605 plies to unsafe_of_string : you may use it if you were the owner of the
606 string value, and you will own the return bytes in the same mode.
607
608 In practice, unique ownership of string values is extremely difficult
609 to reason about correctly. You should always assume strings are shared,
610 never uniquely owned.
611
612 For example, string literals are implicitly shared by the compiler, so
613 you never uniquely own them.
614
615
616 let incorrect = Bytes.unsafe_of_string "hello"
617 let s = Bytes.of_string "hello"
618
619
620 The first declaration is incorrect, because the string literal "hello"
621 could be shared by the compiler with other parts of the program, and
622 mutating incorrect is a bug. You must always use the second version,
623 which performs a copy and is thus correct.
624
625 Assuming unique ownership of strings that are not string literals, but
626 are (partly) built from string literals, is also incorrect. For exam‐
627 ple, mutating unsafe_of_string ("foo" ^ s) could mutate the shared
628 string "foo" -- assuming a rope-like representation of strings. More
629 generally, functions operating on strings will assume shared ownership,
630 they do not preserve unique ownership. It is thus incorrect to assume
631 unique ownership of the result of unsafe_of_string .
632
633 The only case we have reasonable confidence is safe is if the produced
634 bytes is shared -- used as an immutable byte sequence. This is possibly
635 useful for incremental migration of low-level programs that manipulate
636 immutable sequences of bytes (for example Marshal.from_bytes ) and pre‐
637 viously used the string type for this purpose.
638
639
640
641
642 Iterators
643 val to_seq : t -> char Seq.t
644
645 Iterate on the string, in increasing index order. Modifications of the
646 string during iteration will be reflected in the iterator.
647
648
649 Since 4.07
650
651
652
653 val to_seqi : t -> (int * char) Seq.t
654
655 Iterate on the string, in increasing order, yielding indices along
656 chars
657
658
659 Since 4.07
660
661
662
663 val of_seq : char Seq.t -> t
664
665 Create a string from the generator
666
667
668 Since 4.07
669
670
671
672
673 Binary encoding/decoding of integers
674 The functions in this section binary encode and decode integers to and
675 from byte sequences.
676
677 All following functions raise Invalid_argument if the space needed at
678 index i to decode or encode the integer is not available.
679
680 Little-endian (resp. big-endian) encoding means that least (resp. most)
681 significant bytes are stored first. Big-endian is also known as net‐
682 work byte order. Native-endian encoding is either little-endian or
683 big-endian depending on Sys.big_endian .
684
685 32-bit and 64-bit integers are represented by the int32 and int64
686 types, which can be interpreted either as signed or unsigned numbers.
687
688 8-bit and 16-bit integers are represented by the int type, which has
689 more bits than the binary encoding. These extra bits are handled as
690 follows:
691
692 -Functions that decode signed (resp. unsigned) 8-bit or 16-bit integers
693 represented by int values sign-extend (resp. zero-extend) their result.
694
695 -Functions that encode 8-bit or 16-bit integers represented by int val‐
696 ues truncate their input to their least significant bytes.
697
698
699 val get_uint8 : bytes -> int -> int
700
701
702 get_uint8 b i is b 's unsigned 8-bit integer starting at byte index i .
703
704
705 Since 4.08
706
707
708
709 val get_int8 : bytes -> int -> int
710
711
712 get_int8 b i is b 's signed 8-bit integer starting at byte index i .
713
714
715 Since 4.08
716
717
718
719 val get_uint16_ne : bytes -> int -> int
720
721
722 get_uint16_ne b i is b 's native-endian unsigned 16-bit integer start‐
723 ing at byte index i .
724
725
726 Since 4.08
727
728
729
730 val get_uint16_be : bytes -> int -> int
731
732
733 get_uint16_be b i is b 's big-endian unsigned 16-bit integer starting
734 at byte index i .
735
736
737 Since 4.08
738
739
740
741 val get_uint16_le : bytes -> int -> int
742
743
744 get_uint16_le b i is b 's little-endian unsigned 16-bit integer start‐
745 ing at byte index i .
746
747
748 Since 4.08
749
750
751
752 val get_int16_ne : bytes -> int -> int
753
754
755 get_int16_ne b i is b 's native-endian signed 16-bit integer starting
756 at byte index i .
757
758
759 Since 4.08
760
761
762
763 val get_int16_be : bytes -> int -> int
764
765
766 get_int16_be b i is b 's big-endian signed 16-bit integer starting at
767 byte index i .
768
769
770 Since 4.08
771
772
773
774 val get_int16_le : bytes -> int -> int
775
776
777 get_int16_le b i is b 's little-endian signed 16-bit integer starting
778 at byte index i .
779
780
781 Since 4.08
782
783
784
785 val get_int32_ne : bytes -> int -> int32
786
787
788 get_int32_ne b i is b 's native-endian 32-bit integer starting at byte
789 index i .
790
791
792 Since 4.08
793
794
795
796 val get_int32_be : bytes -> int -> int32
797
798
799 get_int32_be b i is b 's big-endian 32-bit integer starting at byte in‐
800 dex i .
801
802
803 Since 4.08
804
805
806
807 val get_int32_le : bytes -> int -> int32
808
809
810 get_int32_le b i is b 's little-endian 32-bit integer starting at byte
811 index i .
812
813
814 Since 4.08
815
816
817
818 val get_int64_ne : bytes -> int -> int64
819
820
821 get_int64_ne b i is b 's native-endian 64-bit integer starting at byte
822 index i .
823
824
825 Since 4.08
826
827
828
829 val get_int64_be : bytes -> int -> int64
830
831
832 get_int64_be b i is b 's big-endian 64-bit integer starting at byte in‐
833 dex i .
834
835
836 Since 4.08
837
838
839
840 val get_int64_le : bytes -> int -> int64
841
842
843 get_int64_le b i is b 's little-endian 64-bit integer starting at byte
844 index i .
845
846
847 Since 4.08
848
849
850
851 val set_uint8 : bytes -> int -> int -> unit
852
853
854 set_uint8 b i v sets b 's unsigned 8-bit integer starting at byte index
855 i to v .
856
857
858 Since 4.08
859
860
861
862 val set_int8 : bytes -> int -> int -> unit
863
864
865 set_int8 b i v sets b 's signed 8-bit integer starting at byte index i
866 to v .
867
868
869 Since 4.08
870
871
872
873 val set_uint16_ne : bytes -> int -> int -> unit
874
875
876 set_uint16_ne b i v sets b 's native-endian unsigned 16-bit integer
877 starting at byte index i to v .
878
879
880 Since 4.08
881
882
883
884 val set_uint16_be : bytes -> int -> int -> unit
885
886
887 set_uint16_be b i v sets b 's big-endian unsigned 16-bit integer start‐
888 ing at byte index i to v .
889
890
891 Since 4.08
892
893
894
895 val set_uint16_le : bytes -> int -> int -> unit
896
897
898 set_uint16_le b i v sets b 's little-endian unsigned 16-bit integer
899 starting at byte index i to v .
900
901
902 Since 4.08
903
904
905
906 val set_int16_ne : bytes -> int -> int -> unit
907
908
909 set_int16_ne b i v sets b 's native-endian signed 16-bit integer start‐
910 ing at byte index i to v .
911
912
913 Since 4.08
914
915
916
917 val set_int16_be : bytes -> int -> int -> unit
918
919
920 set_int16_be b i v sets b 's big-endian signed 16-bit integer starting
921 at byte index i to v .
922
923
924 Since 4.08
925
926
927
928 val set_int16_le : bytes -> int -> int -> unit
929
930
931 set_int16_le b i v sets b 's little-endian signed 16-bit integer start‐
932 ing at byte index i to v .
933
934
935 Since 4.08
936
937
938
939 val set_int32_ne : bytes -> int -> int32 -> unit
940
941
942 set_int32_ne b i v sets b 's native-endian 32-bit integer starting at
943 byte index i to v .
944
945
946 Since 4.08
947
948
949
950 val set_int32_be : bytes -> int -> int32 -> unit
951
952
953 set_int32_be b i v sets b 's big-endian 32-bit integer starting at byte
954 index i to v .
955
956
957 Since 4.08
958
959
960
961 val set_int32_le : bytes -> int -> int32 -> unit
962
963
964 set_int32_le b i v sets b 's little-endian 32-bit integer starting at
965 byte index i to v .
966
967
968 Since 4.08
969
970
971
972 val set_int64_ne : bytes -> int -> int64 -> unit
973
974
975 set_int64_ne b i v sets b 's native-endian 64-bit integer starting at
976 byte index i to v .
977
978
979 Since 4.08
980
981
982
983 val set_int64_be : bytes -> int -> int64 -> unit
984
985
986 set_int64_be b i v sets b 's big-endian 64-bit integer starting at byte
987 index i to v .
988
989
990 Since 4.08
991
992
993
994 val set_int64_le : bytes -> int -> int64 -> unit
995
996
997 set_int64_le b i v sets b 's little-endian 64-bit integer starting at
998 byte index i to v .
999
1000
1001 Since 4.08
1002
1003
1004
1005
1006
1007OCamldoc 2021-07-22 Stdlib.BytesLabels(3)