@@ -12,57 +12,59 @@ namespace Microsoft.FSharp.Core
1212[<CompilationRepresentation( CompilationRepresentationFlags.ModuleSuffix) >]
1313[<RequireQualifiedAccess>]
1414module String =
15-
16- let inline emptyIfNull str =
17- match strwith
18- | null -> String.Empty
19- | _ -> str
20-
2115[<CompiledName( " Concat" ) >]
2216let concat sep ( strings : seq < string >) =
2317 String.Join( sep, strings)
2418
2519[<CompiledName( " Iterate" ) >]
2620let iter ( action : ( char -> unit )) ( str : string ) =
27- let str = emptyIfNull str
28- for i= 0 to str.Length- 1 do
29- action str.[ i]
21+ if not ( String.IsNullOrEmpty str) then
22+ for i= 0 to str.Length- 1 do
23+ action str.[ i]
3024
3125[<CompiledName( " IterateIndexed" ) >]
3226let iteri action ( str : string ) =
33- let str = emptyIfNull str
34- let f = OptimizedClosures.FSharpFunc<_,_,_>. Adapt( action)
35- for i= 0 to str.Length- 1 do
36- f.Invoke( i, str.[ i])
27+ if not ( String.IsNullOrEmpty str) then
28+ let f = OptimizedClosures.FSharpFunc<_,_,_>. Adapt( action)
29+ for i= 0 to str.Length- 1 do
30+ f.Invoke( i, str.[ i])
3731
3832[<CompiledName( " Map" ) >]
3933let map ( mapping : char -> char ) ( str : string ) =
40- let str = emptyIfNull str
41- let res = StringBuilder str.Length
42- str|> iter( fun c -> res.Append( mapping c) |> ignore)
43- res.ToString()
34+ if String.IsNullOrEmpty strthen
35+ String.Empty
36+ else
37+ let res = StringBuilder str.Length
38+ str|> iter( fun c -> res.Append( mapping c) |> ignore)
39+ res.ToString()
4440
4541[<CompiledName( " MapIndexed" ) >]
4642let mapi ( mapping : int -> char -> char ) ( str : string ) =
47- let str = emptyIfNull str
48- let res = StringBuilder str.Length
49- let f = OptimizedClosures.FSharpFunc<_,_,_>. Adapt( mapping)
50- str|> iteri( fun i c -> res.Append( f.Invoke( i, c)) |> ignore)
51- res.ToString()
43+ if String.IsNullOrEmpty strthen
44+ String.Empty
45+ else
46+ let res = StringBuilder str.Length
47+ let f = OptimizedClosures.FSharpFunc<_,_,_>. Adapt( mapping)
48+ str|> iteri( fun i c -> res.Append( f.Invoke( i, c)) |> ignore)
49+ res.ToString()
5250
5351[<CompiledName( " Filter" ) >]
5452let filter ( predicate : char -> bool ) ( str : string ) =
55- let str = emptyIfNull str
56- let res = StringBuilder str.Length
57- str|> iter( fun c -> if predicate cthen res.Append c|> ignore)
58- res.ToString()
53+ if String.IsNullOrEmpty strthen
54+ String.Empty
55+ else
56+ let res = StringBuilder str.Length
57+ str|> iter( fun c -> if predicate cthen res.Append c|> ignore)
58+ res.ToString()
5959
6060[<CompiledName( " Collect" ) >]
6161let collect ( mapping : char -> string ) ( str : string ) =
62- let str = emptyIfNull str
63- let res = StringBuilder str.Length
64- str|> iter( fun c -> res.Append( mapping c) |> ignore)
65- res.ToString()
62+ if String.IsNullOrEmpty strthen
63+ String.Empty
64+ else
65+ let res = StringBuilder str.Length
66+ str|> iter( fun c -> res.Append( mapping c) |> ignore)
67+ res.ToString()
6668
6769[<CompiledName( " Initialize" ) >]
6870let init ( count : int ) ( initializer : int -> string ) =
@@ -75,25 +77,34 @@ namespace Microsoft.FSharp.Core
7577[<CompiledName( " Replicate" ) >]
7678let replicate ( count : int ) ( str : string ) =
7779if count< 0 then invalidArgInputMustBeNonNegative" count" count
78- let str = emptyIfNull str
79- let res = StringBuilder( count* str.Length)
80- for i= 0 to count- 1 do
81- res.Append str|> ignore
82- res.ToString()
80+
81+ if String.IsNullOrEmpty strthen
82+ String.Empty
83+ else
84+ let res = StringBuilder( count* str.Length)
85+ for i= 0 to count- 1 do
86+ res.Append str|> ignore
87+ res.ToString()
8388
8489[<CompiledName( " ForAll" ) >]
8590let forall predicate ( str : string ) =
86- let str = emptyIfNull str
87- let rec check i = ( i>= str.Length) || ( predicate str.[ i] && check( i+ 1 ))
88- check0
91+ if String.IsNullOrEmpty strthen
92+ true
93+ else
94+ let rec check i = ( i>= str.Length) || ( predicate str.[ i] && check( i+ 1 ))
95+ check0
8996
9097[<CompiledName( " Exists" ) >]
9198let exists predicate ( str : string ) =
92- let str = emptyIfNull str
93- let rec check i = ( i< str.Length) && ( predicate str.[ i] || check( i+ 1 ))
94- check0
99+ if String.IsNullOrEmpty strthen
100+ false
101+ else
102+ let rec check i = ( i< str.Length) && ( predicate str.[ i] || check( i+ 1 ))
103+ check0
95104
96105[<CompiledName( " Length" ) >]
97106let length ( str : string ) =
98- let str = emptyIfNull str
99- str.Length
107+ if String.IsNullOrEmpty strthen
108+ 0
109+ else
110+ str.Length