Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80cd1a1

Browse files
forkiKevinRansom
authored andcommitted
Do not use new empty string just to throw it away immediately (dotnet#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
1 parent03b5e27 commit80cd1a1

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

‎src/fsharp/FSharp.Core/string.fs‎

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,59 @@ namespace Microsoft.FSharp.Core
1212
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
1313
[<RequireQualifiedAccess>]
1414
moduleString=
15-
16-
let inlineemptyIfNull str=
17-
match strwith
18-
|null-> String.Empty
19-
|_-> str
20-
2115
[<CompiledName("Concat")>]
2216
letconcat sep(strings:seq<string>)=
2317
String.Join(sep, strings)
2418

2519
[<CompiledName("Iterate")>]
2620
letiter(action:(char-> unit))(str:string)=
27-
letstr= emptyIfNullstr
28-
for i=0to str.Length-1do
29-
action str.[i]
21+
ifnot(String.IsNullOrEmptystr)then
22+
for i=0to str.Length-1do
23+
action str.[i]
3024

3125
[<CompiledName("IterateIndexed")>]
3226
letiteri action(str:string)=
33-
letstr= emptyIfNullstr
34-
letf= OptimizedClosures.FSharpFunc<_,_,_>.Adapt(action)
35-
for i=0to str.Length-1do
36-
f.Invoke(i, str.[i])
27+
ifnot(String.IsNullOrEmptystr)then
28+
letf= OptimizedClosures.FSharpFunc<_,_,_>.Adapt(action)
29+
for i=0to str.Length-1do
30+
f.Invoke(i, str.[i])
3731

3832
[<CompiledName("Map")>]
3933
letmap(mapping:char->char)(str:string)=
40-
letstr= emptyIfNull str
41-
letres= 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+
letres= StringBuilder str.Length
38+
str|> iter(fun c-> res.Append(mapping c)|> ignore)
39+
res.ToString()
4440

4541
[<CompiledName("MapIndexed")>]
4642
letmapi(mapping:int->char->char)(str:string)=
47-
letstr= emptyIfNull str
48-
letres= StringBuilder str.Length
49-
letf= 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+
letres= StringBuilder str.Length
47+
letf= OptimizedClosures.FSharpFunc<_,_,_>.Adapt(mapping)
48+
str|> iteri(fun i c-> res.Append(f.Invoke(i, c))|> ignore)
49+
res.ToString()
5250

5351
[<CompiledName("Filter")>]
5452
letfilter(predicate:char->bool)(str:string)=
55-
letstr= emptyIfNull str
56-
letres= 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+
letres= StringBuilder str.Length
57+
str|> iter(fun c->if predicate cthen res.Append c|> ignore)
58+
res.ToString()
5959

6060
[<CompiledName("Collect")>]
6161
letcollect(mapping:char->string)(str:string)=
62-
letstr= emptyIfNull str
63-
letres= 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+
letres= StringBuilder str.Length
66+
str|> iter(fun c-> res.Append(mapping c)|> ignore)
67+
res.ToString()
6668

6769
[<CompiledName("Initialize")>]
6870
letinit(count:int)(initializer:int->string)=
@@ -75,25 +77,34 @@ namespace Microsoft.FSharp.Core
7577
[<CompiledName("Replicate")>]
7678
letreplicate(count:int)(str:string)=
7779
if count<0then invalidArgInputMustBeNonNegative"count" count
78-
letstr= emptyIfNull str
79-
letres= StringBuilder(count* str.Length)
80-
for i=0to count-1do
81-
res.Append str|> ignore
82-
res.ToString()
80+
81+
if String.IsNullOrEmpty strthen
82+
String.Empty
83+
else
84+
letres= StringBuilder(count* str.Length)
85+
for i=0to count-1do
86+
res.Append str|> ignore
87+
res.ToString()
8388

8489
[<CompiledName("ForAll")>]
8590
letforall predicate(str:string)=
86-
letstr= emptyIfNull str
87-
let reccheck i=(i>= str.Length)||(predicate str.[i]&& check(i+1))
88-
check0
91+
if String.IsNullOrEmpty strthen
92+
true
93+
else
94+
let reccheck i=(i>= str.Length)||(predicate str.[i]&& check(i+1))
95+
check0
8996

9097
[<CompiledName("Exists")>]
9198
letexists predicate(str:string)=
92-
letstr= emptyIfNull str
93-
let reccheck i=(i< str.Length)&&(predicate str.[i]|| check(i+1))
94-
check0
99+
if String.IsNullOrEmpty strthen
100+
false
101+
else
102+
let reccheck i=(i< str.Length)&&(predicate str.[i]|| check(i+1))
103+
check0
95104

96105
[<CompiledName("Length")>]
97106
letlength(str:string)=
98-
letstr= emptyIfNull str
99-
str.Length
107+
if String.IsNullOrEmpty strthen
108+
0
109+
else
110+
str.Length

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp