System.ComponentModel.MaskedTextProvider.FindPositionInRange C# (CSharp) Method

FindPositionInRange() private method

Finds a position in the test string according to the needed position type (needEditPos). The positions are relative to the test string. Returns InvalidIndex if it doesn't find one.
private FindPositionInRange ( int startPosition, int endPosition, bool direction, CharType charTypeFlags ) : int
startPosition int
endPosition int
direction bool
charTypeFlags CharType
return int
        private int FindPositionInRange(int startPosition, int endPosition, bool direction, CharType charTypeFlags)
        {
            if (startPosition < 0)
            {
                startPosition = 0;
            }

            if (endPosition >= _testString.Length)
            {
                endPosition = _testString.Length - 1;
            }

            if (startPosition > endPosition)
            {
                return invalidIndex;
            }

            // Iterate through the test string until we find an edit char position.
            int testPosition;

            while (startPosition <= endPosition)
            {
                testPosition = (direction == forward) ? startPosition++ : endPosition--;

                CharDescriptor chDex = _stringDescriptor[testPosition];

                if ((chDex.CharType & charTypeFlags) == chDex.CharType)
                {
                    return testPosition;
                }
            }

            return invalidIndex;
        }