Skip to content

Commit 935fb60

Browse files
authored
Refactor indexer bounds checking (#357)
1 parent a8e30a5 commit 935fb60

File tree

8 files changed

+8
-21
lines changed

8 files changed

+8
-21
lines changed

NetFabric.Hyperlinq.UnitTests/Generation/ValueEnumerable/Return.Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Indexer_With_IndexOutOfRange_Must_Throw(int value, int index)
3333
Func<int> action = () => ValueEnumerable.Return(value)[index];
3434

3535
// Assert
36-
_ = action.Must().Throw<IndexOutOfRangeException>();
36+
_ = action.Must().Throw<ArgumentOutOfRangeException>();
3737
}
3838
}
3939
}

NetFabric.Hyperlinq/Conversion/AsValueEnumerable/AsValueEnumerable.ArraySegment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public readonly TSource this[int index]
3939
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4040
get
4141
{
42-
if (index < 0 || index >= Count) Throw.IndexOutOfRangeException();
42+
if ((uint)index >= (uint)Count) Throw.ArgumentOutOfRangeException<TSource>(nameof(index));
4343

4444
return source.Array![source.Offset + index];
4545
}

NetFabric.Hyperlinq/Generation/ValueEnumerable/Empty.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public int Count
2020
=> 0;
2121

2222
public TSource this[int index]
23-
=> Throw.IndexOutOfRangeException<TSource>();
23+
=> Throw.ArgumentOutOfRangeException<TSource>(nameof(index));
2424
TSource IList<TSource>.this[int index]
2525
{
26-
get => Throw.IndexOutOfRangeException<TSource>();
26+
get => Throw.ArgumentOutOfRangeException<TSource>(nameof(index));
2727

2828
[ExcludeFromCodeCoverage]
2929
// ReSharper disable once ValueParameterNotUsed

NetFabric.Hyperlinq/Generation/ValueEnumerable/Return.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public readonly TSource this[int index]
3333
get => index switch
3434
{
3535
0 => value,
36-
_ => Throw.IndexOutOfRangeException<TSource>()
36+
_ => Throw.ArgumentOutOfRangeException<TSource>(nameof(index))
3737
};
3838
}
3939
TSource IReadOnlyList<TSource>.this[int index]

NetFabric.Hyperlinq/NetFabric.Hyperlinq.csproj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net461;netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<PackageId>NetFabric.Hyperlinq</PackageId>
66
<Title>NetFabric.Hyperlinq</Title>
7-
<Description> High performance LINQ implementation with minimal heap allocations. Supports enumerables, async enumerables, arrays, Memory and Span.</Description>
7+
<Description> High performance LINQ implementation with minimal heap allocations. Supports enumerables, async enumerables, Memory, and Span.</Description>
88
<Version>3.0.0-beta44</Version>
99
<PackageIcon>Icon.png</PackageIcon>
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
@@ -81,9 +81,4 @@
8181
</EmbeddedResource>
8282
</ItemGroup>
8383

84-
<ItemGroup>
85-
<Folder Include="Conversion\AsAsyncEnumerable" />
86-
<Folder Include="Utils\Generator\" />
87-
</ItemGroup>
88-
8984
</Project>

NetFabric.Hyperlinq/Projection/Select/Select/Select.ArraySegment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public TResult this[int index]
4444
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4545
get
4646
{
47-
if (index < 0 || index >= Count) Throw.IndexOutOfRangeException();
47+
if ((uint)index >= (uint)Count) Throw.ArgumentOutOfRangeException<TSource>(nameof(index));
4848

4949
return selector.Invoke(source.Array![index + source.Offset]);
5050
}

NetFabric.Hyperlinq/Projection/Select/SelectAt/SelectAt.ArraySegment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public TResult this[int index]
4747
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4848
get
4949
{
50-
if (index < 0 || index >= Count) Throw.IndexOutOfRangeException();
50+
if ((uint)index >= (uint)Count) Throw.ArgumentOutOfRangeException<TSource>(nameof(index));
5151

5252
return selector.Invoke(source.Array![index + source.Offset], index);
5353
}

NetFabric.Hyperlinq/Utils/Throw.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ public static T ArgumentOutOfRangeException<T>(string? paramName)
3838
public static ref readonly T ArgumentOutOfRangeExceptionRef<T>(string? paramName)
3939
=> throw new ArgumentOutOfRangeException(paramName);
4040

41-
[DoesNotReturn]
42-
public static void IndexOutOfRangeException()
43-
=> throw new IndexOutOfRangeException();
44-
45-
[DoesNotReturn]
46-
public static T IndexOutOfRangeException<T>()
47-
=> throw new IndexOutOfRangeException();
48-
4941
[DoesNotReturn]
5042
public static void EmptySequence()
5143
=> throw new InvalidOperationException(Resource.EmptySequence);

0 commit comments

Comments
 (0)