123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125open!Importletstage=Staged.stagemoduleT=structtypet=bytes[@@deriving_inlinesexp]lett_of_sexp:Ppx_sexp_conv_lib.Sexp.t->t=bytes_of_sexpletsexp_of_t:t->Ppx_sexp_conv_lib.Sexp.t=sexp_of_bytes[@@@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;tletof_char_listl=lett=create(List.lengthl)inList.iteril~f:(funic->settic);tletto_listt=letreclooptiacc=ifInt_replace_polymorphic_compare.(<)i0thenaccelseloopt(i-1)(unsafe_getti::acc)inloopt(lengtht-1)[]lettr~target~replacements=fori=0tolengths-1doifChar.equal(unsafe_getsi)targetthenunsafe_setsireplacementdonelettr_multi~target~replacement=ifInt_replace_polymorphic_compare.(=)(String.lengthtarget)0thenstageignoreelseifInt_replace_polymorphic_compare.(=)(String.lengthreplacement)0theninvalid_arg"tr_multi: replacement is the empty string"elsematchBytes_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~maxletclampt~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=let(pos,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