-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
884 additions
and
89 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
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
49 changes: 49 additions & 0 deletions
49
src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAny1CharValue.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,49 @@ | ||
// 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 IndexOfAny1CharValue<TShouldUsePacked> : IndexOfAnyValues<char> | ||
where TShouldUsePacked : struct, IndexOfAnyValues.IRuntimeConst | ||
{ | ||
private char _e0; | ||
|
||
public IndexOfAny1CharValue(char value) => | ||
_e0 = value; | ||
|
||
internal override char[] GetValues() => new[] { _e0 }; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value == _e0; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? SpanHelpers.PackedIndexOf(ref MemoryMarshal.GetReference(span), _e0, span.Length) | ||
: SpanHelpers.NonPackedIndexOfValueType<short, SpanHelpers.DontNegate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? SpanHelpers.PackedIndexOfAnyExcept(ref MemoryMarshal.GetReference(span), _e0, span.Length) | ||
: SpanHelpers.NonPackedIndexOfValueType<short, SpanHelpers.Negate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
span.LastIndexOf(_e0); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyExcept(_e0); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAny2CharValues.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,51 @@ | ||
// 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 IndexOfAny2CharValue<TShouldUsePacked> : IndexOfAnyValues<char> | ||
where TShouldUsePacked : struct, IndexOfAnyValues.IRuntimeConst | ||
{ | ||
private char _e0, _e1; | ||
|
||
public IndexOfAny2CharValue(char value0, char value1) => | ||
(_e0, _e1) = (value0, value1); | ||
|
||
internal override char[] GetValues() => new[] { _e0 }; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value == _e0 || value == _e1; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? SpanHelpers.PackedIndexOfAny(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyValueType<short, SpanHelpers.DontNegate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
Unsafe.As<char, short>(ref _e1), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? SpanHelpers.PackedIndexOfAnyExcept(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyValueType<short, SpanHelpers.Negate<short>>( | ||
ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, short>(ref _e0), | ||
Unsafe.As<char, short>(ref _e1), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAny(_e0, _e1); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyExcept(_e0, _e1); | ||
} | ||
} |
35 changes: 15 additions & 20 deletions
35
...dexOfAnyValues/IndexOfAnyValuesInRange.cs → ...fAnyValues/IndexOfAnyByteValuesInRange.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 |
---|---|---|
@@ -1,58 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Buffers | ||
{ | ||
internal sealed class IndexOfAnyValuesInRange<T> : IndexOfAnyValues<T> | ||
where T : struct, INumber<T> | ||
internal sealed class IndexOfAnyByteValuesInRange : IndexOfAnyValues<byte> | ||
{ | ||
private readonly T _lowInclusive, _highInclusive; | ||
private readonly byte _lowInclusive, _highInclusive; | ||
private readonly uint _lowUint, _highMinusLow; | ||
|
||
public IndexOfAnyValuesInRange(T lowInclusive, T highInclusive) | ||
public IndexOfAnyByteValuesInRange(byte lowInclusive, byte highInclusive) | ||
{ | ||
Debug.Assert(lowInclusive is byte or char); | ||
(_lowInclusive, _highInclusive) = (lowInclusive, highInclusive); | ||
_lowUint = uint.CreateChecked(lowInclusive); | ||
_highMinusLow = uint.CreateChecked(highInclusive - lowInclusive); | ||
_lowUint = lowInclusive; | ||
_highMinusLow = (uint)(highInclusive - lowInclusive); | ||
} | ||
|
||
internal override T[] GetValues() | ||
internal override byte[] GetValues() | ||
{ | ||
T[] values = new T[_highMinusLow + 1]; | ||
byte[] values = new byte[_highMinusLow + 1]; | ||
|
||
T element = _lowInclusive; | ||
int low = _lowInclusive; | ||
for (int i = 0; i < values.Length; i++) | ||
{ | ||
values[i] = element; | ||
element += T.One; | ||
values[i] = (byte)(low + i); | ||
} | ||
|
||
return values; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(T value) => | ||
uint.CreateChecked(value) - _lowUint <= _highMinusLow; | ||
internal override bool ContainsCore(byte value) => | ||
value - _lowUint <= _highMinusLow; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<T> span) => | ||
internal override int IndexOfAny(ReadOnlySpan<byte> span) => | ||
span.IndexOfAnyInRange(_lowInclusive, _highInclusive); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<T> span) => | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<byte> span) => | ||
span.IndexOfAnyExceptInRange(_lowInclusive, _highInclusive); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<T> span) => | ||
internal override int LastIndexOfAny(ReadOnlySpan<byte> span) => | ||
span.LastIndexOfAnyInRange(_lowInclusive, _highInclusive); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<T> span) => | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<byte> span) => | ||
span.LastIndexOfAnyExceptInRange(_lowInclusive, _highInclusive); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...braries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAnyCharValuesInRange.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,67 @@ | ||
// 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 IndexOfAnyCharValuesInRange<TShouldUsePacked> : IndexOfAnyValues<char> | ||
where TShouldUsePacked : struct, IndexOfAnyValues.IRuntimeConst | ||
{ | ||
private char _lowInclusive, _rangeInclusive, _highInclusive; | ||
private readonly uint _lowUint, _highMinusLow; | ||
|
||
public IndexOfAnyCharValuesInRange(char lowInclusive, char highInclusive) | ||
{ | ||
(_lowInclusive, _rangeInclusive, _highInclusive) = (lowInclusive, (char)(highInclusive - lowInclusive), highInclusive); | ||
_lowUint = lowInclusive; | ||
_highMinusLow = (uint)(highInclusive - lowInclusive); | ||
} | ||
|
||
internal override char[] GetValues() | ||
{ | ||
char[] values = new char[_rangeInclusive + 1]; | ||
|
||
int low = _lowInclusive; | ||
for (int i = 0; i < values.Length; i++) | ||
{ | ||
values[i] = (char)(low + i); | ||
} | ||
|
||
return values; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override bool ContainsCore(char value) => | ||
value - _lowUint <= _highMinusLow; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAny(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? SpanHelpers.PackedIndexOfAnyInRange(ref MemoryMarshal.GetReference(span), _lowInclusive, _rangeInclusive, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyInRangeUnsignedNumber<ushort, SpanHelpers.DontNegate<ushort>>( | ||
ref Unsafe.As<char, ushort>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, ushort>(ref _lowInclusive), | ||
Unsafe.As<char, ushort>(ref _highInclusive), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
TShouldUsePacked.Value | ||
? SpanHelpers.PackedIndexOfAnyExceptInRange(ref MemoryMarshal.GetReference(span), _lowInclusive, _rangeInclusive, span.Length) | ||
: SpanHelpers.NonPackedIndexOfAnyInRangeUnsignedNumber<ushort, SpanHelpers.Negate<ushort>>( | ||
ref Unsafe.As<char, ushort>(ref MemoryMarshal.GetReference(span)), | ||
Unsafe.As<char, ushort>(ref _lowInclusive), | ||
Unsafe.As<char, ushort>(ref _highInclusive), | ||
span.Length); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAny(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyInRange(_lowInclusive, _highInclusive); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => | ||
span.LastIndexOfAnyExceptInRange(_lowInclusive, _highInclusive); | ||
} | ||
} |
Oops, something went wrong.