Browse Source

Optimized the BigInteger parser function used when PORTABLE is defined; now, it doesn't create any intermediate strings via slicing.

provider
Jack Pappas 13 years ago
parent
commit
3c639b6b7a
  1. 37
      src/FSharp/BigIntegerExtensions.fs

37
src/FSharp/BigIntegerExtensions.fs

@ -8,30 +8,29 @@ open System
[<AutoOpen>] [<AutoOpen>]
module BigIntegerExtensions = module BigIntegerExtensions =
//
let private parse str = let rec private parseImpl (str : string) len acc i =
let len = String.length str if i = len then
let rec build acc i = acc
if i = len then else
acc let d = int str.[i] - int '0'
if 0 <= d && d <= 9 then
parseImpl str len (10I * acc + (bigint d)) (i + 1)
else else
let c = str.[i] raise <| FormatException ("The value could not be parsed.")
let d = int c - int '0'
if 0 <= d && d <= 9 then
build (10I * acc + (bigint d)) (i+1)
else
raise (new FormatException("The value could not be parsed"))
build 0I 0
type BigInteger with type BigInteger with
//
static member Parse (str : string) =
let len = str.Length
if len = 0 then
raise <| FormatException ("The value could not be parsed.")
static member Parse(text: string) = if str.[0] = '-' then
let len = text.Length parseImpl str len 0I 1
if len = 0 then raise (new FormatException("The value could not be parsed")) |> bigint.Negate
if text.[0..0] = "-" then
parse text.[1..len-1] |> bigint.Negate
else else
parse text parseImpl str len 0I 0
#endif #endif
#endif #endif

Loading…
Cancel
Save