Skip to content

Commit b7e361a

Browse files
authored
Merge pull request #20 from rameel/simplify-bounds-check
Simplify bounds checks in ArrayView<T> and StringView Slice methods
2 parents a093b2b + 29a3bdc commit b7e361a

2 files changed

Lines changed: 11 additions & 40 deletions

File tree

Ramstack.Structures/Collections/ArrayView`1.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,11 @@ public ArrayView(T[] array, int index)
8181
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8282
public ArrayView(T[] array, int index, int length)
8383
{
84-
if (IntPtr.Size == 8)
85-
{
86-
if ((ulong)(uint)index + (uint)length > (uint)array.Length)
87-
ThrowHelper.ThrowArgumentOutOfRangeException();
88-
}
89-
else
90-
{
91-
if ((uint)index > (uint)array.Length || (uint)length > (uint)(array.Length - index))
92-
ThrowHelper.ThrowArgumentOutOfRangeException();
93-
}
84+
//
85+
// https://github.com/dotnet/runtime/issues/119689
86+
//
87+
if ((uint)index > (uint)array.Length || (uint)length > (uint)(array.Length - index))
88+
ThrowHelper.ThrowArgumentOutOfRangeException();
9489

9590
_index = index;
9691
_count = length;
@@ -152,16 +147,8 @@ public ArrayView<T> Slice(int index)
152147
[MethodImpl(MethodImplOptions.AggressiveInlining)]
153148
public ArrayView<T> Slice(int index, int count)
154149
{
155-
if (IntPtr.Size == 8)
156-
{
157-
if ((ulong)(uint)index + (uint)count > (uint)_count)
158-
ThrowHelper.ThrowArgumentOutOfRangeException();
159-
}
160-
else
161-
{
162-
if ((uint)index > (uint)_count || (uint)count > (uint)(_count - index))
163-
ThrowHelper.ThrowArgumentOutOfRangeException();
164-
}
150+
if ((uint)index > (uint)_count || (uint)count > (uint)(_count - index))
151+
ThrowHelper.ThrowArgumentOutOfRangeException();
165152

166153
return new ArrayView<T>(_array!, _index + index, count, unused: 0);
167154
}

Ramstack.Structures/Text/StringView.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,8 @@ public StringView(string value, int index)
7676
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7777
public StringView(string value, int index, int length)
7878
{
79-
if (IntPtr.Size == 8)
80-
{
81-
if ((ulong)(uint)index + (uint)length > (uint)value.Length)
82-
ThrowHelper.ThrowArgumentOutOfRangeException();
83-
}
84-
else
85-
{
86-
if ((uint)index > (uint)value.Length || (uint)length > (uint)(value.Length - index))
87-
ThrowHelper.ThrowArgumentOutOfRangeException();
88-
}
79+
if ((uint)index > (uint)value.Length || (uint)length > (uint)(value.Length - index))
80+
ThrowHelper.ThrowArgumentOutOfRangeException();
8981

9082
_index = index;
9183
_length = length;
@@ -177,16 +169,8 @@ public StringView Slice(int start)
177169
[MethodImpl(MethodImplOptions.AggressiveInlining)]
178170
public StringView Slice(int start, int length)
179171
{
180-
if (IntPtr.Size == 8)
181-
{
182-
if ((ulong)(uint)start + (uint)length > (uint)_length)
183-
ThrowHelper.ThrowArgumentOutOfRangeException();
184-
}
185-
else
186-
{
187-
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
188-
ThrowHelper.ThrowArgumentOutOfRangeException();
189-
}
172+
if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
173+
ThrowHelper.ThrowArgumentOutOfRangeException();
190174

191175
return new StringView(_value!, _index + start, length, unused: 0);
192176
}

0 commit comments

Comments
 (0)