123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566(**************************************************************************)(* ocgtk - OCaml bindings for GTK4 *)(* *)(* This program is free software; you can redistribute it *)(* and/or modify it under the terms of the GNU Library General *)(* Public License version 2, as published by the *)(* Free Software Foundation with the exception described in file *)(* COPYING which comes with the library. *)(* *)(* Based on lablgtk3 (https://github.com/garrigue/lablgtk) *)(* *)(**************************************************************************)(* Unsigned 64-bit integer backed by OCaml int64.
* Values > Int64.max_int are stored as negative int64 bit patterns.
* All arithmetic is modular (wrapping), matching C uint64_t semantics. *)typet=int64letzero=0Lletone=1Lletmax_int=-1L(* 0xFFFFFFFFFFFFFFFF *)letof_int=Int64.of_intletto_int=Int64.to_intletadd=Int64.addletsub=Int64.subletmul=Int64.mulletequal(a:t)(b:t)=a=b(* Unsigned comparison: shift both values into the signed domain by adding
* Int64.min_int (i.e. XOR the sign bit), then compare as signed int64.
* This maps [0, 2^64) bijectively to [-2^63, 2^63) preserving unsigned order. *)letcompare(a:t)(b:t)=Int64.compare(Int64.addaInt64.min_int)(Int64.addbInt64.min_int)letto_stringx=Printf.sprintf"%Lu"xletdigit_of_charbasec=letd=ifc>='0'&&c<='9'thenChar.codec-Char.code'0'elseifc>='a'&&c<='f'thenChar.codec-Char.code'a'+10elseifc>='A'&&c<='F'thenChar.codec-Char.code'A'+10else-1inifd>=0&&d<basethendelse-1letof_strings=letn=String.lengthsinifn=0theninvalid_arg"UInt64.of_string";letstart,base=ifn>=2&&s.[0]='0'thenmatchs.[1]with|'x'|'X'->(2,16)|'o'|'O'->(2,8)|'b'|'B'->(2,2)|_->(0,10)else(0,10)inletbase64=Int64.of_intbaseinletresult=ref0Linfori=startton-1doletd=digit_of_charbases.[i]inifd<0theninvalid_arg("UInt64.of_string: "^s);result:=Int64.add(Int64.mul!resultbase64)(Int64.of_intd)done;!result