Skip to content

Commit 0fc047c

Browse files
authored
Use IndexOfAnyValues in Cookie DomainCharsTest (#78896)
1 parent f35444a commit 0fc047c

File tree

1 file changed

+6
-16
lines changed
  • src/libraries/System.Net.Primitives/src/System/Net

1 file changed

+6
-16
lines changed

src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public sealed class Cookie
4444
// Space (' ') should be reserved as well per RFCs, but major web browsers support it and some web sites use it - so we support it too
4545
private static readonly IndexOfAnyValues<char> s_reservedToNameChars = IndexOfAnyValues.Create("\t\r\n=;,");
4646

47+
private static readonly IndexOfAnyValues<char> s_domainChars =
48+
IndexOfAnyValues.Create("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz");
49+
4750
private string m_comment = string.Empty; // Do not rename (binary serialization)
4851
private Uri? m_commentUri; // Do not rename (binary serialization)
4952
private CookieVariant m_cookieVariant = CookieVariant.Plain; // Do not rename (binary serialization)
@@ -560,22 +563,9 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma
560563

561564
// Very primitive test to make sure that the name does not have illegal characters
562565
// as per RFC 952 (relaxed on first char could be a digit and string can have '_').
563-
private static bool DomainCharsTest(string name)
564-
{
565-
if (name == null || name.Length == 0)
566-
{
567-
return false;
568-
}
569-
for (int i = 0; i < name.Length; ++i)
570-
{
571-
char ch = name[i];
572-
if (!(char.IsAsciiLetterOrDigit(ch) || ch == '.' || ch == '-' || ch == '_'))
573-
{
574-
return false;
575-
}
576-
}
577-
return true;
578-
}
566+
private static bool DomainCharsTest(string name) =>
567+
!string.IsNullOrEmpty(name) &&
568+
name.AsSpan().IndexOfAnyExcept(s_domainChars) < 0;
579569

580570
[AllowNull]
581571
public string Port

0 commit comments

Comments
 (0)