public string ToString(bool ignorePasswordChar, bool includePrompt, bool includeLiterals, int startPosition, int length)
{
if (length <= 0)
{
return string.Empty;
}
if (startPosition < 0)
{
startPosition = 0;
//throw new ArgumentOutOfRangeException("startPosition");
}
if (startPosition >= _testString.Length)
{
return string.Empty;
//throw new ArgumentOutOfRangeException("startPosition");
}
int maxLength = _testString.Length - startPosition;
if (length > maxLength)
{
length = maxLength;
//throw new ArgumentOutOfRangeException("length");
}
if (!IsPassword || ignorePasswordChar) // we may not need to format the text...
{
if (includePrompt && includeLiterals)
{
return _testString.ToString(startPosition, length); // testString contains just what the user is asking for.
}
}
// Build the formatted string ...
StringBuilder st = new StringBuilder();
int lastPosition = startPosition + length - 1;
if (!includePrompt)
{
// If prompt is not to be included we need to replace it with a space, but only for unassigned postions below
// the last assigned position or last literal position if including literals, whichever is higher; upper unassigned
// positions are not included in the resulting string.
int lastLiteralPos = includeLiterals ? FindNonEditPositionInRange(startPosition, lastPosition, backward) : InvalidIndex;
int lastAssignedPos = FindAssignedEditPositionInRange(lastLiteralPos == InvalidIndex ? startPosition : lastLiteralPos, lastPosition, backward);
// If lastLiteralPos is in the range and lastAssignedPos is not InvalidIndex, the lastAssignedPos is the upper limit
// we are looking for since it is searched in the range from lastLiteralPos and lastPosition. In any other case
// lastLiteral would contain the upper position we are looking for or InvalidIndex, meaning all characters in the
// range are to be ignored, in this case a null string should be returned.
lastPosition = lastAssignedPos != InvalidIndex ? lastAssignedPos : lastLiteralPos;
if (lastPosition == InvalidIndex)
{
return string.Empty;
}
}
for (int index = startPosition; index <= lastPosition; index++)
{
char ch = _testString[index];
CharDescriptor chDex = _stringDescriptor[index];
switch (chDex.CharType)
{
case CharType.EditOptional:
case CharType.EditRequired:
if (chDex.IsAssigned)
{
if (IsPassword && !ignorePasswordChar)
{
st.Append(_passwordChar); // replace edit char with pwd char.
break;
}
}
else
{
if (!includePrompt)
{
st.Append(spaceChar); // replace prompt with space.
break;
}
}
goto default;
case CharType.Separator:
case CharType.Literal:
if (!includeLiterals)
{
break; // exclude literals.
}
goto default;
default:
st.Append(ch);
break;
}
}
return st.ToString();
}