From d023bc99bd60b9fa42e4410d4c1db077ea698ec2 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Sat, 6 Aug 2016 19:36:27 -0700 Subject: [PATCH] HttpClient: Faster whitespace detection Instead of allocating a List with the four allowed whitespace chars and searching the list, just check if the char is one of the four values. Commit migrated from https://github.com/dotnet/corefx/commit/bcc05791de64688b681e01974a0ba8f7d75f2501 --- .../src/Internal/Mail/MailAddressParser.cs | 2 +- .../src/Internal/Mail/MailBnfHelper.cs | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs b/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs index 4dc40953442..8f65d346459 100644 --- a/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs +++ b/src/libraries/System.Net.Http/src/Internal/Mail/MailAddressParser.cs @@ -223,7 +223,7 @@ namespace System.Net.Mail // before the next address. if (index >= 0 && !( - MailBnfHelper.Whitespace.Contains(data[index]) // < local@domain > + MailBnfHelper.IsAllowedWhiteSpace(data[index]) // < local@domain > || data[index] == MailBnfHelper.EndComment // <(comment)local@domain> || (expectAngleBracket && data[index] == MailBnfHelper.StartAngleBracket) // || (expectMultipleAddresses && data[index] == MailBnfHelper.Comma) // local@dom,local@dom diff --git a/src/libraries/System.Net.Http/src/Internal/Mail/MailBnfHelper.cs b/src/libraries/System.Net.Http/src/Internal/Mail/MailBnfHelper.cs index 0362230a3b6..b195645e417 100644 --- a/src/libraries/System.Net.Http/src/Internal/Mail/MailBnfHelper.cs +++ b/src/libraries/System.Net.Http/src/Internal/Mail/MailBnfHelper.cs @@ -47,18 +47,6 @@ namespace System.Net.Mime internal static readonly char EndSquareBracket = ']'; internal static readonly char Comma = ','; internal static readonly char Dot = '.'; - internal static readonly IList Whitespace = CreateAllowedWhitespace(); - - private static List CreateAllowedWhitespace() - { - // all allowed whitespace characters - var whitespace = new List(4); - whitespace.Add(Tab); - whitespace.Add(Space); - whitespace.Add(CR); - whitespace.Add(LF); - return whitespace; - } // NOTE: See RFC 2822 for more detail. By default, every value in the array is false and only // those values which are allowed in that particular set are then set to true. The numbers @@ -397,6 +385,12 @@ namespace System.Net.Mime return true; } + internal static bool IsAllowedWhiteSpace(char c) + { + // all allowed whitespace characters + return c == Tab || c == Space || c == CR || c == LF; + } + internal static bool HasCROrLF(string data) { for (int i = 0; i < data.Length; i++)