123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960moduleOCamlNumber=structtypefloat=Float.ttypeint=Int.tletnormalize_nanf=(* TOML spec makes no difference between positive and negative NaN,
but OCaml does, so we strip the sign from NaNs.
*)ifFloat.is_nanfthenFloat.copy_signf0.0elsef(* float_of_string correctly handles all possible TOML floats,
including underscores, leading +, the mantissa/exponent format (2021 = 2.021e3),
and special values: nan, +int, -inf
It also differentiates between +nan and -nan,
which is why the normalize function is needed.
*)letfloat_of_stringx=float_of_stringx|>normalize_nan(* string_of_float would be sufficient because its outputs
as of OCaml 4.12 are exactly what TOML uses: nan, inf, -inf
However, it's probably better to account for possible breaking changes
in the future, or for default float functions to be shadowed by
an alternative implementation, than to risk breakage.
*)letfloat_to_stringx=ifx=infinitythen"inf"elseifx=neg_infinitythen"-inf"elsePrintf.sprintf"%.2f"x|>String.lowercase_asciiletfloat_to_booleanx=not(x=0.0)letfloat_of_booleanb=ifbthen1.0else0.0letfloat_of_int=Float.of_intletint_of_float=Int.of_floatletint_of_string=int_of_stringletint_to_string=Int.to_stringletint_to_booleani=i>0letint_of_booleanb=ifbthen1else0endmoduleStringDate=structtypet=stringletlocal_time_of_strings=sletlocal_date_of_strings=sletlocal_datetime_of_strings=sletoffset_datetime_of_strings=sletlocal_time_to_strings=sletlocal_date_to_strings=sletlocal_datetime_to_strings=sletoffset_datetime_to_strings=send