-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Revert "Remove Latin1CharSearchValues (#91884)" This reverts commit 4a09c82. * Keep the projitems formatting
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/libraries/System.Private.CoreLib/src/System/SearchValues/Latin1CharSearchValues.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class Latin1CharSearchValues : SearchValues<char> | ||
{ | ||
private readonly BitVector256 _lookup; | ||
|
||
public Latin1CharSearchValues(ReadOnlySpan<char> values) | ||
{ | ||
foreach (char c in values) | ||
{ | ||
if (c > 255) | ||
{ | ||
// The values were modified concurrent with the call to SearchValues.Create | ||
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); | ||
} | ||
|
||
_lookup.Set(c); | ||
} | ||
} | ||
|
||
internal override char[] GetValues() => _lookup.GetCharValues(); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
_lookup.Contains256(value); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
IndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(ref MemoryMarshal.GetReference(span), span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
IndexOfAny<IndexOfAnyAsciiSearcher.Negate>(ref MemoryMarshal.GetReference(span), span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
LastIndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(ref MemoryMarshal.GetReference(span), span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate>(ref MemoryMarshal.GetReference(span), span.Length); | ||
|
||
private int IndexOfAny<TNegator>(ref char searchSpace, int searchSpaceLength) | ||
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator | ||
{ | ||
ref char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength); | ||
ref char cur = ref searchSpace; | ||
|
||
while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd)) | ||
{ | ||
char c = cur; | ||
if (TNegator.NegateIfNeeded(_lookup.Contains256(c))) | ||
{ | ||
return (int)((nuint)Unsafe.ByteOffset(ref searchSpace, ref cur) / sizeof(char)); | ||
} | ||
|
||
cur = ref Unsafe.Add(ref cur, 1); | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private int LastIndexOfAny<TNegator>(ref char searchSpace, int searchSpaceLength) | ||
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator | ||
{ | ||
for (int i = searchSpaceLength - 1; i >= 0; i--) | ||
{ | ||
char c = Unsafe.Add(ref searchSpace, i); | ||
if (TNegator.NegateIfNeeded(_lookup.Contains256(c))) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters