xl*55.5.0+dev0-2025-04-28/%Bytes&length@%bytes@@@#int@@@@@-%bytes_lengthAA @@@6../../stdlib/bytes.mliy  y  :@@%Bytes@@@#get@@@@@@@@$char@@@@@@@/%bytes_safe_getBA$@@@@$| x x%| x @@#A@@#set@@@@@@A@@@@$@@@$unit@@@@@@@@@/%bytes_safe_setCAM@@@@@NA 2 2OA 2 q@@MB@@&create@d@@@o@@@@@1caml_create_bytesAAh@@@gF  hF  ?@@fC@@$make@}@@@@`@@@@@@@@@@@K  K  '@@D@@$init@@@@@@@@@@@@@@@@@@@@@@P  P  @@E@@%empty@@@@VV@@F@@$copy@@@@@@@@@@YY*@@G@@)of_string@&string@@@@@@@@@]]@@H@@)to_string@@@@@@@@@@aa@@I@@#sub@@@@@@@@@@@@#@@@@@@@@@@errer@@J@@*sub_string@3@@@@4@@@@;@@@a@@@@@@@@@@:l;l@@9K@@&extend@V@@@@W@@@@^@@@i@@@@@@@@@@]o  ^o 5@@\L@@$fill@y@@@@z@@@@@@@@d@@@@@@@@@@@@@@@@yy*@@M@@$blit @@@@@@@@@@@@@@@@@@@@q@@@@@@@@@@@@@@$-@@N@@+blit_string!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@AJ@@O@@&concat"@@@@@$list@@@@@@@@@@@@@@  @@ P@@#cat#@( @@@@/ @@@4 @@@@@@@@()&@@'Q@@$iter$@@ @@@ @@@@@ @R@@@ @@@ @@ @@ @KL/@@JR@@%iteri%@@c@@@@F@@@"@@@@@@@@|@@@.@@@@@@@@uv@@tS@@#map&@@i@@@n@@@@@@@@@@@@@@@@@@@T@@$mapi'@@@@@@@@@@@@ @@!@@"@@@@#@@@$@@%@@&@~~~@@U@@)fold_left(@@#acc@0A@'@@@@( @@)@@*@@@@@+@@,@@-@@.@lll@@V@@*fold_right)@@ @@@1@#acc@:A@2@@3@@4@!@@@5@@@6@@7@@8@BB B@@W@@'for_all*@@"@@@;$bool#@@@<@@=@3$@@@> %@@@?@@@@@A@,  -  H@@+X@@&exists+@@ &@@@B$'@@@C@@D@V(@@@E0)@@@F@@G@@H@O  P  @@NY@@$trim,@k*@@@Ip+@@@J@@K@d!E!Ee!E!^@@cZ@@'escaped-@,@@@L-@@@M@@N@y""z"":@@x[@@%index.@.@@@O@r/@@@P0@@@Q@@R@@S@####@@\@@)index_opt/@1@@@T@2@@@U&optionL3@@@V@@@X@@Y@@Z@$I$I$I$s@@]@@&rindex0@4@@@[@5@@@\6@@@]@@^@@_@%%%%&@@^@@*rindex_opt1@7@@@`@8@@@a?9@@@b@@@d@@e@@f@%%%%@@_@@*index_from2@:@@@g@;@@@h@<@@@i=@@@j@@k@@l@@m@&m&m&m&@@`@@.index_from_opt3@4>@@@n@5?@@@o@@@@@pEA@@@q@@@s@@t@@u@@v@@''A''@@?a@@+rindex_from4@\B@@@w@]C@@@x@@D@@@yiE@@@z@@{@@|@@}@c)B)Bd)B)o@@bb@@/rindex_from_opt5@F@@@~@G@@@@cH@@@ՠI@@@@@@@@@@@@@****@@c@@(contains6@J@@@@K@@@L@@@@@@@@ ,D,D ,D,h@@d@@-contains_from7@M@@@@N@@@@O@@@P@@@@@@@@@@,,,,@@e@@.rcontains_from8@Q@@@@R@@@@S@@@T@@@@@@@@@@----@@f@@/uppercase_ascii9@ U@@@V@@@@@@....@@g@@/lowercase_ascii:@W@@@#X@@@@@@!/l/l!/l/@@h@@0capitalize_ascii;@3Y@@@8Z@@@@@@,&0606-&060[@@+i@@2uncapitalize_ascii<@H[@@@M\@@@@@@A+00B+01@@@j@@!t=A;@@@A]]@@@@@@@Q011R011@@@@Pk@A@'compare>@%Bytes^!t@@@@%Bytes_!t@@@y`@@@@@@@@s311t312@@rl@@%equal?@%Bytesa!t@@@@%Bytesb!t@@@vc@@@@@@@@9339334@@m@@+starts_with@&prefixd@@@@e@@@f@@@@@@@@=33>33@@n@@)ends_withA&suffixg@@@@h@@@i@@@@@@@@D4W4WE4g4@@o@@0unsafe_to_stringB@j@@@ k@@@@@@T66T66@@p@@0unsafe_of_stringC@l@@@m@@@@@@CCCC@@q@@-split_on_charD@n@@@@o@@@q(p@@@@@@@@@@@JJJK @@r@@&to_seq@%Bytesr!t@@@&Stdlibt#Seq!t!s@@@ @@@ @@ @@M[M[AM[Mw@@?s@@'to_seqi@%Bytesu!t@@@ &Stdlibx#Seq!t@lv@@@ @Ow@@@ @@ @@@ @@ @nNNoNN;@@mt@@&of_seq@&Stdlibz#Seq!tjy@@@ @@@ %Bytes{!t@@@ @@ @NNNN@@u@@/get_utf_8_uchar@%Bytes|!t@@@ @}@@@ &Stdlib~%Uchar*utf_decode@@@ @@ @@ @OIOIOIO{@@v@@/set_utf_8_uchar@%Bytes!t@@@ @@@@ @&Stdlib%Uchar!t@@@ @@@ @@ @@ @@ @OOOO@@w@@.is_valid_utf_8@%Bytes!t@@@ @@@ @@ @Q+Q+Q+QI@@x@@2get_utf_16be_uchar@%Bytes!t@@@ @@@@ &Stdlib%Uchar*utf_decode@@@ @@ @@ @QQQQ@@y@@2set_utf_16be_uchar@%Bytes!t@@@ @<@@@ @&Stdlib%Uchar!t@@@ M@@@ @@ @@ @@ @GRMRMHRMR@@Fz@@1is_valid_utf_16be@%Bytes!t@@@ @@@@ @@ @_ SS` SS@@^{@@2get_utf_16le_uchar@%Bytes!t@@@ @@@@ &Stdlib%Uchar*utf_decode@@@ @@ @@ @TPTPTPT@@|@@2set_utf_16le_uchar@%Bytes!t@@@ @@@@ @&Stdlib%Uchar!t@@@ @@@ @@ @@ @@ @TTTU@@}@@1is_valid_utf_16le@%Bytes!t@@@ @@@ @@ @VDVDVDVe@@~@@)get_uint8@@@@ @@@@ @@@ @@ @@ @@[|[|@[|[@@@@(get_int8@@@@ @@@@ @@@ @@ @@ @E\\E\\&@@@@@-get_uint16_ne@@@@ @@@@  @@@ @@ @@ @J\\J\\@@A@@-get_uint16_be@6@@@ @7@@@ <@@@ @@ @@ @6P]+]+7P]+]R@@5B@@-get_uint16_le@R@@@ @S@@@ X@@@ @@ @@ @RV]]SV]]@@QC@@,get_int16_ne@n@@@ @o@@@ t@@@ @@ @@ @n\^n^no\^n^@@mD@@,get_int16_be@@@@ @@@@ @@@ @@ @@ @b_ _ b_ _3@@E@@,get_int16_le@@@@ @@@@ @@@ @@ @@ @h__h__@@F@@,get_int32_ne@@@@ @@@@ %int32@@@ @@ @@ @n`H`Hn`H`p@@G@@,get_int32_be@@@@ @@@@ @@@ @@ @@ @t``t`a @@H@@,get_int32_le@@@@ @@@@ 9@@@ @@ @@ @zayayzaya@@I@@,get_int64_ne@ @@@ @ @@@ %int64@@@ @@ @@ @ bb bb;@@ J@@,get_int64_be@ 4@@@ @ 5@@@ @@@ @@ @@ @ 4bb 5bb@@ 3K@@,get_int64_le@ P@@@ @ Q@@@ 9@@@ @@ @@ @ PcDcD QcDcl@@ OL@@)set_uint8@ l@@@ @ m@@@ @ t@@@  ,@@@ @@ @@ @@ @ scc tcd @@ rM@@(set_int8@ @@@ @ @@@ @ @@@ ! O@@@ "@@ #@@ $@@ %@ d}d} d}d@@ N@@-set_uint16_ne@ @@@ &@ @@@ '@ @@@ ( r@@@ )@@ *@@ +@@ ,@ ee eeG@@ O@@-set_uint16_be@ @@@ -@ @@@ .@ @@@ / @@@ 0@@ 1@@ 2@@ 3@ ee ee@@ P@@-set_uint16_le@ @@@ 4@ @@@ 5@ @@@ 6 @@@ 7@@ 8@@ 9@@ :@ ff ff@@ Q@@,set_int16_ne@ @@@ ;@ @@@ <@ #@@@ = @@@ >@@ ?@@ @@@ A@ "g7g7 #g7ge@@ !R@@,set_int16_be@ >@@@ B@ ?@@@ C@ F@@@ D @@@ E@@ F@@ G@@ H@ Egg Fgh@@ DS@@,set_int16_le@ a@@@ I@ b@@@ J@ i@@@ K !@@@ L@@ M@@ N@@ O@ hhh ihh@@ gT@@,set_int32_ne@ @@@ P@ @@@ Q@@@@ R D@@@ S@@ T@@ U@@ V@ iJiJ iJiz@@ U@@,set_int32_be@ @@@ W@ @@@ X@@@@ Y g@@@ Z@@ [@@ \@@ ]@ ii ij'@@ V@@,set_int32_le@ @@@ ^@ @@@ _@ @@@ ` @@@ a@@ b@@ c@@ d@ jj jj@@ W@@,set_int64_ne@ @@@ e@ @@@ f@@@@ g @@@ h@@ i@@ j@@ k@ kNkN kNk~@@ X@@,set_int64_be@ @@@ l@ @@@ m@@@@ n @@@ o@@ p@@ q@@ r@ kk kl+@@ Y@@,set_int64_le@ 3@@@ s@ 4@@@ t@@@@ u @@@ v@@ w@@ x@@ y@ :ll ;ll@@ 9Z@@*unsafe_get@ V@@@ z@ W@@@ { 8@@@ |@@ }@@ ~1%bytes_unsafe_getBA [@@@@ [-xmxm \-xmx@@ Z[@@*unsafe_set@ w@@@ @ x@@@ @ [@@@  7@@@ @@ @@ @@ 1%bytes_unsafe_setCA @@@@@ .xx .xx@@ \@@+unsafe_blit@ @@@ @ @@@ @ @@@ @ @@@ @ @@@  n@@@ @@ @@ @@ @@ @@ /caml_blit_bytesE@ @@@@@@@ /xx 1y7y_@'noalloc 1y7yW 1y7y^@@ 1y7yT @@ ]@@2unsafe_blit_string@ @@@ @ @@@ @ @@@ @  @@@ @  @@@   @@@ @@ @@ @@ @@ @@ 0caml_blit_stringE@ @@@@@@@ 2y`y` 4yy@'noalloc 4yy 4yy@@ 4yy @@ ^@@+unsafe_fill@ & @@@ @ ' @@@ @ .@@@ @ @@@  @@@ @@ @@ @@ @@ /caml_fill_bytesD@ 9@@@@@@ ;5yy <6yz,@'noalloc B6yz$ C6yz+@@ F6yz! @@ D_@@-unsafe_escape@ a@@@  f@@@ @@ @ Z8z.z. [8z.zP@@ Y`@@@39Byte sequence operations.@ A byte sequence is a mutable data structure that contains a fixed-length sequence of bytes. Each byte can be indexed in constant time for reading or writing.@: Given a byte sequence !s+ of length !l?, we can access each of the !l* bytes of !s 4 via its index in the sequence. Indexes start at !0 %, and we will call an index valid in !s ! if it falls within the range )[0...l-1] (inclusive). A position is the point between two bytes or at the beginning or end of the sequence. We call a position valid in !s> if it falls within the range '[0...l] - (inclusive). Note that the byte at index !n9 is between positions !n% and #n+1!.@3 Two parameters %start% and #len + are said to designate a valid range of !s$ if (len >= 0% and %start% and )start+len; are valid positions in !s!.@ B Byte sequences can be modified in place, for instance via the #set( and $blit 9 functions described below. See also strings (module &String@@ P), which are almost the same data structure, but cannot be modified in place.@ , Bytes are represented by the OCaml type $char!.@ J The labeled version of this module can be used as described in the )StdLabels@@( module.@@@@$4.02@@@@@@@A6../../stdlib/bytes.mli,Bytes.length3 4Return the length (number of bytes) of the argument.@@@@@@@@@@@@ -@  /@@@@ )Bytes.get3'get s n; returns the byte at index !n- in argument !s!.@@@@@@@@0Invalid_argument#if !n9 is not a valid index in !s!.@@@@@ @@1 A@4 =@@@@ /)Bytes.set3)set s n c* modifies !s ' in place, replacing the byte at index !n* with !c!.@@@@@@@@0Invalid_argument#if !n9 is not a valid index in !s!.@@@@@ W@j X@m T@p P@@@@ A,Bytes.create3(create n ' returns a new byte sequence of length !n A. The sequence is uninitialized and contains arbitrary bytes.@@@@@@@@0Invalid_argument#if %n < 0$ or $n > 5Sys.max_string_length@@!.@@@@@ `@ a@@@@ U*Bytes.make3(make n c ' returns a new byte sequence of length !n;, filled with the byte !c!.@@@@@@@@0Invalid_argument#if %n < 0$ or $n > 5Sys.max_string_length@@!.@@@@@ z@ {@ w@@@@ o*Bytes.init3(init n f ) returns a fresh byte sequence of length !n5, with character !i> initialized to the result of #f i ! (in increasing index order).@@@@@@@@0Invalid_argument#if %n < 0$ or $n > 5Sys.max_string_length@@!.@@@@@ @  @ @@@@ +Bytes.empty3:A byte sequence of size 0.@@@@@@@@@@@@ @@@@ *Bytes.copy3 LReturn a new byte sequence that contains the same bytes as the argument.@@@@@@@@@@@@ @* @@@@ /Bytes.of_string3 PReturn a new byte sequence that contains the same bytes as the given string.@@@@@@@@@@@@ @9 @@@@ y/Bytes.to_string3 PReturn a new string that contains the same bytes as the given byte sequence.@@@@@@@@@@@@ z@H {@@@@ s)Bytes.sub3-sub s pos len ' returns a new byte sequence of length #len $, containing the subsequence of !s9 that starts at position #pos4 and has length #len!.@@@@@@@@0Invalid_argument#if #pos% and #len ' do not designate a valid range of !s!.@@@@@ @ @ @ @@@@ 0Bytes.sub_string3(Same as QD@ 0 but return a string instead of a byte sequence.@@@@@@@@@@@@ @ @ @ @@@@ ,Bytes.extend33extend s left right < returns a new byte sequence that contains the bytes of !s', with $left ' uninitialized bytes prepended and %right ( uninitialized bytes appended to it. If $left$ or %right b is negative, then bytes are removed (instead of appended) from the corresponding side of !s!.@@@@34.05 in BytesLabels@@@0Invalid_argument 4if the result length is negative or longer than 5Sys.max_string_length@@' bytes.@@@@@ @ @ @ @@@@ *Bytes.fill30fill s pos len c* modifies !s5 in place, replacing #len5 characters with !c., starting at #pos!.@@@@@@@@0Invalid_argument#if #pos% and #len ' do not designate a valid range of !s!.@@@@@ @7 @: @= @@ @@@@ ؠ*Bytes.blit3 blit src src_pos dst dst_pos len( copies #len> bytes from byte sequence #src4, starting at index 'src_pos3, to byte sequence #dst8, starting at index 'dst_pos=. It works correctly even if #src% and #dst V are the same byte sequence, and the source and destination intervals overlap.@@@@@@@@0Invalid_argument#if 'src_pos% and #len ' do not designate a valid range of #src(, or if 'dst_pos% and #len ' do not designate a valid range of #dst!.@@@@@0@1@-@)@%@!@@@@1Bytes.blit_string3 'blit_string src src_pos dst dst_pos len( copies #len7 bytes from string #src4, starting at index 'src_pos3, to byte sequence #dst8, starting at index 'dst_pos!.@@@@34.05 in BytesLabels@@@0Invalid_argument#if 'src_pos% and #len ' do not designate a valid range of #src(, or if 'dst_pos% and #len ' do not designate a valid range of #dst!.@@@@@g@h@d@`@\@X@@@@P,Bytes.concat3-concat sep sl ) concatenates the list of byte sequences "sl ,, inserting the separator byte sequence #sep A between each, and returns the result as a new byte sequence.@@@@@@@@0Invalid_argument !if the result is longer than 5Sys.max_string_length@@' bytes.@@@@@l@Dm@Gi@@@@Z)Bytes.cat3)cat s1 s2. concatenates "s1% and "s2 3 and returns the result as a new byte sequence.@@@@34.05 in BytesLabels@@@0Invalid_argument !if the result is longer than 5Sys.max_string_length@@' bytes.@@@@@x@sy@vu@@@@m*Bytes.iter3(iter f s2 applies function !f= in turn to all the bytes of !s:. It is equivalent to ?f (get s 0); f (get s 1); ...; f (get s (length s - 1)); ()!.@@@@@@@@@@@@@@y@@@@q+Bytes.iteri3(Same as 0D@ x, but the function is applied to the index of the byte as first argument and the byte itself as second argument.@@@@@@@@@@@@x@y@g@@@@_)Bytes.map3'map f s2 applies function !f= in turn to all the bytes of !s u (in increasing index order) and stores the resulting bytes in a new sequence that is returned as the result.@@@@@@@@@@@@o@p@e@@@@]*Bytes.mapi3(mapi f s' calls !f8 with each character of !s and its index (in increasing index order) and stores the resulting bytes in a new sequence that is returned as the result.@@@@@@@@@@@@m@n@\@@@@T/Bytes.fold_left3/fold_left f x s. computes 7f (... (f (f x (get s 0)) (get s 1)) ...) (get s (n-1)),, where !n2 is the length of !s!.@@@@$4.13@@@@@@@l@m@ n@#`@@@@]0Bytes.fold_right30fold_right f s x. computes 8f (get s 0) (f (get s 1) ( ... (f (get s (n-1)) x) ...)),, where !n2 is the length of !s!.@@@@$4.13@@@@@@@u@Iv@Lh@Os@@@@f-Bytes.for_all3+for_all p s= checks if all characters in !s7 satisfy the predicate !p!.@@@@$4.13@@@@@@@x@oy@rm@@@@e,Bytes.exists3*exists p s % checks if at least one character of !s= satisfies the predicate !p!.@@@@$4.13@@@@@@@w@x@m@@@@e*Bytes.trim3 Return a copy of the argument, without leading and trailing whitespace. The bytes regarded as whitespace are the ASCII characters #' '", &'\012'", $'\n'", $'\r'&, and $'\t'!.@@@@@@@@@@@@@@@@@}-Bytes.escaped3 Return a copy of the argument, with special characters represented by escape sequences, following the lexical conventions of OCaml. All characters outside the ASCII printable range (32..126) are escaped, as well as backslash and double-quote.@@@@@@@@0Invalid_argument !if the result is longer than 5Sys.max_string_length@@' bytes.@@@@@@@@@@+Bytes.index3)index s c 3 returns the index of the first occurrence of byte !c( in !s!.@@@@@@@@)Not_found#if !c3 does not occur in !s!.@@@@@@ @@@@@/Bytes.index_opt3-index_opt s c 3 returns the index of the first occurrence of byte !c( in !s$ or $None$ if !c3 does not occur in !s!.@@@@$4.05@@@@@@@@B@E@@@@,Bytes.rindex3*rindex s c 2 returns the index of the last occurrence of byte !c( in !s!.@@@@@@@@)Not_found#if !c3 does not occur in !s!.@@@@@@u@x@@@@à0Bytes.rindex_opt3.rindex_opt s c 2 returns the index of the last occurrence of byte !c( in !s$ or $None$ if !c3 does not occur in !s!.@@@@$4.05@@@@@@@@@@@@@נ0Bytes.index_from30index_from s i c 7 returns the index of the first occurrence of byte !c$ in !s0 after position !i#. )index s c6 is equivalent to 0index_from s 0 c!.@@@@@@@@0Invalid_argument#if !i< is not a valid position in !s!.@)Not_found#if !c3 does not occur in !s0 after position !i!.@@@@@#@$@  @ @@@@4Bytes.index_from_opt34index_from_opt s i c 7 returns the index of the first occurrence of byte !c$ in !s0 after position !i$ or $None$ if !c3 does not occur in !s4 after position !i&. -index_opt s c2 is equivalent to 4index_from_opt s 0 c!.@@@@$4.05@@@0Invalid_argument#if !i< is not a valid position in !s!.@@@@@b@ic@l_@o[@@@@N1Bytes.rindex_from31rindex_from s i c 6 returns the index of the last occurrence of byte !c$ in !s1 before position #i+1#. *rindex s c6 is equivalent to >rindex_from s (length s - 1) c!.@@@@@@@@0Invalid_argument#if #i+1< is not a valid position in !s!.@)Not_found#if !c3 does not occur in !s1 before position #i+1!.@@@@@@@@@@@@5Bytes.rindex_from_opt35rindex_from_opt s i c 6 returns the index of the last occurrence of byte !c$ in !s1 before position #i+1$ or $None$ if !c7 does not occur in !s1 before position #i+1#. .rindex_opt s c6 is equivalent to >rindex_from s (length s - 1) c!.@@@@$4.05@@@0Invalid_argument#if #i+1< is not a valid position in !s!.@@@@@@+@.@1@@@@Š.Bytes.contains3,contains s c/ tests if byte !c, appears in !s!.@@@@@@@@@@@@@O@R@@@@ʠ3Bytes.contains_from37contains_from s start c/ tests if byte !c, appears in !s4 after position %start#. ,contains s c2 is equivalent to 7contains_from s 0 c!.@@@@@@@@0Invalid_argument#if %start< is not a valid position in !s!.@@@@@@@@@@@@4Bytes.rcontains_from37rcontains_from s stop c/ tests if byte !c, appears in !s5 before position &stop+1!.@@@@@@@@0Invalid_argument#if (stop < 0$ or &stop+1 is not a valid position in !s!.@@@@@@@@@@@@5Bytes.uppercase_ascii3 wReturn a copy of the argument, with all lowercase letters translated to uppercase, using the US-ASCII character set.@@@@:4.03 (4.05 in BytesLabels)@@@@@@@@@@@@ 5Bytes.lowercase_ascii3 wReturn a copy of the argument, with all uppercase letters translated to lowercase, using the US-ASCII character set.@@@@:4.03 (4.05 in BytesLabels)@@@@@@@ @@@@@6Bytes.capitalize_ascii3 nReturn a copy of the argument, with the first character set to uppercase, using the US-ASCII character set.@@@@:4.03 (4.05 in BytesLabels)@@@@@@@ @ @@@@8Bytes.uncapitalize_ascii3 nReturn a copy of the argument, with the first character set to lowercase, using the US-ASCII character set.@@@@:4.03 (4.05 in BytesLabels)@@@@@@@@ @@@@#'Bytes.t3 (An alias for the type of byte sequences.@@@@@@@@@@@@@@A@@@-Bytes.compare3 OThe comparison function for byte sequences, with the same specification as .Stdlib.compare@@7. Along with the type !t4, this function 'compare3 allows the module %Bytes . to be passed as argument to the functors (Set.Make@@% and (Map.Make@@!.@@@@@@@@@@@@"@b#@e@@@@+Bytes.equal3 )The equality function for byte sequences.@@@@:4.03 (4.05 in BytesLabels)@@@@@@@@v@y@@@@1Bytes.starts_with3,starts_with )~prefix s$ is $true0 if and only if !s1 starts with &prefix!.@@@@$4.13@@@@@@@@@@@@@/Bytes.ends_with3*ends_with )~suffix s$ is $true0 if and only if !s+ ends with &suffix!.@@@@$4.13@@@@@@@,@,+@'@@@@A&unsafe 'Unsafe conversions (for advanced users)@@ O This section describes unsafe, low-level conversion functions between %bytes% and &string . They do not copy the internal data; used improperly, they can break the immutability invariant on strings. They are available for expert library authors, but for most purposes you should use the always-correct D@) and ŐD@) instead.@6Bytes.unsafe_to_string3 /Unsafely convert a byte sequence into a string.@ To reason about the use of 0unsafe_to_string , it is convenient to consider an "ownership" discipline. A piece of code that manipulates some data "owns" it; there are several disjoint ownership modes, including: 6Unique ownership: the data may be accessed and mutated@ \Shared ownership: the data has several owners, that may only access it, not mutate it.@@  Unique ownership is linear: passing the data to another piece of code means giving up ownership (we cannot write the data again). A unique owner may decide to make the data shared (giving up mutation rights on it), but shared data may not become uniquely-owned again.@$ 2unsafe_to_string s < can only be used when the caller owns the byte sequence !s U -- either uniquely or as shared immutable data. The caller gives up ownership of !s 0, and gains ownership of the returned string.@ L There are two valid use-cases that respect this ownership discipline:@ 1. Creating a string by initializing and mutating a byte sequence that is never changed after initialization is performed.@$ let string_init len f : string = let s = Bytes.create len in for i = 0 to len - 1 do Bytes.set s i (f i) done; Bytes.unsafe_to_string s @ 4 This function is safe because the byte sequence !s , will never be accessed or mutated after 0unsafe_to_string3 is called. The +string_init< code gives up ownership of !s E, and returns the ownership of the resulting string to its caller.@ $ Note that it would be unsafe if !s : was passed as an additional parameter to the function !f @ as it could escape this way and be mutated in the future -- +string_init? would give up ownership of !s/ to pass it to !f5, and could not call 0unsafe_to_string+ safely.@9 We have provided the +String.init@@", *String.map@@( and +String.mapi@@ X functions to cover most cases of building new strings. You should prefer those over )to_string' or 0unsafe_to_string5 whenever applicable.@ 2. Temporarily giving ownership of a byte sequence to a function that expects a uniquely owned string and returns ownership back, so that we can mutate the sequence again after the call ended.@$ N let bytes_length (s : bytes) = String.length (Bytes.unsafe_to_string s) @ - In this use-case, we do not promise that !s , will never be mutated after the call to .bytes_length s&. The -String.length@@ \ function temporarily borrows unique ownership of the byte sequence (and sees it as a &string K), but returns this ownership back to the caller, which may assume that !s j is still a valid byte sequence after the call. Note that this is only correct because we know that -String.length@@ h does not capture its argument -- it could escape by a side-channel such as a memoization combinator.@> The caller may not mutate !s while the string is borrowed (it has temporarily given up ownership). This affects concurrent programs, but also higher-order functions: if -String.length@@ + returned a closure to be called later, !s T should not be mutated until this closure is fully applied and returns ownership.@@@@@@@@@@@@0@ 1@@@@)6Bytes.unsafe_of_string3 SUnsafely convert a shared string to a byte sequence that should not be mutated.@ . The same ownership discipline that makes 0unsafe_to_string8 correct applies to 0unsafe_of_string 2: you may use it if you were the owner of the &string ( value, and you will own the return %bytes2 in the same mode.@ In practice, unique ownership of string values is extremely difficult to reason about correctly. You should always assume strings are shared, never uniquely owned.@ l For example, string literals are implicitly shared by the compiler, so you never uniquely own them.@% T let incorrect = Bytes.unsafe_of_string "hello" let s = Bytes.of_string "hello" @ H The first declaration is incorrect, because the string literal '"hello" S could be shared by the compiler with other parts of the program, and mutating )incorrect a is a bug. You must always use the second version, which performs a copy and is thus correct.@ Assuming unique ownership of strings that are not string literals, but are (partly) built from string literals, is also incorrect. For example, mutating ) and previously used the &string7 type for this purpose.@@@@@@@@@@@@@ f@@@@3Bytes.split_on_char33split_on_char sep s > returns the list of all (possibly empty) subsequences of !s; that are delimited by the #sep3 character. If !s , is empty, the result is the singleton list '[empty]!.@ D The function's output is specified by the following invariants:@6The list is not empty.@ !Concatenating its elements using #sep B as a separator returns a byte sequence equal to the input ( EBytes.concat (Bytes.make 1 sep) (Bytes.split_on_char sep s) = s").@ ,No byte sequence in the result contains the #sep+ character.@@@@@@$4.13@@@@@@@@ @ @@@@A@)Iterators@@,Bytes.to_seq3 Iterate on the string, in increasing index order. Modifications of the string during iteration will be reflected in the sequence.@@@@$4.07@@@@@@@@ @@@@-Bytes.to_seqi3 HIterate on the string, in increasing order, yielding indices along chars@@@@$4.07@@@@@@@@ @@@@,Bytes.of_seq3 "Create a string from the generator@@@@$4.07@@@@@@@@ @@@@A#utf:UTF codecs and validations@@B%utf_8%UTF-8@@5Bytes.get_utf_8_uchar33get_utf_8_uchar b i % decodes an UTF-8 character at index !i( in !b!.@@@@@@@@@@@@@ $@ '@@@@5Bytes.set_utf_8_uchar35set_utf_8_uchar b i u/ UTF-8 encodes !u* at index !i$ in !b % and returns the number of bytes !n # that were written starting at !i%. If !n$ is !0 & there was not enough space to encode !u( at !i% and !b E was left untouched. Otherwise a new character can be encoded at %i + n!.@@@@@@@@@@@@@ {@ ~@ @@@@4Bytes.is_valid_utf_830is_valid_utf_8 b$ is $true0 if and only if !b? contains valid UTF-8 data.@@@@@@@@@@@@@ @@@@ƠB(utf_16be(UTF-16BE@@8Bytes.get_utf_16be_uchar36get_utf_16be_uchar b i , decodes an UTF-16BE character at index !i$ in !b!.@@@@@@@@@@@@@ @ @@@@̠8Bytes.set_utf_16be_uchar38set_utf_16be_uchar b i u2 UTF-16BE encodes !u* at index !i$ in !b % and returns the number of bytes !n # that were written starting at !i%. If !n$ is !0 & there was not enough space to encode !u( at !i% and !b E was left untouched. Otherwise a new character can be encoded at %i + n!.@@@@@@@@@@@@@ @  @ #@@@@7Bytes.is_valid_utf_16be33is_valid_utf_16be b$ is $true0 if and only if !b " contains valid UTF-16BE data.@@@@@@@@@@@@ @ A @@@@B(utf_16le(UTF-16LE@@8Bytes.get_utf_16le_uchar36get_utf_16le_uchar b i , decodes an UTF-16LE character at index !i$ in !b!.@@@@@@@@@@@@@ h@ k@@@@8Bytes.set_utf_16le_uchar38set_utf_16le_uchar b i u2 UTF-16LE encodes !u* at index !i$ in !b % and returns the number of bytes !n # that were written starting at !i%. If !n$ is !0 & there was not enough space to encode !u( at !i% and !b E was left untouched. Otherwise a new character can be encoded at %i + n!.@@@@@@@@@@@@M@ N@ G@ C@@@@67Bytes.is_valid_utf_16le33is_valid_utf_16le b$ is $true0 if and only if !b " contains valid UTF-16LE data.@@@@@@@@@@@@F@ G@@@@ Bytes.set b i (Char.chr (f (Char.code x)))) b let d1 = Domain.spawn (update b (fun x -> x + 1)) let d2 = Domain.spawn (update b (fun x -> 2 * x + 1)) let () = Domain.join d1; Domain.join d2 8 the bytes sequence !b 0 may contain a non-deterministic mixture of #'!'", #'A'", #'B'&, and #'C'( values.@ : After executing this code, each byte of the sequence !b+ is either #'!'&, #'A'", #'B'%, or #'C' k. If atomicity is required, then the user must implement their own synchronization (for example, using 'Mutex.t@@").@% B/bytes_data_race*Data races@@ If two domains only access disjoint parts of a byte sequence, then the observed behaviour is the equivalent to some sequential interleaving of the operations from the two domains.@ % A data race is said to occur when two domains access the same byte without synchronization and at least one of the accesses is a write. In the absence of data races, the observed behaviour is equivalent to some sequential interleaving of the operations from different domains.@ Whenever possible, data races should be avoided by using synchronization to mediate the accesses to the elements of the sequence.@ H Indeed, in the presence of data races, programs will not crash but the observed behaviour may not be equivalent to any sequential interleaving of operations from different domains. Nevertheless, even in the presence of data races, a read operation will return the value of some prior write to that location.@% B2bytes_mixed_access4Mixed-size accesses @@  Another subtle point is that if a data race involves mixed-size writes and reads to the same location, the order in which those writes and reads are observed by domains is not specified. For instance, the following code write sequentially a 32-bit integer and a $char3 to the same index j let b = Bytes.make 10 '\000' let d1 = Domain.spawn (fun () -> Bytes.set_int32_ne b 0 100; b.[0] <- 'd' ) @ E In this situation, a domain that observes the write of 'd' to b.!0 < is not guaranteed to also observe the write to indices !1", !2%, or !3!.@@@[@@A%Uchar#Seq@@@@@