Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file base_bigstring.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918open!BasemoduleBigstring0=structtypet=(char,Stdlib.Bigarray.int8_unsigned_elt,Stdlib.Bigarray.c_layout)Stdlib.Bigarray.Array1.tendmoduleArray1=structtype('a,'b,'c)t=('a,'b,'c)Stdlib.Bigarray.Array1.texternalget:('a,'b,'c)t->int->'a="%caml_ba_ref_1"externalset:('a,'b,'c)t->int->'a->unit="%caml_ba_set_1"externalunsafe_get:('a,'b,'c)t->int->'a="%caml_ba_unsafe_ref_1"externalunsafe_set:(('a,'b,'c)t[@local_opt])->int->'a->unit="%caml_ba_unsafe_set_1"externaldim:(('a,'b,'c)t[@local_opt])->int="%caml_ba_dim_1"endincludeBigstring0externalaux_create:size:int->t="bigstring_alloc_v2"letsprintf=Printf.sprintf(* One needs to use [Caml.Sys.word_size] so that its value is known at compile-time. *)letarch_sixtyfour=Stdlib.Sys.word_size=64letarch_big_endian=Stdlib.Sys.big_endianletnot_on_32bit=Stdlib.Sys.word_size>32letcreatesize=(* This check is important because [aux_create ~size:(-1)] raises [Out_of_memory], which
could be confusing during debugging. *)ifsize<0theninvalid_arg(sprintf"create: size = %d < 0"size);aux_create~size;;letlength=Array1.dimexternalis_mmapped:t->bool="bigstring_is_mmapped_stub"[@@noalloc]letinitn~f=lett=createninfori=0ton-1dot.{i}<-fidone;t;;let[@inlinenever]check_args_slow~loc~pos~len(bstr:t)=ifpos<0theninvalid_arg(loc^": pos < 0");iflen<0theninvalid_arg(loc^": len < 0");letbstr_len=lengthbstrin(* Be careful with overflow! We could have bogons like [pos = Int.max_value] or [len =
Int.max_value] passed by the user. *)ifbstr_len-pos<lentheninvalid_arg(sprintf"Bigstring.%s: length(bstr) < pos + len"loc);;let[@inlinealways]check_args~loc~pos~len(bstr:t)=letopenBool.Non_short_circuitinginletbstr_len=lengthbstrinifpos<0||len<0||bstr_len-pos<lenthencheck_args_slow~loc~pos~lenbstr;;letget_opt_lenbstr~pos=function|Somelen->len|None->lengthbstr-pos;;(* Blitting *)externalunsafe_blit:src:(t[@local_opt])->src_pos:int->dst:(t[@local_opt])->dst_pos:int->len:int->unit="bigstring_blit_stub"[@@noalloc](* Exposing the external version of get/set supports better inlining. *)externalget:t->int->char="%caml_ba_ref_1"externalunsafe_get:t->int->char="%caml_ba_unsafe_ref_1"externalset:t->int->char->unit="%caml_ba_set_1"externalunsafe_set:t->int->char->unit="%caml_ba_unsafe_set_1"moduleBigstring_sequence=structtypenonrect=tletcreate~len=createlenletlength=lengthendmoduleBytes_sequence=structtypet=bytesletcreate~len=Bytes.createlenletlength=Bytes.lengthendincludeBlit.Make(structincludeBigstring_sequenceletunsafe_blit=unsafe_blitend)moduleFrom_bytes=Blit.Make_distinct(Bytes_sequence)(structexternalunsafe_blit:src:(bytes[@local_opt])->src_pos:int->dst:(t[@local_opt])->dst_pos:int->len:int->unit="bigstring_blit_bytes_bigstring_stub"[@@noalloc]includeBigstring_sequenceend)moduleTo_bytes=Blit.Make_distinct(Bigstring_sequence)(structexternalunsafe_blit:src:(t[@local_opt])->src_pos:int->dst:(bytes[@local_opt])->dst_pos:int->len:int->unit="bigstring_blit_bigstring_bytes_stub"[@@noalloc]includeBytes_sequenceend)moduleFrom_string=Blit.Make_distinct(structtypet=stringletlength=String.lengthend)(structexternalunsafe_blit:src:(string[@local_opt])->src_pos:int->dst:(t[@local_opt])->dst_pos:int->len:int->unit="bigstring_blit_string_bigstring_stub"[@@noalloc]includeBigstring_sequenceend)moduleTo_string=structincludeTo_bytesincludeBlit.Make_to_string(Bigstring0)(To_bytes)endletof_string=From_string.suboletof_bytes=From_bytes.suboletto_string=To_string.suboletto_bytes=To_bytes.suboletsexp_of_tt=Sexp.Atom(to_stringt)lett_of_sexp:Sexp.t->t=function|Atomstr->of_stringstr|List_assexp->Sexplib0.Sexp_conv.of_sexp_error"bigstring_of_sexp: atom needed"sexp;;letcopyt:t=subt~pos:0~len:(lengtht)letconcat=letappend~src~dst~dst_pos_ref=letlen=lengthsrcinletsrc_pos=0inletdst_pos=!dst_pos_refinblit~dst~dst_pos~src~src_pos~len;dst_pos_ref:=dst_pos+leninfun?seplist->matchlistwith|[]->create0|head::tail->lethead_len=lengthheadinletsep_len=Option.value_mapsep~f:(funt->lengtht)~default:0inlettail_count=List.lengthtailinletlen=head_len+(sep_len*tail_count)+List.sum(moduleInt)tail~f:(funt->lengtht)inletdst=createleninletdst_pos_ref=ref0inappend~src:head~dst~dst_pos_ref;List.itertail~f:(funsrc->(matchsepwith|None->()|Somesep->append~src:sep~dst~dst_pos_ref);append~src~dst~dst_pos_ref);assert(!dst_pos_ref=len);dst;;externalunsafe_memset:t->pos:int->len:int->char->unit="bigstring_memset_stub"[@@noalloc]letmemsett~pos~lenc=Ordered_collection_common.check_pos_len_exn~pos~len~total_length:(lengtht);unsafe_memsett~pos~lenc;;(* Comparison *)externalunsafe_memcmp:t->pos1:int->t->pos2:int->len:int->int="bigstring_memcmp_stub"[@@noalloc]letmemcmpt1~pos1t2~pos2~len=Ordered_collection_common.check_pos_len_exn~pos:pos1~len~total_length:(lengtht1);Ordered_collection_common.check_pos_len_exn~pos:pos2~len~total_length:(lengtht2);unsafe_memcmpt1~pos1t2~pos2~len;;externalunsafe_memcmp_bytes:t->pos1:int->Bytes.t->pos2:int->len:int->int="bigstring_memcmp_bytes_stub"[@@noalloc]letmemcmp_bytest~pos1bytes~pos2~len=Ordered_collection_common.check_pos_len_exn~pos:pos1~len~total_length:(lengtht);Ordered_collection_common.check_pos_len_exn~pos:pos2~len~total_length:(Bytes.lengthbytes);unsafe_memcmp_bytest~pos1bytes~pos2~len;;letmemcmp_stringt~pos1str~pos2~len=memcmp_bytest~pos1(Bytes.unsafe_of_string_promise_no_mutationstr)~pos2~len[@nontail];;letcomparet1t2=ifphys_equalt1t2then0else(letlen1=lengtht1inletlen2=lengtht2inletlen=Int.minlen1len2inmatchunsafe_memcmpt1~pos1:0t2~pos2:0~lenwith|0->iflen1<len2then-1elseiflen1>len2then1else0|n->n);;externalinternalhash_fold_bigstring:Hash.state->t->Hash.state="internalhash_fold_bigstring"[@@noalloc]let_making_sure_the_C_binding_takes_an_int(x:Hash.state)=(x:>int)lethash_fold_t=internalhash_fold_bigstringlethash=Ppx_hash_lib.Std.Hash.of_foldhash_fold_ttypet_frozen=t[@@derivingcompare,hash,sexp]letequalt1t2=ifphys_equalt1t2thentrueelse(letlen1=lengtht1inletlen2=lengtht2inInt.equallen1len2&&Int.equal(unsafe_memcmpt1~pos1:0t2~pos2:0~len:len1)0);;(* Search *)externalunsafe_find:t->char->pos:int->len:int->int="bigstring_find"[@@noalloc]externalunsafe_memmem:haystack:t->needle:t->haystack_pos:int->haystack_len:int->needle_pos:int->needle_len:int->int="bigstring_memmem_bytecode""bigstring_memmem"[@@noalloc]letfind?(pos=0)?lenchrbstr=letlen=get_opt_lenbstr~poslenincheck_args~loc:"find"~pos~lenbstr;letres=unsafe_findbstrchr~pos~leninifres<0thenNoneelseSomeres;;letmemmem~haystack~needle?(haystack_pos=0)?haystack_len?(needle_pos=0)?needle_len()=lethaystack_len=get_opt_lenhaystack~pos:haystack_poshaystack_leninletneedle_len=get_opt_lenneedle~pos:needle_posneedle_leninletres=unsafe_memmem~haystack~needle~haystack_pos~haystack_len~needle_pos~needle_leninifres<0thenNoneelseSomeres;;(* vim: set filetype=ocaml : *)(* Binary-packing like accessors *)externalint32_of_int:int->int32="%int32_of_int"externalint32_to_int:int32->int="%int32_to_int"externalint64_of_int:int->int64="%int64_of_int"externalint64_to_int:int64->int="%int64_to_int"externalswap16:int->int="%bswap16"externalswap32:int32->int32="%bswap_int32"externalswap64:int64->int64="%bswap_int64"externalunsafe_get_16:t->int->int="%caml_bigstring_get16u"externalunsafe_get_32:t->int->int32="%caml_bigstring_get32u"externalunsafe_get_64:t->int->(int64[@local_opt])="%caml_bigstring_get64u"externalunsafe_set_16:(t[@local_opt])->int->int->unit="%caml_bigstring_set16u"externalunsafe_set_32:(t[@local_opt])->int->int32->unit="%caml_bigstring_set32u"externalunsafe_set_64:t->int->int64->unit="%caml_bigstring_set64u"let[@inlinealways]get_16(t:t)(pos:int):int=check_args~loc:"get_16"~pos~len:2t;unsafe_get_16tpos;;let[@inlinealways]get_32(t:t)(pos:int):int32=check_args~loc:"get_32"~pos~len:4t;unsafe_get_32tpos;;let[@inlinealways]get_64(t:t)(pos:int):int64=check_args~loc:"get_64"~pos~len:8t;unsafe_get_64tpos;;let[@inlinealways]set_16_trunc(t:t)(pos:int)(v:int):unit=check_args~loc:"set_16"~pos~len:2t;unsafe_set_16tposv;;let[@inlinealways]set_32(t:t)(pos:int)(v:int32):unit=check_args~loc:"set_32"~pos~len:4t;unsafe_set_32tposv;;let[@inlinealways]set_64(t:t)(pos:int)(v:int64):unit=check_args~loc:"set_64"~pos~len:8t;unsafe_set_64tposv;;letsign_extend_16u=(ulsl(Int.num_bits-16))asr(Int.num_bits-16)letcheck_valid_uint16x~loc=ifx<0||x>0xFFFFtheninvalid_arg(sprintf"%s: %d is not a valid unsigned 16-bit integer"locx);;letcheck_valid_int16x~loc=ifx<-0x8000||x>0x7FFFtheninvalid_arg(sprintf"%s: %d is not a valid (signed) 16-bit integer"locx);;letcheck_valid_uint8x~loc=ifx<0||x>0xFFtheninvalid_arg(sprintf"%s: %d is not a valid unsigned 8-bit integer"locx);;letcheck_valid_int8x~loc=ifx<-0x80||x>0x7Ftheninvalid_arg(sprintf"%s: %d is not a valid (signed) 8-bit integer"locx);;letcheck_valid_int32=ifnotarch_sixtyfourthenfun_~loc:_->()elsefunx~loc->ifx>=-1lsl31&&x<1lsl31then()elseinvalid_arg(sprintf"%s: %d is not a valid (signed) 32-bit integer"locx);;letcheck_valid_uint32=ifnotarch_sixtyfourthenfunx~loc->ifx>=0then()elseinvalid_arg(sprintf"%s: %d is not a valid unsigned 32-bit integer"locx)elsefunx~loc->ifx>=0&&x<1lsl32then()elseinvalid_arg(sprintf"%s: %d is not a valid unsigned 32-bit integer"locx);;letcheck_valid_uint64x~loc=ifx>=0then()elseinvalid_arg(sprintf"%s: %d is not a valid unsigned 64-bit integer"locx);;letunsafe_read_int16t~pos=sign_extend_16(unsafe_get_16tpos)letunsafe_read_int16_swapt~pos=sign_extend_16(swap16(unsafe_get_16tpos))letunsafe_write_int16t~posx=unsafe_set_16tposxletunsafe_write_int16_swapt~posx=unsafe_set_16tpos(swap16x)letread_int16t~pos=sign_extend_16(get_16tpos)letread_int16_swapt~pos=sign_extend_16(swap16(get_16tpos))letwrite_int16_exnt~posx=check_valid_int16x~loc:"Bigstring.write_int16";set_16_trunctposx;;letwrite_int16_swap_exnt~posx=(* Omit "_swap" from the error message it's bi-endian. *)check_valid_int16x~loc:"Bigstring.write_int16";set_16_trunctpos(swap16x);;letunsafe_read_uint16t~pos=unsafe_get_16tposletunsafe_read_uint16_swapt~pos=swap16(unsafe_get_16tpos)letunsafe_write_uint16t~posx=unsafe_set_16tposxletunsafe_write_uint16_swapt~posx=unsafe_set_16tpos(swap16x)letread_uint16t~pos=get_16tposletread_uint16_swapt~pos=swap16(get_16tpos)letwrite_uint16_exnt~posx=check_valid_uint16x~loc:"Bigstring.write_uint16";set_16_trunctposx;;letwrite_uint16_swap_exnt~posx=(* Omit "_swap" from the error message it's bi-endian. *)check_valid_uint16x~loc:"Bigstring.write_uint16";set_16_trunctpos(swap16x);;letunsafe_read_int32_intt~pos=int32_to_int(unsafe_get_32tpos)letunsafe_read_int32_int_swapt~pos=int32_to_int(swap32(unsafe_get_32tpos))letunsafe_read_int32t~pos=unsafe_get_32tposletunsafe_read_int32_swapt~pos=swap32(unsafe_get_32tpos)letunsafe_write_int32t~posx=unsafe_set_32tposxletunsafe_write_int32_swapt~posx=unsafe_set_32tpos(swap32x)letunsafe_write_int32_intt~posx=unsafe_set_32tpos(int32_of_intx)letunsafe_write_int32_int_swapt~posx=unsafe_set_32tpos(swap32(int32_of_intx))letread_int32_intt~pos=int32_to_int(get_32tpos)letread_int32_int_swapt~pos=int32_to_int(swap32(get_32tpos))letread_int32t~pos=get_32tposletread_int32_swapt~pos=swap32(get_32tpos)letwrite_int32t~posx=set_32tposxletwrite_int32_swapt~posx=set_32tpos(swap32x)letwrite_int32_int_exnt~posx=check_valid_int32x~loc:"Bigstring.write_int32_int";set_32tpos(int32_of_intx);;letwrite_int32_int_swap_exnt~posx=(* Omit "_swap" from the error message it's bi-endian. *)check_valid_int32x~loc:"Bigstring.write_int32_int";set_32tpos(swap32(int32_of_intx));;let[@inlinealways]unsafe_read_int64_intt~pos=int64_to_int(unsafe_get_64tpos)let[@inlinealways]unsafe_read_int64_int_swapt~pos=int64_to_int(swap64(unsafe_get_64tpos));;let[@inlinealways]unsafe_read_int64t~pos=unsafe_get_64tposlet[@inlinealways]unsafe_read_int64_swapt~pos=swap64(unsafe_get_64tpos)let[@inlinealways]unsafe_write_int64t~posx=unsafe_set_64tposxlet[@inlinealways]unsafe_write_int64_swapt~posx=unsafe_set_64tpos(swap64x)let[@inlinealways]unsafe_write_int64_intt~posx=unsafe_set_64tpos(int64_of_intx)let[@inlinealways]unsafe_write_int64_int_swapt~posx=unsafe_set_64tpos(swap64(int64_of_intx));;let[@inlinealways]read_int64_intt~pos=int64_to_int(get_64tpos)let[@inlinealways]read_int64_int_swapt~pos=int64_to_int(swap64(get_64tpos))let[@inlinealways]read_int64t~pos=get_64tposlet[@inlinealways]read_int64_swapt~pos=swap64(get_64tpos)letwrite_int64t~posx=set_64tposxletwrite_int64_swapt~posx=set_64tpos(swap64x)letwrite_int64_intt~posx=set_64tpos(int64_of_intx)letwrite_int64_int_swapt~posx=set_64tpos(swap64(int64_of_intx))letunsafe_get_int16_be=ifarch_big_endianthenunsafe_read_int16elseunsafe_read_int16_swap;;letunsafe_get_int16_le=ifarch_big_endianthenunsafe_read_int16_swapelseunsafe_read_int16;;letunsafe_get_uint16_be=ifarch_big_endianthenunsafe_read_uint16elseunsafe_read_uint16_swap;;letunsafe_get_uint16_le=ifarch_big_endianthenunsafe_read_uint16_swapelseunsafe_read_uint16;;letget_int16_be=ifarch_big_endianthenread_int16elseread_int16_swapletget_int16_le=ifarch_big_endianthenread_int16_swapelseread_int16letget_uint16_be=ifarch_big_endianthenread_uint16elseread_uint16_swapletget_uint16_le=ifarch_big_endianthenread_uint16_swapelseread_uint16letunsafe_set_int16_be=ifarch_big_endianthenunsafe_write_int16elseunsafe_write_int16_swap;;letunsafe_set_int16_le=ifarch_big_endianthenunsafe_write_int16_swapelseunsafe_write_int16;;letunsafe_set_uint16_be=ifarch_big_endianthenunsafe_write_uint16elseunsafe_write_uint16_swap;;letunsafe_set_uint16_le=ifarch_big_endianthenunsafe_write_uint16_swapelseunsafe_write_uint16;;letset_int16_be_exn=ifarch_big_endianthenwrite_int16_exnelsewrite_int16_swap_exnletset_int16_le_exn=ifarch_big_endianthenwrite_int16_swap_exnelsewrite_int16_exnletset_uint16_be_exn=ifarch_big_endianthenwrite_uint16_exnelsewrite_uint16_swap_exn;;letset_uint16_le_exn=ifarch_big_endianthenwrite_uint16_swap_exnelsewrite_uint16_exn;;letunsafe_get_int32_t_be=ifarch_big_endianthenunsafe_read_int32elseunsafe_read_int32_swap;;letunsafe_get_int32_t_le=ifarch_big_endianthenunsafe_read_int32_swapelseunsafe_read_int32;;letunsafe_set_int32_t_be=ifarch_big_endianthenunsafe_write_int32elseunsafe_write_int32_swap;;letunsafe_set_int32_t_le=ifarch_big_endianthenunsafe_write_int32_swapelseunsafe_write_int32;;letget_int32_t_be=ifarch_big_endianthenread_int32elseread_int32_swapletget_int32_t_le=ifarch_big_endianthenread_int32_swapelseread_int32letset_int32_t_be=ifarch_big_endianthenwrite_int32elsewrite_int32_swapletset_int32_t_le=ifarch_big_endianthenwrite_int32_swapelsewrite_int32letunsafe_get_int32_be=ifarch_big_endianthenunsafe_read_int32_intelseunsafe_read_int32_int_swap;;letunsafe_get_int32_le=ifarch_big_endianthenunsafe_read_int32_int_swapelseunsafe_read_int32_int;;letunsafe_set_int32_be=ifarch_big_endianthenunsafe_write_int32_intelseunsafe_write_int32_int_swap;;letunsafe_set_int32_le=ifarch_big_endianthenunsafe_write_int32_int_swapelseunsafe_write_int32_int;;letget_int32_be=ifarch_big_endianthenread_int32_intelseread_int32_int_swapletget_int32_le=ifarch_big_endianthenread_int32_int_swapelseread_int32_intletset_int32_be_exn=ifarch_big_endianthenwrite_int32_int_exnelsewrite_int32_int_swap_exn;;letset_int32_le_exn=ifarch_big_endianthenwrite_int32_int_swap_exnelsewrite_int32_int_exn;;letunsafe_get_int64_be_trunc=ifarch_big_endianthenunsafe_read_int64_intelseunsafe_read_int64_int_swap;;letunsafe_get_int64_le_trunc=ifarch_big_endianthenunsafe_read_int64_int_swapelseunsafe_read_int64_int;;letunsafe_set_int64_be=ifarch_big_endianthenunsafe_write_int64_intelseunsafe_write_int64_int_swap;;letunsafe_set_int64_le=ifarch_big_endianthenunsafe_write_int64_int_swapelseunsafe_write_int64_int;;letget_int64_be_trunc=ifarch_big_endianthenread_int64_intelseread_int64_int_swapletget_int64_le_trunc=ifarch_big_endianthenread_int64_int_swapelseread_int64_intletset_int64_be=ifarch_big_endianthenwrite_int64_intelsewrite_int64_int_swapletset_int64_le=ifarch_big_endianthenwrite_int64_int_swapelsewrite_int64_intletunsafe_get_int64_t_be=ifarch_big_endianthenunsafe_read_int64elseunsafe_read_int64_swap;;letunsafe_get_int64_t_le=ifarch_big_endianthenunsafe_read_int64_swapelseunsafe_read_int64;;letunsafe_set_int64_t_be=ifarch_big_endianthenunsafe_write_int64elseunsafe_write_int64_swap;;letunsafe_set_int64_t_le=ifarch_big_endianthenunsafe_write_int64_swapelseunsafe_write_int64;;letget_stringt~pos~len=letbytes=Bytes.createleninTo_bytes.blit~src:t~src_pos:pos~dst:bytes~dst_pos:0~len;Bytes.unsafe_to_string~no_mutation_while_string_reachable:bytes;;letunsafe_get_stringt~pos~len=letbytes=Bytes.createleninTo_bytes.unsafe_blit~src:t~src_pos:pos~dst:bytes~dst_pos:0~len;Bytes.unsafe_to_string~no_mutation_while_string_reachable:bytes;;moduleLocal=structlet[@inlinealways]unsafe_read_int64_localt~pos=Int64.(+)0L(unsafe_read_int64t~pos);;let[@inlinealways]unsafe_read_int64_swap_localt~pos=Int64.(+)0L(unsafe_read_int64_swapt~pos);;let[@inlinealways]read_int64_localt~pos=Int64.(+)0L(read_int64t~pos)let[@inlinealways]read_int64_swap_localt~pos=Int64.(+)0L(read_int64_swapt~pos);;letunsafe_get_int64_t_be=ifarch_big_endianthenunsafe_read_int64_localelseunsafe_read_int64_swap_local;;letunsafe_get_int64_t_le=ifarch_big_endianthenunsafe_read_int64_swap_localelseunsafe_read_int64_local;;letget_int64_t_be=ifarch_big_endianthenread_int64_localelseread_int64_swap_localletget_int64_t_le=ifarch_big_endianthenread_int64_swap_localelseread_int64_localletget_stringt~pos~len=letbytes=Bytes.create_localleninTo_bytes.blit~src:t~src_pos:pos~dst:bytes~dst_pos:0~len;Bytes.unsafe_to_string~no_mutation_while_string_reachable:bytes;;letunsafe_get_stringt~pos~len=letbytes=Bytes.create_localleninTo_bytes.unsafe_blit~src:t~src_pos:pos~dst:bytes~dst_pos:0~len;Bytes.unsafe_to_string~no_mutation_while_string_reachable:bytes;;endletget_int64_t_be=ifarch_big_endianthenread_int64elseread_int64_swapletget_int64_t_le=ifarch_big_endianthenread_int64_swapelseread_int64letset_int64_t_be=ifarch_big_endianthenwrite_int64elsewrite_int64_swapletset_int64_t_le=ifarch_big_endianthenwrite_int64_swapelsewrite_int64letint64_conv_error()=failwith"unsafe_read_int64: value cannot be represented unboxed!";;letuint64_conv_error()=failwith"unsafe_read_uint64: value cannot be represented unboxed!";;let[@inlinealways]int64_to_int_exnn=letn'=int64_to_intnin(* The compiler will eliminate any boxing here. *)ifInt64.(=)(Int64.of_intn')nthenn'elseint64_conv_error();;let[@inlinealways]uint64_to_int_exnn=ifarch_sixtyfourthenifInt64.(n>=0L&&n<0x4000_0000_0000_0000L)thenint64_to_intnelseuint64_conv_error()elseifInt64.(n>=0L&&n<0x0000_0000_4000_0000L)thenint64_to_intnelseuint64_conv_error();;let[@inline]unsafe_get_int64_be_exnt~pos=int64_to_int_exn(unsafe_get_int64_t_bet~pos);;let[@inline]unsafe_get_int64_le_exnt~pos=int64_to_int_exn(unsafe_get_int64_t_let~pos);;letget_int64_be_exnt~pos=int64_to_int_exn(get_int64_t_bet~pos)letget_int64_le_exnt~pos=int64_to_int_exn(get_int64_t_let~pos)let[@inline]unsafe_get_uint64_be_exnt~pos=uint64_to_int_exn(unsafe_get_int64_t_bet~pos);;let[@inline]unsafe_get_uint64_le_exnt~pos=uint64_to_int_exn(unsafe_get_int64_t_let~pos);;letget_uint64_be_exnt~pos=uint64_to_int_exn(get_int64_t_bet~pos)letget_uint64_le_exnt~pos=uint64_to_int_exn(get_int64_t_let~pos)letunsafe_set_uint64_be=unsafe_set_int64_beletunsafe_set_uint64_le=unsafe_set_int64_leletset_uint64_be_exnt~posn=check_valid_uint64~loc:"Bigstring.set_uint64_be_exn"n;set_int64_bet~posn;;letset_uint64_le_exnt~posn=check_valid_uint64~loc:"Bigstring.set_uint64_le_exn"n;set_int64_let~posn;;(* Type annotations on the [t]s are important here: in order for the compiler to generate
optimized code, it needs to know the fully instantiated type of the bigarray. This is
because the type of the bigarray encodes the element kind and the layout of the
bigarray. Without the annotation the compiler generates a C call to the generic access
functions. *)letunsafe_set_uint8(t:t)~posn=Array1.unsafe_settpos(Char.unsafe_of_intn)letunsafe_set_int8(t:t)~posn=(* In all the set functions where there are these tests, it looks like the test could be
removed, since they are only changing the values of the bytes that are not
written. *)letn=ifn<0thenn+256elseninArray1.unsafe_settpos(Char.unsafe_of_intn);;letunsafe_get_uint8(t:t)~pos=Char.to_int(Array1.unsafe_gettpos)letunsafe_get_int8(t:t)~pos=letn=Char.to_int(Array1.unsafe_gettpos)inifn>=128thenn-256elsen;;letset_uint8_exn(t:t)~posn=check_valid_uint8~loc:"Bigstring.set_uint8_exn"n;Array1.settpos(Char.unsafe_of_intn);;letset_int8_exn(t:t)~posn=check_valid_int8~loc:"Bigstring.set_int8_exn"n;letn=ifn<0thenn+256elseninArray1.settpos(Char.unsafe_of_intn);;letget_uint8(t:t)~pos=Char.to_int(Array1.gettpos)letget_int8(t:t)~pos=letn=Char.to_int(Array1.gettpos)inifn>=128thenn-256elsen;;letmask32_n=Stdlib.Nativeint.(sub(shift_left1n32)1n)let[@inlinealways]uint32_of_int32_tn=ifnot_on_32bitthen(* use Caml.Nativeint to ensure inlining even without x-library-inlining *)Stdlib.Nativeint.(to_int(logand(of_int32n)mask32_n))elseint32_to_intn;;let[@inline]unsafe_set_uint32_let~posn=unsafe_set_int32_t_let~pos(int32_of_intn)let[@inline]unsafe_set_uint32_bet~posn=unsafe_set_int32_t_bet~pos(int32_of_intn)let[@inline]unsafe_get_uint32_let~pos=uint32_of_int32_t(unsafe_get_int32_t_let~pos);;let[@inline]unsafe_get_uint32_bet~pos=uint32_of_int32_t(unsafe_get_int32_t_bet~pos);;letset_uint32_le_exnt~posn=check_valid_uint32~loc:"Bigstring.set_uint32_le_exn"n;letn=ifnot_on_32bit&&n>=1lsl31thenn-(1lsl32)elseninset_int32_le_exnt~posn;;letset_uint32_be_exnt~posn=check_valid_uint32~loc:"Bigstring.set_uint32_be_exn"n;letn=ifnot_on_32bit&&n>=1lsl31thenn-(1lsl32)elseninset_int32_be_exnt~posn;;letget_uint32_let~pos=uint32_of_int32_t(get_int32_t_let~pos)letget_uint32_bet~pos=uint32_of_int32_t(get_int32_t_bet~pos)moduleInt_repr=structmoduleF=structtypet=t_frozenletget_uint8tpos=get_uint8t~posletset_uint8tposx=Array1.settpos(Char.unsafe_of_intx)letget_uint16_netpos=get_16tposletset_uint16_netposx=set_16_trunctposxletget_int32_netpos=get_32tposletset_int32_netposx=set_32tposxletget_int64_netpos=get_64tposletset_int64_netposx=set_64tposxmoduleLocal=structletget_int64_netpos=check_args~loc:"get_64"~pos~len:8t;unsafe_get_64tpos;;endendincludeInt_repr.Make_get(F)includeInt_repr.Make_set(F)moduleUnsafe=structmoduleF=structtypet=t_frozenletget_uint8tpos=unsafe_get_uint8t~posletset_uint8tposx=unsafe_set_uint8t~posxletget_uint16_netpos=unsafe_get_16tposletset_uint16_netposx=unsafe_set_16tposxletget_int32_netpos=unsafe_get_32tposletset_int32_netposx=unsafe_set_32tposxletget_int64_netpos=unsafe_get_64tposletset_int64_netposx=unsafe_set_64tposxmoduleLocal=structletget_int64_netpos=unsafe_get_64tposendendincludeInt_repr.Make_get(F)includeInt_repr.Make_set(F)endendmodulePrivate=structletsign_extend_16=sign_extend_16end