diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs index 5787996513129a..305795f7d2504f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs @@ -487,46 +487,23 @@ public char this[int index] { get { - StringBuilder? chunk = this; - while (true) + if ((uint)index >= (uint)Length) { - int indexInBlock = index - chunk.m_ChunkOffset; - if (indexInBlock >= 0) - { - if (indexInBlock >= chunk.m_ChunkLength) - { - throw new IndexOutOfRangeException(); - } - return chunk.m_ChunkChars[indexInBlock]; - } - chunk = chunk.m_ChunkPrevious; - if (chunk == null) - { - throw new IndexOutOfRangeException(); - } + ThrowHelper.ThrowIndexOutOfRangeException(); } + + StringBuilder chunk = FindChunkForIndex(index); + return chunk.m_ChunkChars[index - chunk.m_ChunkOffset]; } set { - StringBuilder? chunk = this; - while (true) + if ((uint)index >= (uint)Length) { - int indexInBlock = index - chunk.m_ChunkOffset; - if (indexInBlock >= 0) - { - if (indexInBlock >= chunk.m_ChunkLength) - { - throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess); - } - chunk.m_ChunkChars[indexInBlock] = value; - return; - } - chunk = chunk.m_ChunkPrevious; - if (chunk == null) - { - throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexMustBeLess); - } + ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessException(); } + + StringBuilder chunk = FindChunkForIndex(index); + chunk.m_ChunkChars[index - chunk.m_ChunkOffset] = value; } }