Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IndexOfAnyValues in X500NameEncoder #78676

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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