Skip to content

Commit fd1f1c3

Browse files
Use preferred overloads of string.Split() (#23712)
* Use preferred overloads of string.Split * Revert TrimEntries for netstandard2.0 Revert usage of StringSplit.TrimEntries for projects that target netstandard2.0. Co-authored-by: Levi Broderick <[email protected]>
1 parent a738d1c commit fd1f1c3

File tree

4 files changed

+5
-41
lines changed

4 files changed

+5
-41
lines changed

src/Hosting/Hosting/src/Internal/WebHostOptions.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,8 @@ public IEnumerable<string> GetFinalHostingStartupAssemblies()
7676

7777
private IReadOnlyList<string> Split(string value)
7878
{
79-
if (string.IsNullOrWhiteSpace(value))
80-
{
81-
return Array.Empty<string>();
82-
}
83-
84-
var list = new List<string>();
85-
foreach (var part in value.Split(';', StringSplitOptions.RemoveEmptyEntries))
86-
{
87-
var trimmedPart = part;
88-
if (!string.IsNullOrEmpty(trimmedPart))
89-
{
90-
list.Add(trimmedPart);
91-
}
92-
}
93-
return list;
79+
return value?.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
80+
?? Array.Empty<string>();
9481
}
9582
}
9683
}

src/Http/Routing/src/ParameterPolicyActivator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ private static IParameterPolicy CreateParameterPolicy(IServiceProvider servicePr
100100
}
101101
else
102102
{
103-
var arguments = !string.IsNullOrEmpty(argumentString)
104-
? argumentString.Split(',').Select(argument => argument.Trim()).ToArray()
105-
: Array.Empty<string>();
103+
var arguments = argumentString?.Split(',', StringSplitOptions.TrimEntries) ?? Array.Empty<string>();
106104

107105
// We want to find the constructors that match the number of passed in arguments
108106
// We either want a single match, or a single best match. The best match is the one with the most

src/Mvc/Mvc.Core/src/BindAttribute.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,6 @@ public Func<ModelMetadata, bool> PropertyFilter
7171
}
7272

7373
private static IEnumerable<string> SplitString(string original)
74-
{
75-
if (string.IsNullOrEmpty(original))
76-
{
77-
return Array.Empty<string>();
78-
}
79-
80-
var split = original.Split(',').Select(piece => piece.Trim()).Where(piece => !string.IsNullOrEmpty(piece));
81-
82-
return split;
83-
}
74+
=> original?.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
8475
}
8576
}

src/Mvc/Mvc.ViewFeatures/src/RemoteAttributeBase.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,7 @@ private static void MergeAttribute(IDictionary<string, string> attributes, strin
173173
}
174174

175175
private static IEnumerable<string> SplitAndTrimPropertyNames(string original)
176-
{
177-
if (string.IsNullOrEmpty(original))
178-
{
179-
return Array.Empty<string>();
180-
}
181-
182-
var split = original
183-
.Split(',')
184-
.Select(piece => piece.Trim())
185-
.Where(trimmed => !string.IsNullOrEmpty(trimmed));
186-
187-
return split;
188-
}
176+
=> original?.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
189177

190178
private void CheckForLocalizer(ClientModelValidationContext context)
191179
{

0 commit comments

Comments
 (0)