Skip to content

Use char overloads to Join/Index strings #56531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Hosting/TestHost/src/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
// User-Agent is a space delineated single line header but HttpRequestHeaders parses it as multiple elements.
if (string.Equals(header.Key, HeaderNames.UserAgent, StringComparison.OrdinalIgnoreCase))
{
req.Headers.Append(header.Key, string.Join(" ", header.Value));
req.Headers.Append(header.Key, string.Join(' ', header.Value));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void StoreTokens(this AuthenticationProperties properties, IEnumer

if (tokenNames.Count > 0)
{
properties.Items[TokenNamesKey] = string.Join(";", tokenNames);
properties.Items[TokenNamesKey] = string.Join(';', tokenNames);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Http.Abstractions/src/Internal/ParsingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void SetHeaderJoined(IHeaderDictionary headers, string key, String
}
else
{
headers[key] = string.Join(",", value.Select(QuoteIfNeeded));
headers[key] = string.Join(',', value.Select(QuoteIfNeeded));
}
}

Expand Down Expand Up @@ -116,7 +116,7 @@ public static void AppendHeaderJoined(IHeaderDictionary headers, string key, par
}
else
{
headers[key] = existing + "," + string.Join(",", values.Select(QuoteIfNeeded));
headers[key] = existing + "," + string.Join(',', values.Select(QuoteIfNeeded));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Http.Abstractions/src/WebSocketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private string DebuggerToString()
return IsWebSocketRequest switch
{
false => "IsWebSocketRequest = false",
true => $"IsWebSocketRequest = true, RequestedProtocols = {string.Join(",", WebSocketRequestedProtocols)}",
true => $"IsWebSocketRequest = true, RequestedProtocols = {string.Join(',', WebSocketRequestedProtocols)}",
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/Http/Http/src/BindingAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ public static BindingAddress Parse(string address)
}
}

pathDelimiterStart = address.IndexOf(":", schemeDelimiterEnd + unixPipeHostPrefixLength, StringComparison.Ordinal);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not your fault, but it seems very weird that the char version doesn't (AFAICT) accept a StringComparison. I'm assuming the default is Ordinal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll double-check this one locally in an IDE.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find one a char, int, StringComparison overload.

The IDE code-fixer also suggests exactly what's here:

image

I'm gonna guess that it does an ordinal check anyway and in this case as it's punctuation it's moot.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have been clearer - there isn't a better overload you could have called. I don't know why that is, but you don't need to do anything about it.

I just confirmed in the source that the default comparison is Ordinal so the change looks correct (if, sadly, less explicit).

pathDelimiterStart = address.IndexOf(':', schemeDelimiterEnd + unixPipeHostPrefixLength);
pathDelimiterEnd = pathDelimiterStart + ":".Length;
}
else if (isNamedPipe)
{
pathDelimiterStart = address.IndexOf(":", schemeDelimiterEnd + NamedPipeHostPrefix.Length, StringComparison.Ordinal);
pathDelimiterStart = address.IndexOf(':', schemeDelimiterEnd + NamedPipeHostPrefix.Length);
pathDelimiterEnd = pathDelimiterStart + ":".Length;
}
else
{
pathDelimiterStart = address.IndexOf("/", schemeDelimiterEnd, StringComparison.Ordinal);
pathDelimiterStart = address.IndexOf('/', schemeDelimiterEnd);
pathDelimiterEnd = pathDelimiterStart;
}

Expand All @@ -205,7 +205,7 @@ public static BindingAddress Parse(string address)
var hasSpecifiedPort = false;
if (!isUnixPipe)
{
var portDelimiterStart = address.LastIndexOf(":", pathDelimiterStart - 1, pathDelimiterStart - schemeDelimiterEnd, StringComparison.Ordinal);
var portDelimiterStart = address.LastIndexOf(':', pathDelimiterStart - 1, pathDelimiterStart - schemeDelimiterEnd);
if (portDelimiterStart >= 0)
{
var portDelimiterEnd = portDelimiterStart + ":".Length;
Expand Down
24 changes: 12 additions & 12 deletions src/Http/Http/src/Features/FormFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ private MediaTypeHeaderValue? ContentType
{
get
{
MediaTypeHeaderValue? mt = null;
MediaTypeHeaderValue? mt = null;

if (_request is not null)
{
_ = MediaTypeHeaderValue.TryParse(_request.ContentType, out mt);
}
if (_request is not null)
{
_ = MediaTypeHeaderValue.TryParse(_request.ContentType, out mt);
}

if (_form is not null && mt is null)
{
mt = _formContentType;
}
if (_form is not null && mt is null)
{
mt = _formContentType;
}

return mt;
return mt;
}
}

Expand Down Expand Up @@ -127,9 +127,9 @@ public IFormCollection? Form
{
_formContentType = null;
}
else
else
{
_formContentType ??= new MediaTypeHeaderValue("application/x-www-form-urlencoded");
_formContentType ??= new MediaTypeHeaderValue("application/x-www-form-urlencoded");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Routing/src/HostAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override string ToString()
{
var hostsDisplay = (Hosts.Count == 0)
? "*:*"
: string.Join(",", Hosts.Select(h => h.Contains(':') ? h : h + ":*"));
: string.Join(',', Hosts.Select(h => h.Contains(':') ? h : h + ":*"));

return DebuggerHelpers.GetDebugText(nameof(Hosts), hostsDisplay);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static CompositeBindingSource Create(
}
}

var id = string.Join("&", bindingSources.Select(s => s.Id).OrderBy(s => s, StringComparer.Ordinal));
var id = string.Join('&', bindingSources.Select(s => s.Id).OrderBy(s => s, StringComparer.Ordinal));
return new CompositeBindingSource(id, displayName, bindingSources);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Security/Authorization/Policy/src/PolicyEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public virtual async Task<AuthenticateResult> AuthenticateAsync(AuthorizationPol
if (newPrincipal != null)
{
context.User = newPrincipal;
var ticket = new AuthenticationTicket(newPrincipal, string.Join(";", policy.AuthenticationSchemes));
var ticket = new AuthenticationTicket(newPrincipal, string.Join(';', policy.AuthenticationSchemes));
// ExpiresUtc is the easiest property to reason about when dealing with multiple schemes
// SignalR will use this property to evaluate auth expiration for long running connections
ticket.Properties.ExpiresUtc = minExpiresUtc;
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Diagnostics/BaseView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void EndWriteAttribute()
Debug.Assert(AttributeValues != null);
Debug.Assert(!string.IsNullOrEmpty(AttributeEnding));

var attributes = string.Join(" ", AttributeValues);
var attributes = string.Join(' ', AttributeValues);
Output.Write(attributes);
AttributeValues = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Shared/RazorViews/BaseView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected void EndWriteAttribute()
Debug.Assert(AttributeValues != null);
Debug.Assert(!string.IsNullOrEmpty(AttributeEnding));

var attributes = string.Join(" ", AttributeValues);
var attributes = string.Join(' ', AttributeValues);
Output.Write(attributes);
AttributeValues = null;

Expand Down
Loading