diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X500NameEncoder.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X500NameEncoder.cs index 00fb503ad2550..fc85db32b294a 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X500NameEncoder.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X500NameEncoder.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.Formats.Asn1; @@ -16,6 +17,9 @@ internal static partial class X500NameEncoder private const string UseNewlineSeparators = "\r\n"; private const string DefaultSeparators = ",;"; + private static readonly IndexOfAnyValues s_needsQuotingChars = + IndexOfAnyValues.Create(",+=\"\n<>#;"); // \r is NOT in this list, because it isn't in Windows. + internal static string X500DistinguishedNameDecode( byte[] encodedName, bool printOid, @@ -117,18 +121,11 @@ internal static byte[] X500DistinguishedNameEncode( return writer.Encode(); } - private static bool NeedsQuoting(ReadOnlySpan rdnValue) - { - if (rdnValue.IsEmpty || - IsQuotableWhitespace(rdnValue[0]) || - IsQuotableWhitespace(rdnValue[^1])) - { - return true; - } - - const string QuoteNeedingChars = ",+=\"\n<>#;"; // \r is NOT in this list, because it isn't in Windows. - return rdnValue.IndexOfAny(QuoteNeedingChars) >= 0; - } + private static bool NeedsQuoting(ReadOnlySpan rdnValue) => + rdnValue.IsEmpty || + IsQuotableWhitespace(rdnValue[0]) || + IsQuotableWhitespace(rdnValue[^1]) || + rdnValue.IndexOfAny(s_needsQuotingChars) >= 0; private static bool IsQuotableWhitespace(char c) {