From 15fca2df79ef4c5b13f2f2e9ce461436e1652212 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Mon, 28 Nov 2022 01:32:22 +0100 Subject: [PATCH] Use IndexOfAnyValues in Cookie DomainCharsTest --- .../src/System/Net/Cookie.cs | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs b/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs index 12ca4d8ef80cc2..190fd9f4e991ad 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs @@ -44,6 +44,9 @@ public sealed class Cookie // 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 private static readonly IndexOfAnyValues s_reservedToNameChars = IndexOfAnyValues.Create("\t\r\n=;,"); + private static readonly IndexOfAnyValues s_domainChars = + IndexOfAnyValues.Create("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"); + private string m_comment = string.Empty; // Do not rename (binary serialization) private Uri? m_commentUri; // Do not rename (binary serialization) private CookieVariant m_cookieVariant = CookieVariant.Plain; // Do not rename (binary serialization) @@ -560,22 +563,9 @@ internal bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDoma // Very primitive test to make sure that the name does not have illegal characters // as per RFC 952 (relaxed on first char could be a digit and string can have '_'). - private static bool DomainCharsTest(string name) - { - if (name == null || name.Length == 0) - { - return false; - } - for (int i = 0; i < name.Length; ++i) - { - char ch = name[i]; - if (!(char.IsAsciiLetterOrDigit(ch) || ch == '.' || ch == '-' || ch == '_')) - { - return false; - } - } - return true; - } + private static bool DomainCharsTest(string name) => + !string.IsNullOrEmpty(name) && + name.AsSpan().IndexOfAnyExcept(s_domainChars) < 0; [AllowNull] public string Port