提交 80cd1a15 编写于 作者: S Steffen Forkmann 提交者: Kevin Ransom (msft)

Do not use new empty string just to throw it away immediately (#5638)

* Do not use new empty string just to throw it away immediately

* Do not use new empty string just to throw it away immediately

* No need to optimize closures for empty strings

* remove emptyIfNull
上级 03b5e27f
......@@ -12,57 +12,59 @@ namespace Microsoft.FSharp.Core
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<RequireQualifiedAccess>]
module String =
let inline emptyIfNull str =
match str with
| null -> String.Empty
| _ -> str
[<CompiledName("Concat")>]
let concat sep (strings : seq<string>) =
String.Join(sep, strings)
[<CompiledName("Iterate")>]
let iter (action : (char -> unit)) (str:string) =
let str = emptyIfNull str
for i = 0 to str.Length - 1 do
action str.[i]
if not (String.IsNullOrEmpty str) then
for i = 0 to str.Length - 1 do
action str.[i]
[<CompiledName("IterateIndexed")>]
let iteri action (str:string) =
let str = emptyIfNull str
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(action)
for i = 0 to str.Length - 1 do
f.Invoke(i, str.[i])
if not (String.IsNullOrEmpty str) then
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(action)
for i = 0 to str.Length - 1 do
f.Invoke(i, str.[i])
[<CompiledName("Map")>]
let map (mapping: char -> char) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
str |> iter (fun c -> res.Append(mapping c) |> ignore)
res.ToString()
if String.IsNullOrEmpty str then
String.Empty
else
let res = StringBuilder str.Length
str |> iter (fun c -> res.Append(mapping c) |> ignore)
res.ToString()
[<CompiledName("MapIndexed")>]
let mapi (mapping: int -> char -> char) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(mapping)
str |> iteri (fun i c -> res.Append(f.Invoke(i, c)) |> ignore)
res.ToString()
if String.IsNullOrEmpty str then
String.Empty
else
let res = StringBuilder str.Length
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(mapping)
str |> iteri (fun i c -> res.Append(f.Invoke(i, c)) |> ignore)
res.ToString()
[<CompiledName("Filter")>]
let filter (predicate: char -> bool) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
str |> iter (fun c -> if predicate c then res.Append c |> ignore)
res.ToString()
if String.IsNullOrEmpty str then
String.Empty
else
let res = StringBuilder str.Length
str |> iter (fun c -> if predicate c then res.Append c |> ignore)
res.ToString()
[<CompiledName("Collect")>]
let collect (mapping: char -> string) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
str |> iter (fun c -> res.Append(mapping c) |> ignore)
res.ToString()
if String.IsNullOrEmpty str then
String.Empty
else
let res = StringBuilder str.Length
str |> iter (fun c -> res.Append(mapping c) |> ignore)
res.ToString()
[<CompiledName("Initialize")>]
let init (count:int) (initializer: int-> string) =
......@@ -75,25 +77,34 @@ namespace Microsoft.FSharp.Core
[<CompiledName("Replicate")>]
let replicate (count:int) (str:string) =
if count < 0 then invalidArgInputMustBeNonNegative "count" count
let str = emptyIfNull str
let res = StringBuilder(count * str.Length)
for i = 0 to count - 1 do
res.Append str |> ignore
res.ToString()
if String.IsNullOrEmpty str then
String.Empty
else
let res = StringBuilder(count * str.Length)
for i = 0 to count - 1 do
res.Append str |> ignore
res.ToString()
[<CompiledName("ForAll")>]
let forall predicate (str:string) =
let str = emptyIfNull str
let rec check i = (i >= str.Length) || (predicate str.[i] && check (i+1))
check 0
if String.IsNullOrEmpty str then
true
else
let rec check i = (i >= str.Length) || (predicate str.[i] && check (i+1))
check 0
[<CompiledName("Exists")>]
let exists predicate (str:string) =
let str = emptyIfNull str
let rec check i = (i < str.Length) && (predicate str.[i] || check (i+1))
check 0
if String.IsNullOrEmpty str then
false
else
let rec check i = (i < str.Length) && (predicate str.[i] || check (i+1))
check 0
[<CompiledName("Length")>]
let length (str:string) =
let str = emptyIfNull str
str.Length
if String.IsNullOrEmpty str then
0
else
str.Length
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册