123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154open!ImportmoduleArray=Array0letstage=Staged.stagemoduleT=structtypet=bytes[@@deriving_inlinesexp]lett_of_sexp=(bytes_of_sexp:Ppx_sexp_conv_lib.Sexp.t->t)letsexp_of_t=(sexp_of_bytes:t->Ppx_sexp_conv_lib.Sexp.t)[@@@end]includeBytes0letmodule_name="Base.Bytes"letppfmtt=Caml.Format.fprintffmt"%S"(to_stringt)endincludeTmoduleTo_bytes=Blit.Make(structincludeTletcreate~len=createlenend)includeTo_bytesincludeComparator.Make(T)includeComparable.Validate(T)includePretty_printer.Register_pp(T)(* Open replace_polymorphic_compare after including functor instantiations so they do not
shadow its definitions. This is here so that efficient versions of the comparison
functions are available within this module. *)open!Bytes_replace_polymorphic_comparemoduleTo_string=Blit.Make_to_string(T)(To_bytes)moduleFrom_string=Blit.Make_distinct(structtypet=stringletlength=String.lengthend)(structtypenonrect=tletcreate~len=createlenletlength=lengthletunsafe_blit=unsafe_blit_stringend)letinitn~f=ifInt_replace_polymorphic_compare.(<)n0thenPrintf.invalid_argf"Bytes.init %d"n();lett=createninfori=0ton-1dounsafe_setti(fi)done;t;;letof_char_listl=lett=create(List.lengthl)inList.iteril~f:(funic->settic);t;;letto_listt=letreclooptiacc=ifInt_replace_polymorphic_compare.(<)i0thenaccelseloopt(i-1)(unsafe_getti::acc)inloopt(lengtht-1)[];;letto_arrayt=Array.init(lengtht)~f:(funi->unsafe_getti)letmapt~f=mapt~fletmapit~f=mapit~fletfold=letrecloopt~f~len~posacc=ifInt_replace_polymorphic_compare.equalposlenthenaccelseloopt~f~len~pos:(pos+1)(facc(unsafe_gettpos))infunt~init~f->loopt~f~len:(lengtht)~pos:0init;;letfoldi=letrecloopt~f~len~posacc=ifInt_replace_polymorphic_compare.equalposlenthenaccelseloopt~f~len~pos:(pos+1)(fposacc(unsafe_gettpos))infunt~init~f->loopt~f~len:(lengtht)~pos:0init;;lettr~target~replacements=fori=0tolengths-1doifChar.equal(unsafe_getsi)targetthenunsafe_setsireplacementdone;;lettr_multi~target~replacement=ifInt_replace_polymorphic_compare.(=)(String.lengthtarget)0thenstageignoreelseifInt_replace_polymorphic_compare.(=)(String.lengthreplacement)0theninvalid_arg"tr_multi: replacement is the empty string"else(matchBytes_tr.tr_create_map~target~replacementwith|None->stageignore|Sometr_map->stage(funs->fori=0tolengths-1dounsafe_setsi(String.unsafe_gettr_map(Char.to_int(unsafe_getsi)))done));;letbetweent~low~high=low<=t&&t<=highletclamp_uncheckedt~min~max=ift<minthenminelseift<=maxthentelsemaxletclamp_exnt~min~max=assert(min<=max);clamp_uncheckedt~min~max;;letclampt~min~max=ifmin>maxthenOr_error.error_s(Sexp.message"clamp requires [min <= max]"["min",T.sexp_of_tmin;"max",T.sexp_of_tmax])elseOk(clamp_uncheckedt~min~max);;letcontains?pos?lentchar=letpos,len=Ordered_collection_common.get_pos_len_exn()?pos?len~total_length:(lengtht)inletlast=pos+leninletrecloopi=Int_replace_polymorphic_compare.(<)ilast&&(Char.equal(getti)char||loop(i+1))inlooppos;;(* Include type-specific [Replace_polymorphic_compare] at the end, after
including functor application that could shadow its definitions. This is
here so that efficient versions of the comparison functions are exported by
this module. *)includeBytes_replace_polymorphic_compare