Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file qty_repr.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)moduletypeQTY=sigvalid:stringvalname:stringendmoduletypeS=sigtypeqtytypeerror+=|Addition_overflowofqty*qty(* `Temporary *)|Subtraction_underflowofqty*qty(* `Temporary *)|Multiplication_overflowofqty*int64(* `Temporary *)|Negative_multiplicatorofqty*int64(* `Temporary *)|Invalid_divisorofqty*int64(* `Temporary *)valid:stringvalzero:qtyvalone_mutez:qtyvalone_cent:qtyvalfifty_cents:qtyvalone:qtyval(-?):qty->qty->qtytzresultval(+?):qty->qty->qtytzresultval(*?):qty->int64->qtytzresultval(/?):qty->int64->qtytzresultvalto_mutez:qty->int64(** [of_mutez n] (micro tez) is None if n is negative *)valof_mutez:int64->qtyoption(** [of_mutez_exn n] fails if n is negative.
It should only be used at toplevel for constants. *)valof_mutez_exn:int64->qty(** It should only be used at toplevel for constants. *)valadd_exn:qty->qty->qty(** It should only be used at toplevel for constants. *)valmul_exn:qty->int->qtyvalencoding:qtyData_encoding.tvalto_int64:qty->int64includeCompare.Swithtypet:=qtyvalpp:Format.formatter->qty->unitvalof_string:string->qtyoptionvalto_string:qty->stringendmoduleMake(T:QTY):S=structtypeqty=int64(* invariant: positive *)typeerror+=|Addition_overflowofqty*qty(* `Temporary *)|Subtraction_underflowofqty*qty(* `Temporary *)|Multiplication_overflowofqty*int64(* `Temporary *)|Negative_multiplicatorofqty*int64(* `Temporary *)|Invalid_divisorofqty*int64(* `Temporary *)includeCompare.Int64letzero=0L(* all other constant are defined from the value of one micro tez *)letone_mutez=1Lletone_cent=Int64.mulone_mutez10_000Lletfifty_cents=Int64.mulone_cent50L(* 1 tez = 100 cents = 1_000_000 mutez *)letone=Int64.mulone_cent100Lletid=T.idletof_strings=lettriplets=function|hd::tl->letlen=String.lengthhdinCompare.Int.(len<=3&&len>0&&List.for_all(funs->String.lengths=3)tl)|[]->falseinletintegerss=triplets(String.split_on_char','s)inletdecimalss=letl=String.split_on_char','sinifCompare.Int.(List.lengthl>2)thenfalseelsetriplets(List.revl)inletparseleftright=letremove_commass=String.concat""(String.split_on_char','s)inletpad_to_sixs=letlen=String.lengthsinString.init6(funi->ifCompare.Int.(i<len)thens.[i]else'0')intrySome(Int64.of_string(remove_commasleft^pad_to_six(remove_commasright)))with_->NoneinmatchString.split_on_char'.'swith|[left;right]->ifString.containss','thenifintegersleft&&decimalsrightthenparseleftrightelseNoneelseifCompare.Int.(String.lengthright>0)&&Compare.Int.(String.lengthright<=6)thenparseleftrightelseNone|[left]->if(not(String.containss','))||integersleftthenparseleft""elseNone|_->Noneletppppfamount=letmult_int=1_000_000Linletrecleftppfamount=let(d,r)=(Int64.(divamount1000L),Int64.(remamount1000L))inifd>0LthenFormat.fprintfppf"%a%03Ld"leftdrelseFormat.fprintfppf"%Ld"rinletrightppfamount=lettripletppfv=ifCompare.Int.(vmod10>0)thenFormat.fprintfppf"%03d"velseifCompare.Int.(vmod100>0)thenFormat.fprintfppf"%02d"(v/10)elseFormat.fprintfppf"%d"(v/100)inlet(hi,lo)=(amount/1000,amountmod1000)inifCompare.Int.(lo=0)thenFormat.fprintfppf"%a"triplethielseFormat.fprintfppf"%03d%a"hitripletloinlet(ints,decs)=(Int64.(divamountmult_int),Int64.(to_int(remamountmult_int)))inFormat.fprintfppf"%a"leftints;ifCompare.Int.(decs>0)thenFormat.fprintfppf".%a"rightdecsletto_stringt=Format.asprintf"%a"pptlet(-)t1t2=ift2<=t1thenSome(Int64.subt1t2)elseNonelet(-?)t1t2=matcht1-t2with|None->error(Subtraction_underflow(t1,t2))|Somev->okvlet(+?)t1t2=lett=Int64.addt1t2inift<t1thenerror(Addition_overflow(t1,t2))elseoktlet(*?)tm=letopenCompare.Int64inletopenInt64inletrecstepcurpowacc=ifcur=0Lthenokaccelsepow+?pow>>?funnpow->iflogandcur1L=1Lthenacc+?pow>>?funnacc->step(shift_right_logicalcur1)npownaccelsestep(shift_right_logicalcur1)npowaccinifm<0Lthenerror(Negative_multiplicator(t,m))elsematchstepmt0Lwith|Okres->Okres|Error([Addition_overflow_]aserrs)->Error(Multiplication_overflow(t,m)::errs)|Errorerrs->Errorerrslet(/?)td=ifd<=0Lthenerror(Invalid_divisor(t,d))elseok(Int64.divtd)letadd_exnt1t2=lett=Int64.addt1t2inift<=0Ltheninvalid_arg"add_exn"elsetletmul_exntm=matcht*?Int64.(of_intm)with|Okv->v|Error_->invalid_arg"mul_exn"letof_mutezt=ift<0LthenNoneelseSometletof_mutez_exnx=matchof_mutezxwithNone->invalid_arg"Qty.of_mutez"|Somev->vletto_int64t=tletto_mutezt=tletencoding=letopenData_encodinginData_encoding.defT.name(check_size10(convZ.of_int64(Json.wrap_errorZ.to_int64)n))let()=letopenData_encodinginregister_error_kind`Temporary~id:(T.id^".addition_overflow")~title:("Overflowing "^T.id^" addition")~pp:(funppf(opa,opb)->Format.fprintfppf"Overflowing addition of %a %s and %a %s"ppopaT.idppopbT.id)~description:("An addition of two "^T.id^" amounts overflowed")(obj1(req"amounts"(tup2encodingencoding)))(functionAddition_overflow(a,b)->Some(a,b)|_->None)(fun(a,b)->Addition_overflow(a,b));register_error_kind`Temporary~id:(T.id^".subtraction_underflow")~title:("Underflowing "^T.id^" subtraction")~pp:(funppf(opa,opb)->Format.fprintfppf"Underflowing subtraction of %a %s and %a %s"ppopaT.idppopbT.id)~description:("An subtraction of two "^T.id^" amounts underflowed")(obj1(req"amounts"(tup2encodingencoding)))(functionSubtraction_underflow(a,b)->Some(a,b)|_->None)(fun(a,b)->Subtraction_underflow(a,b));register_error_kind`Temporary~id:(T.id^".multiplication_overflow")~title:("Overflowing "^T.id^" multiplication")~pp:(funppf(opa,opb)->Format.fprintfppf"Overflowing multiplication of %a %s and %Ld"ppopaT.idopb)~description:("A multiplication of a "^T.id^" amount by an integer overflowed")(obj2(req"amount"encoding)(req"multiplicator"int64))(functionMultiplication_overflow(a,b)->Some(a,b)|_->None)(fun(a,b)->Multiplication_overflow(a,b));register_error_kind`Temporary~id:(T.id^".negative_multiplicator")~title:("Negative "^T.id^" multiplicator")~pp:(funppf(opa,opb)->Format.fprintfppf"Multiplication of %a %s by negative integer %Ld"ppopaT.idopb)~description:("Multiplication of a "^T.id^" amount by a negative integer")(obj2(req"amount"encoding)(req"multiplicator"int64))(functionNegative_multiplicator(a,b)->Some(a,b)|_->None)(fun(a,b)->Negative_multiplicator(a,b));register_error_kind`Temporary~id:(T.id^".invalid_divisor")~title:("Invalid "^T.id^" divisor")~pp:(funppf(opa,opb)->Format.fprintfppf"Division of %a %s by non positive integer %Ld"ppopaT.idopb)~description:("Multiplication of a "^T.id^" amount by a non positive integer")(obj2(req"amount"encoding)(req"divisor"int64))(functionInvalid_divisor(a,b)->Some(a,b)|_->None)(fun(a,b)->Invalid_divisor(a,b))end