Skip to content

Commit

Permalink
Use IndexOfAnyValues in X500NameEncoder (#78676)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Nov 22, 2022
1 parent 8902a95 commit 0b3f44e
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +17,9 @@ internal static partial class X500NameEncoder
private const string UseNewlineSeparators = "\r\n";
private const string DefaultSeparators = ",;";

private static readonly IndexOfAnyValues<char> 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,
Expand Down Expand Up @@ -117,18 +121,11 @@ internal static byte[] X500DistinguishedNameEncode(
return writer.Encode();
}

private static bool NeedsQuoting(ReadOnlySpan<char> 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<char> rdnValue) =>
rdnValue.IsEmpty ||
IsQuotableWhitespace(rdnValue[0]) ||
IsQuotableWhitespace(rdnValue[^1]) ||
rdnValue.IndexOfAny(s_needsQuotingChars) >= 0;

private static bool IsQuotableWhitespace(char c)
{
Expand Down

0 comments on commit 0b3f44e

Please sign in to comment.