Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove IAsciiSet special cases
  • Loading branch information
MihaZupan committed Nov 19, 2022
commit c520667a9d121f8dd8a4f75952e6cb0f42fac761
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyAsciiByteValues.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyAsciiCharValues.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyAsciiSearcher.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyByteInAsciiSet.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyByteValues.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyCharInAsciiSet.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyCharValuesProbabilistic.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyLatin1CharValues.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOfAnyValues\IndexOfAnyValues.cs" />
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,6 @@ namespace System.Buffers
/// </remarks>
public static class IndexOfAnyValues
{
#if DEBUG
static IndexOfAnyValues()
{
CheckAsciiSet<AsciiLetter>("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
CheckAsciiSet<AsciiLetterOrDigit>("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
CheckAsciiSet<AsciiHexDigit>("0123456789ABCDEFabcdef");
CheckAsciiSet<AsciiHexDigitLower>("0123456789abcdef");
CheckAsciiSet<AsciiHexDigitUpper>("0123456789ABCDEF");
}

private static void CheckAsciiSet<TAsciiSet>(string set)
where TAsciiSet : IAsciiSet
{
byte[] byteSet = System.Text.Encoding.ASCII.GetBytes(set);

IndexOfAnyAsciiSearcher.ComputeBitmap<char>(set, out Vector128<byte> bitmap, out BitVector256 lookup);
Debug.Assert(bitmap == TAsciiSet.Bitmap);
Debug.Assert(lookup.GetCharValues().AsSpan().SequenceEqual(set));
Debug.Assert(lookup.GetByteValues().AsSpan().SequenceEqual(byteSet));

IndexOfAnyAsciiSearcher.ComputeBitmap<byte>(byteSet, out bitmap, out lookup);
Debug.Assert(bitmap == TAsciiSet.Bitmap);
Debug.Assert(lookup.GetCharValues().AsSpan().SequenceEqual(set));
Debug.Assert(lookup.GetByteValues().AsSpan().SequenceEqual(byteSet));

for (int i = 0; i < 256; i++)
{
Debug.Assert(TAsciiSet.Contains((char)i) == lookup.Contains256((char)i), $"{i}");
}

for (int i = 256; i <= char.MaxValue; i++)
{
Debug.Assert(!TAsciiSet.Contains((char)i), $"{i}");
}
}
#endif

/// <summary>
/// Creates an optimized representation of <paramref name="values"/> used for efficient searching.
/// </summary>
Expand Down Expand Up @@ -93,12 +56,6 @@ public static IndexOfAnyValues<byte> Create(ReadOnlySpan<byte> values)
{
IndexOfAnyAsciiSearcher.ComputeBitmap(values, out Vector128<byte> bitmap, out BitVector256 lookup);

if (bitmap == AsciiLetter.Bitmap) return IndexOfAnyByteInAsciiSet<AsciiLetter>.Instance;
if (bitmap == AsciiLetterOrDigit.Bitmap) return IndexOfAnyByteInAsciiSet<AsciiLetterOrDigit>.Instance;
if (bitmap == AsciiHexDigit.Bitmap) return IndexOfAnyByteInAsciiSet<AsciiHexDigit>.Instance;
if (bitmap == AsciiHexDigitLower.Bitmap) return IndexOfAnyByteInAsciiSet<AsciiHexDigitLower>.Instance;
if (bitmap == AsciiHexDigitUpper.Bitmap) return IndexOfAnyByteInAsciiSet<AsciiHexDigitUpper>.Instance;

return Sse3.IsSupported && lookup.Contains(0)
? new IndexOfAnyAsciiByteValues<IndexOfAnyAsciiSearcher.Ssse3HandleZeroInNeedle>(bitmap, lookup)
: new IndexOfAnyAsciiByteValues<IndexOfAnyAsciiSearcher.Default>(bitmap, lookup);
Expand Down Expand Up @@ -149,12 +106,6 @@ ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(values)),
{
IndexOfAnyAsciiSearcher.ComputeBitmap(values, out Vector128<byte> bitmap, out BitVector256 lookup);

if (bitmap == AsciiLetter.Bitmap) return IndexOfAnyCharInAsciiSet<AsciiLetter>.Instance;
if (bitmap == AsciiLetterOrDigit.Bitmap) return IndexOfAnyCharInAsciiSet<AsciiLetterOrDigit>.Instance;
if (bitmap == AsciiHexDigit.Bitmap) return IndexOfAnyCharInAsciiSet<AsciiHexDigit>.Instance;
if (bitmap == AsciiHexDigitLower.Bitmap) return IndexOfAnyCharInAsciiSet<AsciiHexDigitLower>.Instance;
if (bitmap == AsciiHexDigitUpper.Bitmap) return IndexOfAnyCharInAsciiSet<AsciiHexDigitUpper>.Instance;

return Sse3.IsSupported && lookup.Contains(0)
? new IndexOfAnyAsciiCharValues<IndexOfAnyAsciiSearcher.Ssse3HandleZeroInNeedle>(bitmap, lookup)
: new IndexOfAnyAsciiCharValues<IndexOfAnyAsciiSearcher.Default>(bitmap, lookup);
Expand Down Expand Up @@ -219,47 +170,6 @@ ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(values)),
return (IndexOfAnyValues<T>)(object)new IndexOfAnyValuesInRange<T>(min, max);
}

internal interface IAsciiSet
{
public static abstract Vector128<byte> Bitmap { get; }
public static abstract bool Contains(char c);
}

private readonly struct AsciiLetter : IAsciiSet
{
// IndexOfAnyAsciiSearcher.ComputeBitmap for "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
public static Vector128<byte> Bitmap => Vector128.Create(160, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 80, 80, 80, 80, 80);
public static bool Contains(char c) => char.IsAsciiLetter(c);
}

private readonly struct AsciiLetterOrDigit : IAsciiSet
{
// IndexOfAnyAsciiSearcher.ComputeBitmap for "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
public static Vector128<byte> Bitmap => Vector128.Create(168, 248, 248, 248, 248, 248, 248, 248, 248, 248, 240, 80, 80, 80, 80, 80);
public static bool Contains(char c) => char.IsAsciiLetterOrDigit(c);
}

private readonly struct AsciiHexDigit : IAsciiSet
{
// IndexOfAnyAsciiSearcher.ComputeBitmap for "0123456789ABCDEFabcdef"
public static Vector128<byte> Bitmap => Vector128.Create(8, 88, 88, 88, 88, 88, 88, 8, 8, 8, 0, 0, 0, 0, 0, 0).AsByte();
public static bool Contains(char c) => char.IsAsciiHexDigit(c);
}

private readonly struct AsciiHexDigitLower : IAsciiSet
{
// IndexOfAnyAsciiSearcher.ComputeBitmap for "0123456789abcdef"
public static Vector128<byte> Bitmap => Vector128.Create(8, 72, 72, 72, 72, 72, 72, 8, 8, 8, 0, 0, 0, 0, 0, 0).AsByte();
public static bool Contains(char c) => char.IsAsciiHexDigitLower(c);
}

private readonly struct AsciiHexDigitUpper : IAsciiSet
{
// IndexOfAnyAsciiSearcher.ComputeBitmap for "0123456789ABCDEF"
public static Vector128<byte> Bitmap => Vector128.Create(8, 24, 24, 24, 24, 24, 24, 8, 8, 8, 0, 0, 0, 0, 0, 0).AsByte();
public static bool Contains(char c) => char.IsAsciiHexDigitUpper(c);
}

internal interface IStringContains
{
public static abstract bool Contains(string s, char value);
Expand Down