From 86e8b97568e01768cedfe1e46970ae8c8ac97777 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Tue, 22 Nov 2022 03:45:17 +0100 Subject: [PATCH] Use IndexOfAnyValues in X500NameEncoder --- .../X509Certificates/X500NameEncoder.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) 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) {