Pchp.Library.PhpMath.AbsolutizeRange C# (CSharp) Method

AbsolutizeRange() static private method

Absolutizes range specified by an offset and a length relatively to a dimension of an array.
Ensures that [offset,offset + length] is subrange of [0,count].
static private AbsolutizeRange ( int &offset, int &length, int count ) : void
offset int /// The offset of the range relative to the beginning (if non-negative) or the end of the array (if negative). /// If the offset underflows or overflows the length is shortened appropriately. ///
length int /// The length of the range if non-negative. Otherwise, its absolute value is the number of items /// which will not be included in the range from the end of the array. In the latter case /// the range ends with the ||-th item from the end of the array (counting from zero). ///
count int The number of items in array. Should be non-negative.
return void
        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);
        }