internal static void AbsolutizeRange(ref int offset, ref int length, int count)
{
Debug.Assert(count >= 0);
// prevents overflows:
if (offset >= count || count == 0)
{
offset = count;
length = 0;
return;
}
// negative offset => offset is relative to the end of the string:
if (offset < 0)
{
offset += count;
if (offset < 0) offset = 0;
}
Debug.Assert(offset >= 0 && offset < count);
if (length < 0)
{
// there is count-offset items from offset to the end of array,
// the last |length| items is taken away:
length = count - offset + length;
if (length < 0) length = 0;
}
else if ((long)offset + length > count)
{
// interval ends on the end of array:
length = count - offset;
}
Debug.Assert(length >= 0 && offset + length <= count);
}