private bool InsertAtInt(string input, int position, out int testPosition, out MaskedTextResultHint resultHint, bool testOnly)
{
Debug.Assert(input != null && position >= 0 && position < _testString.Length, "input param out of range.");
if (input.Length == 0) // nothing to insert.
{
testPosition = position;
resultHint = MaskedTextResultHint.NoEffect;
return true;
}
// Test input string first. testPosition will containt the position of the last inserting character from the input.
if (!TestString(input, position, out testPosition, out resultHint))
{
return false;
}
// Now check if we need to open room for the input characters (shift characters right) and if so test the shifting characters.
int srcPos = FindEditPositionFrom(position, forward); // source position.
bool shiftNeeded = FindAssignedEditPositionInRange(srcPos, testPosition, forward) != invalidIndex;
int lastAssignedPos = LastAssignedPosition;
if (shiftNeeded && (testPosition == _testString.Length - 1)) // no room for shifting.
{
resultHint = MaskedTextResultHint.UnavailableEditPosition;
testPosition = _testString.Length;
return false;
}
int dstPos = FindEditPositionFrom(testPosition + 1, forward); // destination position.
if (shiftNeeded)
{
// Temp hint used not to overwrite the primary operation result hint (from TestString).
MaskedTextResultHint tempHint = MaskedTextResultHint.Unknown;
// Test shifting characters.
while (true)
{
if (dstPos == invalidIndex)
{
resultHint = MaskedTextResultHint.UnavailableEditPosition;
testPosition = _testString.Length;
return false;
}
CharDescriptor chDex = _stringDescriptor[srcPos];
if (chDex.IsAssigned) // only test assigned positions.
{
if (!TestChar(_testString[srcPos], dstPos, out tempHint))
{
resultHint = tempHint;
testPosition = dstPos;
return false;
}
}
if (srcPos == lastAssignedPos) // all shifting positions tested?
{
break;
}
srcPos = FindEditPositionFrom(srcPos + 1, forward);
dstPos = FindEditPositionFrom(dstPos + 1, forward);
}
if (tempHint > resultHint)
{
resultHint = tempHint;
}
}
if (testOnly)
{
return true; // test done!
}
// Tests passed so we can go ahead and shift the existing characters (if needed) and insert the new ones.
if (shiftNeeded)
{
while (srcPos >= position)
{
CharDescriptor chDex = _stringDescriptor[srcPos];
if (chDex.IsAssigned)
{
SetChar(_testString[srcPos], dstPos);
}
else
{
ResetChar(dstPos);
}
dstPos = FindEditPositionFrom(dstPos - 1, backward);
srcPos = FindEditPositionFrom(srcPos - 1, backward);
}
}
// Finally set the input characters.
SetString(input, position);
return true;
}