4 people like it.

Int32.tryParse

Converts the string representation of a number in-place to an Int32. A return value of None indicates the conversion failed.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
namespace global

type Int32 private () =
    static member tryParse(s:string,startIndex:int,length:int) =
        let inline isWhiteSpace c = System.Char.IsWhiteSpace c
        let inline isDigit c = System.Char.IsDigit c
        let mutable i = 0
        let inline at i  = s.[startIndex+i]
        while i < length && isWhiteSpace(at i) do i <- i + 1
        if i = length
        then 
            None
        else
            let c = at i
            let sign = 
                if c = '-' then 
                    i <- i + 1
                    -1
                else 
                    if c = '+' then i <- i + 1
                    1
            let mutable acc = 0
            let c = ref '_'
            while i < length && (c := at i; isDigit !c) do
                let digit = int (!c) - int '0'
                acc <- acc * 10 + digit
                i <- i + 1
            while i < length && isWhiteSpace(at i) do
                i <- i + 1
            if i = length 
            then
                Some (acc * sign)
            else
                None
    static member tryParse(s:string) =
        Int32.tryParse(s,0,s.Length)
Multiple items
type Int32 =
  private new : unit -> Int32
  static member tryParse : s:string -> int option
  static member tryParse : s:string * startIndex:int * length:int -> int option

Full name: Int32

--------------------
private new : unit -> Int32
static member Int32.tryParse : s:string * startIndex:int * length:int -> int option

Full name: Int32.tryParse
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val startIndex : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val length : int
val isWhiteSpace : (char -> bool)
val c : char
namespace System
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
System.Char.IsWhiteSpace(c: char) : bool
System.Char.IsWhiteSpace(s: string, index: int) : bool
val isDigit : (char -> bool)
System.Char.IsDigit(c: char) : bool
System.Char.IsDigit(s: string, index: int) : bool
val mutable i : int
val at : (int -> char)
val i : int
union case Option.None: Option<'T>
val sign : int
val mutable acc : int
val c : char ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val digit : int
union case Option.Some: Value: 'T -> Option<'T>
static member Int32.tryParse : s:string -> int option

Full name: Int32.tryParse
static member Int32.tryParse : s:string -> int option
static member Int32.tryParse : s:string * startIndex:int * length:int -> int option
property System.String.Length: int
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/2q
Posted:7 years ago
Author:Phillip Trelford
Tags: parsing