123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201open!ImportmoduleBytes=Bytes0moduleString=String0includeUchar_intfletfailwithf=Printf.failwithfincludeUchar0letmodule_name="Base.Uchar"lethash_fold_tstatet=Hash.fold_intstate(to_intt)lethasht=Hash.runhash_fold_tt(* Not for export. String formats exported via [Utf*] modules below. *)letto_string_internalt=Printf.sprintf"U+%04X"(to_intt)letsexp_of_tt=Sexp.Atom(to_string_internalt)lett_of_sexpsexp=matchsexpwith|Sexp.List_->of_sexp_error"Uchar.t_of_sexp: atom needed"sexp|Sexp.Atoms->(tryStdlib.Scanf.sscanfs"U+%X"(funi->Uchar0.of_inti)with|_->of_sexp_error"Uchar.t_of_sexp: atom of the form U+XXXX needed"sexp);;lett_sexp_grammar:tSexplib0.Sexp_grammar.t=Sexplib0.Sexp_grammar.coercestring_sexp_grammar;;includePretty_printer.Register(structtypenonrect=tletmodule_name=module_nameletto_string=to_string_internalend)includeComparable.Make(structtypenonrect=tletcompare=compareletsexp_of_t=sexp_of_tend)(* 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!Uchar_replace_polymorphic_compareletinvariant(_:t)=()letint_is_scalar=is_validletsucc_exnc=tryUchar0.succcwith|Invalid_argumentmsg->failwithf"Uchar.succ_exn: %s"msg();;letsuccc=trySome(Uchar0.succc)with|Invalid_argument_->None;;letpred_exnc=tryUchar0.predcwith|Invalid_argumentmsg->failwithf"Uchar.pred_exn: %s"msg();;letpredc=trySome(Uchar0.predc)with|Invalid_argument_->None;;letof_scalari=ifint_is_scalarithenSome(unsafe_of_inti)elseNoneletof_scalar_exni=ifint_is_scalarithenunsafe_of_intielsefailwithf"Uchar.of_int_exn got a invalid Unicode scalar value: %04X"i();;letto_scalart=Uchar0.to_inttletto_charc=ifis_charcthenSome(unsafe_to_charc)elseNoneletto_char_exnc=ifis_charcthenunsafe_to_charcelsefailwithf"Uchar.to_char_exn got a non latin-1 character: U+%04X"(to_intc)();;moduleDecode_result=structtypet=Uchar0.utf_decodeletcompare:t->t->int=Poly.compareletequal:t->t->bool=Poly.equallethash_fold_t:Hash.state->t->Hash.state=funstatet->hash_fold_intstate(Hashable.hasht);;lethash:t->int=Hashable.hashletis_valid=Uchar0.utf_decode_is_validletbytes_consumed=Uchar0.utf_decode_lengthletuchar_or_replacement_char=Uchar0.utf_decode_ucharletsexp_of_tt=sexp_of_t(uchar_or_replacement_chart)letuchart=matchis_validtwith|true->Some(uchar_or_replacement_chart)|false->None;;let[@zero_alloc]uchar_exnt=matchis_validtwith|true->uchar_or_replacement_chart|false->Error.raise_s(Atom"Uchar.Decode_result.uchar_exn was called on an invalid decode result");;endmoduleMake_utf(Format:sigvalcodec_name:stringvalmodule_name:stringvalbyte_length:t->intvalget_decode_result:string->byte_pos:int->Decode_result.tvalset:bytes->int->t->intend):Utf=structletcodec_name=Format.codec_nameletbyte_length=Format.byte_lengthletto_stringt=letlen=byte_lengthtinletbytes=Bytes.createleninletpos=Format.setbytes0tinassert(Int_replace_polymorphic_compare.equalposlen);Bytes.unsafe_to_string~no_mutation_while_string_reachable:bytes;;letof_string_message=Format.module_name^".of_string: expected a single Unicode character";;let[@cold]raise_of_stringstring=Error.raise_s(Sexp.messageof_string_message["string",Atomstring]);;letof_stringstring=letdecode=Format.get_decode_resultstring~byte_pos:0inletstring_len=String.lengthstringinletdecode_len=Decode_result.bytes_consumeddecodeinifInt_replace_polymorphic_compare.equalstring_lendecode_len&&Decode_result.is_validdecodethenDecode_result.uchar_or_replacement_chardecodeelseraise_of_stringstring;;endmoduleUtf8=Make_utf(structletcodec_name="UTF-8"letmodule_name="Base.Uchar.Utf8"letbyte_length=utf_8_byte_lengthletget_decode_result=String.get_utf_8_ucharletset=Bytes.set_uchar_utf_8end)moduleUtf16le=Make_utf(structletcodec_name="UTF-16LE"letmodule_name="Base.Uchar.Utf16le"letbyte_length=utf_16_byte_lengthletget_decode_result=String.get_utf_16le_ucharletset=Bytes.set_uchar_utf_16leend)moduleUtf16be=Make_utf(structletcodec_name="UTF-16BE"letmodule_name="Base.Uchar.Utf16be"letbyte_length=utf_16_byte_lengthletget_decode_result=String.get_utf_16be_ucharletset=Bytes.set_uchar_utf_16beend)moduleUtf32le=Make_utf(structletcodec_name="UTF-32LE"letmodule_name="Base.Uchar.Utf32le"letbyte_length_=4letget_decode_result=String.get_utf_32le_ucharletset=Bytes.set_uchar_utf_32leend)moduleUtf32be=Make_utf(structletcodec_name="UTF-32BE"letmodule_name="Base.Uchar.Utf32be"letbyte_length_=4letget_decode_result=String.get_utf_32be_ucharletset=Bytes.set_uchar_utf_32beend)(* 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. *)includeUchar_replace_polymorphic_compare