Skip to content

Commit 6f201a8

Browse files
committed
Unify (simplify) bounds checks in ArrayView<T> and StringView Slice methods
1 parent a093b2b commit 6f201a8

2 files changed

Lines changed: 8 additions & 40 deletions

File tree

Ramstack.Structures/Collections/ArrayView`1.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,8 @@ 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+
if ((uint)index > (uint)array.Length || (uint)length > (uint)(array.Length - index))
85+
ThrowHelper.ThrowArgumentOutOfRangeException();
9486

9587
_index = index;
9688
_count = length;
@@ -152,16 +144,8 @@ public ArrayView<T> Slice(int index)
152144
[MethodImpl(MethodImplOptions.AggressiveInlining)]
153145
public ArrayView<T> Slice(int index, int count)
154146
{
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-
}
147+
if ((uint)index > (uint)_count || (uint)count > (uint)(_count - index))
148+
ThrowHelper.ThrowArgumentOutOfRangeException();
165149

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

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)