fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.91k stars 298 forks source link

Support HexNumber and Any styles in BigInteger.Parse and BigInteger.TryParse #3486

Open njlr opened 1 year ago

njlr commented 1 year ago

Description

BigInteger.TryParse and BigInteger.Parse accept different strings.

Repro code

open System
open System.Numerics
open System.Globalization

let s = "1234567890123456"

let mutable n = 0I

if BigInteger.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, &n) then
  printfn $"String is a valid bigint: %A{n}"
else
  printfn $"String is not a valid bigint"

try 
  let n = BigInteger.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture)
  printfn $"String is a valid bigint: %A{n}"
with _ ->
  printfn $"String is not a valid bigint"

https://fable.io/repl/#?code=PYBwpgdgBAygngZwC5gLYChSVolqB0AcgK6pgBOAlgMYKbjTzJr4DiANsAEYCG7lALx5JKwCOnTswSKAigBeKACIAjACYAzABYArADYA7AA4AnAAZ12-UolSZqYkh5cpUaIrMBJCZQBmUACFKAHNPCBRginwAFXI4AAUecgQwAAoEABooElQuChgkOCkEfABBCDgsgGFidiRicjAw32B8MIA3JMoecJq6hrAsgDIIAEooJAALSHQoKBAqcN9oABIlAsXgqEo5HihO-gATKC4QynCALigAUlKAbwgAXxswdhTZ+cWkZag1jfOtjs3MAZHsDpRjqdguckDZ0Eg4lAPnY3ApAiEwhEoolkmlMtlSHlyAUimASuVKlA+vVGs1Wh0uj0kNSBqMPgsYT8-giAdtdvs+BCTmdLjd7k8bAB3ShTKAAfSgAFoAHzsr5c9Y8iCAuQQEFQMGCyEi2HoIA&html=DwCwLgtgNgfAUHUBTAhgE3gAm54ElgqbhgAOAtEgI4CuAlgG4C8A5AMID2AdmEj+QBUAnqSQtMAY268erXgA8wAenDQA3JJAoATgGcCTGmABm5ABwt4wFagwJgAIw5oh8REqcu311bDhA&css=Q

Expected and actual results

Expected:

String is a valid bigint: 1234567890123456
String is a valid bigint: 1234567890123456

Actual:

String is not a valid bigint
String is a valid bigint: 1234567890123456
ncave commented 1 year ago

@njlr Yes, currently NumberStyles and cultures are not supported for bigint.

A compiler warning needs to be added. I've renamed the issue to clarify it.

In the mean time, parsing without styles/cultures should work (if you need NumberStyles.Any).

The supported formats (for JS) is what the BigInt constructor supports (i.e. something similar to invariant culture).

BigInteger.TryParse(s, &n)
BigInteger.Parse(s)

That's not going to work for hex style, so I'll leave this open until it gets resolved.