Skip to content

Commit 12cf0f3

Browse files
authored
Use string.StartsWith(char) and string.EndsWith(char) where applicable (#44310)
1 parent bc1ff01 commit 12cf0f3

File tree

23 files changed

+36
-36
lines changed

23 files changed

+36
-36
lines changed

src/Components/test/E2ETest/Infrastructure/WebDriverExtensions/BasicTestAppAuthenticationWebDriverExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal static class BasicTestAppAuthenticationWebDriverExtensions
1010
{
1111
public static void SignInAs(this IWebDriver browser, Uri baseUri, string usernameOrNull, string rolesOrNull, bool useSeparateTab = false)
1212
{
13-
var basePath = baseUri.LocalPath.EndsWith("/", StringComparison.Ordinal) ? baseUri.LocalPath : baseUri.LocalPath + "/";
13+
var basePath = baseUri.LocalPath.EndsWith('/') ? baseUri.LocalPath : baseUri.LocalPath + "/";
1414
var authenticationPageUrl = $"{basePath}Authentication";
1515
var baseRelativeUri = usernameOrNull == null
1616
? $"{authenticationPageUrl}?signout=true"

src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/JsonTranscodingRouteAdapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal;
2222
/// For example, consider a multi-segment parameter route:
2323
/// - Before: /v1/{book.name=shelves/*/books/*}
2424
/// - After: /v1/shelves/{__Complex_book.name_2}/books/{__Complex_book.name_4}
25-
///
25+
///
2626
/// It is rewritten so that any * or ** segments become ASP.NET Core route parameters. These parameter
2727
/// names are never used by the user, and instead they're reconstructed into the final value by the
2828
/// adapter and then added to the HttpRequest.RouteValues collection.
@@ -178,7 +178,7 @@ private static SegmentType GetSegmentType(string segment)
178178
{
179179
return SegmentType.CatchAll;
180180
}
181-
else if (segment.StartsWith("*", StringComparison.Ordinal))
181+
else if (segment.StartsWith('*'))
182182
{
183183
return SegmentType.Any;
184184
}

src/Http/Headers/src/ContentDispositionHeaderValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ private StringSegment GetName(string parameter)
400400
{
401401
string? result;
402402
// filename*=utf-8'lang'%7FMyString
403-
if (parameter.EndsWith("*", StringComparison.Ordinal))
403+
if (parameter.EndsWith('*'))
404404
{
405405
if (TryDecode5987(nameParameter.Value, out result))
406406
{

src/Http/Http.Abstractions/src/Extensions/MapExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static IApplicationBuilder Map(this IApplicationBuilder app, PathString p
5858
throw new ArgumentNullException(nameof(configuration));
5959
}
6060

61-
if (pathMatch.HasValue && pathMatch.Value!.EndsWith("/", StringComparison.Ordinal))
61+
if (pathMatch.HasValue && pathMatch.Value!.EndsWith('/'))
6262
{
6363
throw new ArgumentException("The path must not end with a '/'", nameof(pathMatch));
6464
}

src/Http/Http.Results/src/VirtualFileHttpResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private static Task ExecuteCoreAsync(HttpContext httpContext, RangeItemHeaderVal
152152
internal IFileInfo GetFileInformation(IFileProvider fileProvider)
153153
{
154154
var normalizedPath = FileName;
155-
if (normalizedPath.StartsWith("~", StringComparison.Ordinal))
155+
if (normalizedPath.StartsWith('~'))
156156
{
157157
normalizedPath = normalizedPath.Substring(1);
158158
}

src/Http/Routing.Abstractions/src/VirtualPathData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static string NormalizePath(string path)
8686
return string.Empty;
8787
}
8888

89-
if (!path.StartsWith("/", StringComparison.Ordinal))
89+
if (!path.StartsWith('/'))
9090
{
9191
return "/" + path;
9292
}

src/Http/Routing/src/ParameterPolicyActivator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static T ResolveParameterPolicy<T>(
3535

3636
string argumentString;
3737
var indexOfFirstOpenParens = inlineParameterPolicy.IndexOf('(');
38-
if (indexOfFirstOpenParens >= 0 && inlineParameterPolicy.EndsWith(")", StringComparison.Ordinal))
38+
if (indexOfFirstOpenParens >= 0 && inlineParameterPolicy.EndsWith(')'))
3939
{
4040
parameterPolicyKey = inlineParameterPolicy.Substring(0, indexOfFirstOpenParens);
4141
argumentString = inlineParameterPolicy.Substring(

src/Http/Routing/src/Patterns/RoutePatternParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private static bool ParseParameter(Context context, List<RoutePatternPart> parts
220220
var templatePart = RouteParameterParser.ParseRouteParameter(decoded);
221221

222222
// See #475 - this is here because InlineRouteParameterParser can't return errors
223-
if (decoded.StartsWith("*", StringComparison.Ordinal) && decoded.EndsWith("?", StringComparison.Ordinal))
223+
if (decoded.StartsWith('*') && decoded.EndsWith('?'))
224224
{
225225
context.Error = Resources.TemplateRoute_CatchAllCannotBeOptional;
226226
return false;
@@ -466,11 +466,11 @@ private static string TrimPrefix(string routePattern)
466466
{
467467
return routePattern.Substring(2);
468468
}
469-
else if (routePattern.StartsWith("/", StringComparison.Ordinal))
469+
else if (routePattern.StartsWith('/'))
470470
{
471471
return routePattern.Substring(1);
472472
}
473-
else if (routePattern.StartsWith("~", StringComparison.Ordinal))
473+
else if (routePattern.StartsWith('~'))
474474
{
475475
throw new RoutePatternException(routePattern, Resources.TemplateRoute_InvalidRouteTemplate);
476476
}

src/Http/Routing/src/RouteCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public virtual async Task RouteAsync(RouteContext context)
176176
queryString = queryString.ToLowerInvariant();
177177
}
178178

179-
if (_options.AppendTrailingSlash && !urlWithoutQueryString.EndsWith("/", StringComparison.Ordinal))
179+
if (_options.AppendTrailingSlash && !urlWithoutQueryString.EndsWith('/'))
180180
{
181181
urlWithoutQueryString += "/";
182182
}

src/Middleware/StaticFiles/src/Infrastructure/SharedOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public PathString RequestPath
2929
get { return _requestPath; }
3030
set
3131
{
32-
if (value.HasValue && value.Value!.EndsWith("/", StringComparison.Ordinal))
32+
if (value.HasValue && value.Value!.EndsWith('/'))
3333
{
3434
throw new ArgumentException("Request path must not end in a slash");
3535
}

src/Mvc/Mvc.Core/src/ApplicationModels/AttributeRouteModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public static bool IsOverridePattern([StringSyntax("Route")] string? template)
160160
{
161161
return template != null &&
162162
(template.StartsWith("~/", StringComparison.Ordinal) ||
163-
template.StartsWith("/", StringComparison.Ordinal));
163+
template.StartsWith('/'));
164164
}
165165

166166
private static string? ChooseName(
@@ -192,7 +192,7 @@ public static bool IsOverridePattern([StringSyntax("Route")] string? template)
192192
return right;
193193
}
194194

195-
if (left!.EndsWith("/", StringComparison.Ordinal))
195+
if (left!.EndsWith('/'))
196196
{
197197
return left + right;
198198
}
@@ -225,7 +225,7 @@ private static bool IsEmptyLeftSegment(string? template)
225225
}
226226

227227
var startIndex = 0;
228-
if (result.StartsWith("/", StringComparison.Ordinal))
228+
if (result.StartsWith('/'))
229229
{
230230
startIndex = 1;
231231
}
@@ -241,7 +241,7 @@ private static bool IsEmptyLeftSegment(string? template)
241241
}
242242

243243
var subStringLength = result.Length - startIndex;
244-
if (result.EndsWith("/", StringComparison.Ordinal))
244+
if (result.EndsWith('/'))
245245
{
246246
subStringLength--;
247247
}

src/Mvc/Mvc.Core/src/Formatters/FormatterMappings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static string RemovePeriodIfPresent(string format)
114114
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(format));
115115
}
116116

117-
if (format.StartsWith(".", StringComparison.Ordinal))
117+
if (format.StartsWith('.'))
118118
{
119119
if (format == ".")
120120
{

src/Mvc/Mvc.Core/src/Infrastructure/VirtualFileResultExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ internal static IFileInfo GetFileInformation(VirtualFileResult result, IWebHostE
128128
}
129129

130130
var normalizedPath = result.FileName;
131-
if (normalizedPath.StartsWith("~", StringComparison.Ordinal))
131+
if (normalizedPath.StartsWith('~'))
132132
{
133133
normalizedPath = normalizedPath.Substring(1);
134134
}

src/Mvc/Mvc.Core/src/Routing/UrlHelperBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,12 @@ internal static void AppendPathAndFragment(StringBuilder builder, PathString pat
443443
{
444444
builder.Append(pathBase.Value);
445445

446-
if (pathBase.Value.EndsWith("/", StringComparison.Ordinal))
446+
if (pathBase.Value.EndsWith('/'))
447447
{
448448
builder.Length--;
449449
}
450450

451-
if (!virtualPath.StartsWith("/", StringComparison.Ordinal))
451+
if (!virtualPath.StartsWith('/'))
452452
{
453453
builder.Append('/');
454454
}
@@ -483,7 +483,7 @@ private bool TryFastGenerateUrl(
483483
url = "/";
484484
return true;
485485
}
486-
else if (virtualPath.StartsWith("/", StringComparison.Ordinal))
486+
else if (virtualPath.StartsWith('/'))
487487
{
488488
url = virtualPath;
489489
return true;

src/Mvc/Mvc.Razor.RuntimeCompilation/src/FileProviderRazorProjectFileSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ private IEnumerable<RazorProjectItem> EnumerateFiles(IDirectoryContents director
7878

7979
private static string JoinPath(string path1, string path2)
8080
{
81-
var hasTrailingSlash = path1.EndsWith("/", StringComparison.Ordinal);
82-
var hasLeadingSlash = path2.StartsWith("/", StringComparison.Ordinal);
81+
var hasTrailingSlash = path1.EndsWith('/');
82+
var hasLeadingSlash = path2.StartsWith('/');
8383
if (hasLeadingSlash && hasTrailingSlash)
8484
{
8585
return string.Concat(path1, path2.AsSpan(1));

src/Mvc/Mvc.RazorPages/src/ApplicationModels/PageRouteModelFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ internal bool TryParseAreaPath(
8686
const string AreaPagesRoot = "/Pages/";
8787

8888
result = default;
89-
Debug.Assert(relativePath.StartsWith("/", StringComparison.Ordinal));
89+
Debug.Assert(relativePath.StartsWith('/'));
9090
// Parse the area root directory.
9191
var areaRootEndIndex = relativePath.IndexOf('/', startIndex: 1);
9292
if (areaRootEndIndex == -1 ||
@@ -139,7 +139,7 @@ private static string CreateAreaRoute(string areaName, string viewEnginePath)
139139
// Result = /Products/List/Categories
140140
Debug.Assert(!string.IsNullOrEmpty(areaName));
141141
Debug.Assert(!string.IsNullOrEmpty(viewEnginePath));
142-
Debug.Assert(viewEnginePath.StartsWith("/", StringComparison.Ordinal));
142+
Debug.Assert(viewEnginePath.StartsWith('/'));
143143

144144
return string.Create(1 + areaName.Length + viewEnginePath.Length, (areaName, viewEnginePath), (span, tuple) =>
145145
{
@@ -172,8 +172,8 @@ private static SelectorModel CreateSelectorModel(string prefix, string? routeTem
172172

173173
private static string NormalizeDirectory(string directory)
174174
{
175-
Debug.Assert(directory.StartsWith("/", StringComparison.Ordinal));
176-
if (directory.Length > 1 && !directory.EndsWith("/", StringComparison.Ordinal))
175+
Debug.Assert(directory.StartsWith('/'));
176+
if (directory.Length > 1 && !directory.EndsWith('/'))
177177
{
178178
return directory + "/";
179179
}

src/Mvc/Mvc.RazorPages/src/DependencyInjection/RazorPagesRazorViewEngineOptionsSetup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public void Configure(RazorViewEngineOptions options)
6565

6666
private static string CombinePath(string path1, string path2)
6767
{
68-
if (path1.EndsWith("/", StringComparison.Ordinal) || path2.StartsWith("/", StringComparison.Ordinal))
68+
if (path1.EndsWith('/') || path2.StartsWith('/'))
6969
{
7070
return path1 + path2;
7171
}
72-
else if (path1.EndsWith("/", StringComparison.Ordinal) && path2.StartsWith("/", StringComparison.Ordinal))
72+
else if (path1.EndsWith('/') && path2.StartsWith('/'))
7373
{
7474
return string.Concat(path1, path2.AsSpan(1));
7575
}

src/Mvc/Mvc.ViewFeatures/src/NameAndIdProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static string GetFullHtmlFieldName(ViewContext viewContext, string expres
176176

177177
previousNameAndId.HtmlFieldPrefix = htmlFieldPrefix;
178178
previousNameAndId.Expression = expression;
179-
if (expression.StartsWith("[", StringComparison.Ordinal))
179+
if (expression.StartsWith('['))
180180
{
181181
// The expression might represent an indexer access, in which case with a 'dot' would be invalid.
182182
previousNameAndId.OutputFullName = htmlFieldPrefix + expression;

src/Mvc/Mvc.ViewFeatures/src/TemplateInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public string GetFullHtmlFieldName(string partialFieldName)
9797
return partialFieldName;
9898
}
9999

100-
if (partialFieldName.StartsWith("[", StringComparison.Ordinal))
100+
if (partialFieldName.StartsWith('['))
101101
{
102102
// The partialFieldName might represent an indexer access, in which case combining
103103
// with a 'dot' would be invalid.

src/Security/Authentication/OpenIdConnect/src/OpenIdConnectHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ private string BuildRedirectUriIfRelative(string uri)
12901290
return uri;
12911291
}
12921292

1293-
if (!uri.StartsWith("/", StringComparison.Ordinal))
1293+
if (!uri.StartsWith('/'))
12941294
{
12951295
return uri;
12961296
}

src/Security/Authentication/OpenIdConnect/src/OpenIdConnectPostConfigureOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void PostConfigure(string? name, OpenIdConnectOptions options)
8787
if (string.IsNullOrEmpty(options.MetadataAddress) && !string.IsNullOrEmpty(options.Authority))
8888
{
8989
options.MetadataAddress = options.Authority;
90-
if (!options.MetadataAddress.EndsWith("/", StringComparison.Ordinal))
90+
if (!options.MetadataAddress.EndsWith('/'))
9191
{
9292
options.MetadataAddress += "/";
9393
}

src/Security/Authentication/WsFederation/src/WsFederationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ private string BuildRedirectUriIfRelative(string uri)
418418
return uri;
419419
}
420420

421-
if (!uri.StartsWith("/", StringComparison.Ordinal))
421+
if (!uri.StartsWith('/'))
422422
{
423423
return uri;
424424
}

src/Servers/HttpSys/src/UrlPrefix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static UrlPrefix Create(string scheme, string host, int? portValue, strin
8888
{
8989
path = "/";
9090
}
91-
else if (!path.EndsWith("/", StringComparison.Ordinal))
91+
else if (!path.EndsWith('/'))
9292
{
9393
path += "/";
9494
}

0 commit comments

Comments
 (0)