Skip to content

Commit eeeeb7a

Browse files
Optimize string.EndsWith(char) for const values (#69038)
1 parent 6a00977 commit eeeeb7a

File tree

32 files changed

+102
-76
lines changed

32 files changed

+102
-76
lines changed

src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,7 @@ private static void FilterHelper(
20792079
listType = MemberListType.CaseSensitive;
20802080
}
20812081

2082-
if (allowPrefixLookup && name.EndsWith("*", StringComparison.Ordinal))
2082+
if (allowPrefixLookup && name.EndsWith('*'))
20832083
{
20842084
// We set prefixLookup to true if name ends with a "*".
20852085
// We will also set listType to All so that all members are included in

src/libraries/Common/src/System/Net/CookieParser.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,9 @@ private static FieldInfo IsQuotedVersionField
855855

856856
internal static string CheckQuoted(string value)
857857
{
858-
if (value.Length < 2 || value[0] != '\"' || value[value.Length - 1] != '\"')
859-
return value;
860-
861-
return value.Length == 2 ? string.Empty : value.Substring(1, value.Length - 2);
858+
return (value.Length >= 2 && value.StartsWith('\"') && value.EndsWith('\"'))
859+
? value.Substring(1, value.Length - 2)
860+
: value;
862861
}
863862

864863
internal bool EndofHeader()

src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ internal static string GetPipePath(string serverName, string pipeName)
202202
// cross-platform with Windows (which has only '\' as an invalid char).
203203
if (Path.IsPathRooted(pipeName))
204204
{
205-
if (pipeName.IndexOfAny(s_invalidPathNameChars) >= 0 || pipeName[pipeName.Length - 1] == Path.DirectorySeparatorChar)
205+
if (pipeName.IndexOfAny(s_invalidPathNameChars) >= 0 || pipeName.EndsWith(Path.DirectorySeparatorChar))
206206
throw new PlatformNotSupportedException(SR.PlatformNotSupported_InvalidPipeNameChars);
207207

208208
// Caller is in full control of file location.

src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private static bool TrySetOptionalTokenList(NameValueHeaderValue nameValue, ref
537537
// We need the string to be at least 3 chars long: 2x quotes and at least 1 character. Also make sure we
538538
// have a quoted string. Note that NameValueHeaderValue will never have leading/trailing whitespace.
539539
string valueString = nameValue.Value;
540-
if ((valueString.Length < 3) || (valueString[0] != '\"') || (valueString[valueString.Length - 1] != '\"'))
540+
if ((valueString.Length < 3) || !valueString.StartsWith('\"') || !valueString.EndsWith('\"'))
541541
{
542542
return false;
543543
}

src/libraries/System.Net.Http/src/System/Net/Http/Headers/NameValueHeaderValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private static void CheckValueFormat(string? value)
358358
}
359359

360360
// Trailing/leading space are not allowed
361-
if (value[0] == ' ' || value[0] == '\t' || value[^1] == ' ' || value[^1] == '\t')
361+
if (value.StartsWith(' ') || value.StartsWith('\t') || value.EndsWith(' ') || value.EndsWith('\t'))
362362
{
363363
ThrowFormatException(value);
364364
}

src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ internal static string ReadBufferAsString(ArraySegment<byte> buffer, HttpContent
192192
{
193193
// Remove at most a single set of quotes.
194194
if (charset.Length > 2 &&
195-
charset[0] == '\"' &&
196-
charset[charset.Length - 1] == '\"')
195+
charset.StartsWith('\"') &&
196+
charset.EndsWith('\"'))
197197
{
198198
encoding = Encoding.GetEncoding(charset.Substring(1, charset.Length - 2));
199199
}

src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ internal void AddPrefix(string uriPrefix)
146146
{
147147
throw new ArgumentException(SR.net_listener_host, nameof(uriPrefix));
148148
}
149-
if (uriPrefix[uriPrefix.Length - 1] != '/')
149+
if (!uriPrefix.EndsWith('/'))
150150
{
151151
throw new ArgumentException(SR.net_listener_slash, nameof(uriPrefix));
152152
}

src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ internal string GetString()
537537

538538
internal static void FillFromString(NameValueCollection nvc, string s, bool urlencoded, Encoding encoding)
539539
{
540+
int i = s.StartsWith('?') ? 1 : 0;
540541
int l = s.Length;
541-
int i = (l > 0 && s[0] == '?') ? 1 : 0;
542542

543543
while (i < l)
544544
{

src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpEndPointListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static void UnbindContext(HttpListenerContext context)
178178
string host = uri.Host;
179179
int port = uri.Port;
180180
string path = WebUtility.UrlDecode(uri.AbsolutePath);
181-
string pathSlash = path[path.Length - 1] == '/' ? path : path + "/";
181+
string pathSlash = path.EndsWith('/') ? path : path + "/";
182182

183183
HttpListener? bestMatch = null;
184184
int bestLength = -1;

src/libraries/System.Net.Mail/src/System/Net/Mail/Attachment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public string ContentId
204204
ContentId = cid;
205205
return cid;
206206
}
207-
if (cid.Length >= 2 && cid[0] == '<' && cid[cid.Length - 1] == '>')
207+
if (cid.StartsWith('<') && cid.EndsWith('>'))
208208
{
209209
return cid.Substring(1, cid.Length - 2);
210210
}

0 commit comments

Comments
 (0)