123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537(*---------------------------------------------------------------------------
Copyright (c) 2014 The fmt programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
fmt v0.8.6
---------------------------------------------------------------------------*)(* Errors *)leterr_str_formatter="Format.str_formatter can't be set."letstdout=Format.std_formatterletstderr=Format.err_formatter(* Formatting *)letpf=Format.fprintfletpr=Format.printfletepr=Format.eprintfletstrf=Format.asprintfletkpf=Format.kfprintfletkstrfffmt=letbuf=Buffer.create64inletffmt=Format.pp_print_flushfmt();lets=Buffer.contentsbufinBuffer.resetbuf;fsinFormat.kfprintff(Format.formatter_of_bufferbuf)fmt(* Standard output formatting *)(* Exception formatting *)letinvalid_arg'=invalid_argletfailwithfmt=kstrffailwithfmtletinvalid_argfmt=kstrfinvalid_argfmt(* Formatters *)type'at=Format.formatter->'a->unitletnopfmtppf=()letcut=Format.pp_print_cutletsp=Format.pp_print_spaceletcommappf()=pfppf",@ "letconstpp_vvppf()=pfppf"%a"pp_vvletunitfmtppf()=pfppffmtletfmtfmtppf=pfppffmtletalwaysfmtppfv=pfppffmt(* Base type formatters *)letbool=Format.pp_print_boolletint=Format.pp_print_intletnativeintppfv=pfppf"%nd"vletint32ppfv=pfppf"%ld"vletint64ppfv=pfppf"%Ld"vletuintppfv=pfppf"%u"vletuint32ppfv=pfppf"%lu"vletuint64ppfv=pfppf"%Lu"vletunativeintppfv=pfppf"%nu"vletchar=Format.pp_print_charletstring=Format.pp_print_stringletbufferppfb=stringppf(Buffer.contentsb)letexnppfe=stringppf(Printexc.to_stringe)letexn_backtraceppf(e,bt)=letpp_backtrace_strppfs=letstop=String.lengths-1(* there's a newline at the end *)inletrecloopleftright=ifright=stopthenstringppf(String.subsleft(right-left))elseifs.[right]<>'\n'thenloopleft(right+1)elsebeginstringppf(String.subsleft(right-left));cutppf();loop(right+1)(right+1)endinifs=""then(stringppf"No backtrace available.")elseloop00inpfppf"@[<v>Exception: %a@,%a@]"exnepp_backtrace_str(Printexc.raw_backtrace_to_stringbt)(* Floats *)letfloatppfv=pfppf"%g"vletroundx=floor(x+.0.5)letround_dfracdx=ifx-.(roundx)=0.thenxelse(* x is an integer. *)letm=10.**(float_of_intd)in(* m moves 10^-d to 1. *)(floor((x*.m)+.0.5))/.mletround_dsigdx=ifx=0.then0.elseletm=10.**(floor(log10(abs_floatx)))in(* to normalize x. *)(round_dfracd(x/.m))*.mletfloat_dfracdppff=pfppf"%g"(round_dfracdf)letfloat_dsigdppff=pfppf"%g"(round_dsigdf)(* Polymorphic type formatters *)letpair?sep:(pp_sep=cut)pp_fstpp_sndppf(fst,snd)=pp_fstppffst;pp_sepppf();pp_sndppfsndletoption?none:(pp_none=nop)pp_vppf=function|None->pp_noneppf()|Somev->pp_vppfvletresult~ok~errorppf=function|Okv->okppfv|Errore->errorppfeletiter?sep:(pp_sep=cut)iterpp_eltppfv=letis_first=reftrueinletpp_eltv=if!is_firstthen(is_first:=false)elsepp_sepppf();pp_eltppfviniterpp_eltvletiter_bindings?sep:(pp_sep=cut)iterpp_bindingppfv=letis_first=reftrueinletpp_bindingkv=if!is_firstthen(is_first:=false)elsepp_sepppf();pp_bindingppf(k,v)initerpp_bindingvletlist?seppp_elt=iter?sepList.iterpp_eltletarray?seppp_elt=iter?sepArray.iterpp_eltletseq?seppp_elt=iter?sepSeq.iterpp_eltlethashtbl?seppp_binding=iter_bindings?sepHashtbl.iterpp_bindingletqueue?seppp_elt=iterQueue.iterpp_eltletstack?seppp_elt=iterStack.iterpp_eltletusingfppppfv=ppppf(fv)moduleDump=structletsignalppfs=matchswith|swhens=Sys.sigabrt->stringppf"SIGABRT"|swhens=Sys.sigalrm->stringppf"SIGALRM"|swhens=Sys.sigfpe->stringppf"SIGFPE"|swhens=Sys.sighup->stringppf"SIGHUP"|swhens=Sys.sigill->stringppf"SIGILL"|swhens=Sys.sigint->stringppf"SIGINT"|swhens=Sys.sigkill->stringppf"SIGKILL"|swhens=Sys.sigpipe->stringppf"SIGPIPE"|swhens=Sys.sigquit->stringppf"SIGQUIT"|swhens=Sys.sigsegv->stringppf"SIGSEGV"|swhens=Sys.sigterm->stringppf"SIGTERM"|swhens=Sys.sigusr1->stringppf"SIGUSR1"|swhens=Sys.sigusr2->stringppf"SIGUSR2"|swhens=Sys.sigchld->stringppf"SIGCHLD"|swhens=Sys.sigcont->stringppf"SIGCONT"|swhens=Sys.sigstop->stringppf"SIGSTOP"|swhens=Sys.sigtstp->stringppf"SIGTSTP"|swhens=Sys.sigttin->stringppf"SIGTTIN"|swhens=Sys.sigttou->stringppf"SIGTTOU"|swhens=Sys.sigvtalrm->stringppf"SIGVTALRM"|swhens=Sys.sigprof->stringppf"SIGPROF"|swhens=Sys.sigbus->stringppf"SIGBUS"|swhens=Sys.sigpoll->stringppf"SIGPOLL"|swhens=Sys.sigsys->stringppf"SIGSYS"|swhens=Sys.sigtrap->stringppf"SIGTRAP"|swhens=Sys.sigurg->stringppf"SIGURG"|swhens=Sys.sigxcpu->stringppf"SIGXCPU"|swhens=Sys.sigxfsz->stringppf"SIGXFSZ"|unknown->pfppf"SIG(%d)"unknownletucharppfu=pfppf"U+%04X"(Uchar.to_intu)letpairpp_fstpp_sndppf(fst,snd)=pfppf"@[<1>(@[%a@],@ @[%a@])@]"pp_fstfstpp_sndsndletoptionpp_vppf=function|None->pfppf"None"|Somev->pfppf"@[<2>Some@ @[%a@]@]"pp_vvletresult~ok~errorppf=function|Okv->pfppf"@[<2>Ok@ @[%a@]@]"okv|Errore->pfppf"@[<2>Error@ @[%a@]@]"erroreletlistpp_eltppfvs=letrecloop=function|[]->()|v::vs->ifvs=[]then(pfppf"@[%a@]"pp_eltv)else(pfppf"@[%a@];@ "pp_eltv;loopvs)inpfppf"@[<1>[";loopvs;pfppf"]@]"letarraypp_eltppfa=pfppf"@[<2>[|";fori=0toArray.lengtha-1doifi=0thenpfppf"@[%a@]"pp_elta.(i)elsepfppf";@ @[%a@]"pp_elta.(i)done;pfppf"|]@]"letseqpp_eltppfs=letrecloop=function|Seq.Nil->()|Seq.Cons(v,vs)->matchvs()with|Seq.Nil->pfppf"@[%a@]"pp_eltv|Seq.Cons_asnext->pfppf"@[%a@];@ "pp_eltv;loopnextinpfppf"@[<1>[";loop(s());pfppf"]@]"letiteriterpp_namepp_eltppfv=letis_first=reftrueinletpp_eltv=if!is_firstthen(is_first:=false)elsepfppf"@ ";pfppf"@[%a@]"pp_eltvinpfppf"@[<1>(%a@ "pp_namev;iterpp_eltv;pfppf")@]"letiter_bindingsiterpp_namepp_kpp_vppfbs=letis_first=reftrueinletpp_bindingkv=if!is_firstthen()elsepfppf"@ ";pfppf"@[<1>(@[%a@],@ @[%a@])@]"pp_kkpp_vvinpfppf"@[<1>(%a@ "pp_namebs;iterpp_bindingbs;pfppf")@]"lethashtblpp_kpp_v=iter_bindingsHashtbl.iter(always"hashtbl")pp_kpp_vletstackpp_elt=iterStack.iter(always"stack")pp_eltletqueuepp_elt=iterQueue.iter(always"queue")pp_eltend(* Boxes *)letbox?(indent=0)ppppf=Format.pp_open_hovboxppfindent;pfppf"%a@]"pplethboxppppf=Format.pp_open_hboxppf();pfppf"%a@]"ppletvbox?(indent=0)ppppf=Format.pp_open_vboxppfindent;pfppf"%a@]"pplethvbox?(indent=0)ppppf=Format.pp_open_hvboxppfindent;pfppf"%a@]"pp(* Brackets *)letparenspp_vppfv=pfppf"@[<1>(%a)@]"pp_vvletbracketspp_vppfv=pfppf"@[<1>[%a]@]"pp_vvletbracespp_vppfv=pfppf"@[<1>{%a}@]"pp_vvletquote?(mark="\"")pp_vppfv=pfppf"@[<1>@<1>%s%a@<1>%s@]"markpp_vvmark(* Text and lines *)letis_nlc=c='\n'letis_nl_or_spc=is_nlc||c=' 'letis_white=function' '|'\t'..'\r'->true|_->falseletnot_whitec=not(is_whitec)letnot_white_or_nlc=is_nlc||not_whitecletrecstop_atsat~start~maxs=ifstart>maxthenstartelseifsats.[start]thenstartelsestop_atsat~start:(start+1)~maxsletsubsstartstop~max=ifstart=stopthen""elseifstart=0&&stop>maxthenselseString.subsstart(stop-start)letwordsppfs=letmax=String.lengths-1inletrecloopstarts=matchstop_atis_white~start~maxswith|stopwhenstop>max->Format.pp_print_stringppf(subsstartstop~max)|stop->Format.pp_print_stringppf(subsstartstop~max);matchstop_atnot_white~start:stop~maxswith|stopwhenstop>max->()|stop->Format.pp_print_spaceppf();loopstopsinletstart=stop_atnot_white~start:0~maxsinifstart>maxthen()elseloopstartsletparagraphsppfs=letmax=String.lengths-1inletrecloopstarts=matchstop_atis_white~start~maxswith|stopwhenstop>max->Format.pp_print_stringppf(subsstartstop~max)|stop->Format.pp_print_stringppf(subsstartstop~max);matchstop_atnot_white_or_nl~start:stop~maxswith|stopwhenstop>max->()|stop->ifs.[stop]<>'\n'then(Format.pp_print_spaceppf();loopstops)elsematchstop_atnot_white_or_nl~start:(stop+1)~maxswith|stopwhenstop>max->()|stop->ifs.[stop]<>'\n'then(Format.pp_print_spaceppf();loopstops)elsematchstop_atnot_white~start:(stop+1)~maxswith|stopwhenstop>max->()|stop->Format.pp_force_newlineppf();Format.pp_force_newlineppf();loopstopsinletstart=stop_atnot_white~start:0~maxsinifstart>maxthen()elseloopstartslettextppfs=letmax=String.lengths-1inletrecloopstarts=matchstop_atis_nl_or_sp~start~maxswith|stopwhenstop>max->Format.pp_print_stringppf(subsstartstop~max)|stop->Format.pp_print_stringppf(subsstartstop~max);beginmatchs.[stop]with|' '->Format.pp_print_spaceppf()|'\n'->Format.pp_force_newlineppf()|_->assertfalseend;loop(stop+1)sinloop0sletlinesppfs=letmax=String.lengths-1inletrecloopstarts=matchstop_atis_nl~start~maxswith|stopwhenstop>max->Format.pp_print_stringppf(subsstartstop~max)|stop->Format.pp_print_stringppf(subsstartstop~max);Format.pp_force_newlineppf();loop(stop+1)sinloop0slettext_locppf((l0,c0),(l1,c1))=if(l0:int)==(l1:int)&&(c0:int)==(c1:int)thenpfppf"%d.%d"l0c0elsepfppf"%d.%d-%d.%d"l0c0l1c1(* Appending *)letappendpp_v0pp_v1ppf(v0,v1)=pp_v0ppfv0;pp_v1ppfv1letprefixpp_ppp_vppfv=pp_pppf();pp_vppfvletsuffixpp_spp_vppfv=pp_vppfv;pp_sppf()(* Byte sizes *)let_pp_byte_sizekippfs=letpp_frac=float_dfrac1inletdiv_round_upmn=(m+n-1)/ninletfloat=float_of_intinifs<kthenpfppf"%dB"selseletm=k*kinifs<mthenbeginletkstr=ifi=""then"k"(* SI *)else"K"(* IEC *)inletsk=s/kinifsk<10thenpfppf"%a%s%sB"pp_frac(floats/.floatk)kstrielsepfppf"%d%s%sB"(div_round_upsk)kstriendelseletg=k*minifs<gthenbeginletsm=s/minifsm<10thenpfppf"%aM%sB"pp_frac(floats/.floatm)ielsepfppf"%dM%sB"(div_round_upsm)iendelselett=k*ginifs<tthenbeginletsg=s/ginifsg<10thenpfppf"%aG%sB"pp_frac(floats/.floatg)ielsepfppf"%dG%sB"(div_round_upsg)iendelseletp=k*tinifs<pthenbeginletst=s/tinifst<10thenpfppf"%aT%sB"pp_frac(floats/.floatt)ielsepfppf"%dT%sB"(div_round_upst)iendelsebeginletsp=s/pinifsp<10thenpfppf"%aP%sB"pp_frac(floats/.floatp)ielsepfppf"%dP%sB"(div_round_upsp)iendletbyte_sizeppfs=_pp_byte_size1000""ppfsletbi_byte_sizeppfs=_pp_byte_size1024"i"ppfs(* Conditional UTF-8 and styled formatting.
This is very ugly, formally what we would like is to be able to
store arbitrary typed metadata in formatters for clients to consult
(tried to provide an API for doing that but dismissed it for
uglyness and lack of an efficient implementation). In the following
we are using the tags functions (but not the tags mechanism itself)
as a way to store two metadata keys, one for formatter UTF-8
awareness and the other for the formatter style renderer. *)letutf_8_tag="fmt.utf8"letutf_8_of_raw=function|"\x00"->false|"\x01"->true|_->trueletutf_8_to_raw=function|false->"\x00"|true->"\x01"typestyle_renderer=[`Ansi_tty|`None]letstyle_renderer_tag="fmt.style_renderer"letstyle_renderer_of_raw=function|"\x00"->`None|"\x01"->`Ansi_tty|_->`Noneletstyle_renderer_to_raw=function|`None->"\x00"|`Ansi_tty->"\x01"letmeta_storeppf=Format.pp_get_formatter_tag_functionsppf()letset_meta_storeppfstore=Format.pp_set_formatter_tag_functionsppfstoreletmeta_rawstoretag=store.Format.mark_open_tagtagletset_metappfstore~utf_8~style_renderer=letmeta=function|"fmt.utf8"->utf_8|"fmt.style_renderer"->style_renderer|_->"Fmt: do not use the tags mechanism, it is a broken idea"inletstore={storewithFormat.mark_open_tag=meta}inset_meta_storeppfstoreletutf_8ppf=utf_8_of_raw(meta_raw(meta_storeppf)utf_8_tag)letset_utf_8ppfutf_8=ifppf==Format.str_formattertheninvalid_arg'err_str_formatterelseletstore=meta_storeppfinletstyle_renderer=meta_rawstorestyle_renderer_taginletutf_8=utf_8_to_rawutf_8inset_metappfstore~utf_8~style_rendererletstyle_rendererppf=style_renderer_of_raw(meta_raw(meta_storeppf)style_renderer_tag)letset_style_rendererppfrenderer=ifppf==Format.str_formattertheninvalid_arg'err_str_formatterelseletstore=meta_storeppfinletutf_8=meta_rawstoreutf_8_taginletstyle_renderer=style_renderer_to_rawrendererinset_metappfstore~utf_8~style_rendererletwith_buffer?likebuf=letppf=Format.formatter_of_bufferbufinmatchlikewith|None->ppf|Somelike->set_meta_storeppf(meta_storelike);ppfletstrf_likeppffmt=letbuf=Buffer.create64inletbppf=with_buffer~like:ppfbufinletflushppf=Format.pp_print_flushppf();lets=Buffer.contentsbufinBuffer.resetbuf;sinFormat.kfprintfflushbppffmt(* Conditional UTF-8 formatting *)letif_utf_8pp_upp=funppfv->(ifutf_8ppfthenpp_uelsepp)ppfv(* Styled formatting *)typestyle=[`Bold|`Underline|`Black|`Red|`Green|`Yellow|`Blue|`Magenta|`Cyan|`White|`None]letansi_style_code=function|`Bold->"\027[01m"|`Underline->"\027[04m"|`Black->"\027[30m"|`Red->"\027[31m"|`Green->"\027[32m"|`Yellow->"\027[33m"|`Blue->"\027[34m"|`Magenta->"\027[35m"|`Cyan->"\027[36m"|`White->"\027[37m"|`None->"\027[m"letansi_style_reset="\027[m"letstyledstylepp_vppf=matchstyle_rendererppfwith|`None->pp_vppf|`Ansi_tty->letresetppf=pfppf"@<0>%s"ansi_style_resetinkpfresetppf"@<0>%s%a"(ansi_style_codestyle)pp_vletstyled_unitstylefmt=styledstyle(unitfmt)(* Converting with string converters. *)letof_to_stringfppfv=stringppf(fv)letto_to_stringpp_vv=strf"%a"pp_vv(*---------------------------------------------------------------------------
Copyright (c) 2014 The fmt programmers
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)