Tighten assert check in EllipsizeCalculator

Verify that we have exactly as many offsets as expected, both in the
case when the string terminates with a (lone) ampersand and when it
doesn't, instead of checking that it's one or the other without caring
in which case we're.

No real changes, but this seems slightly cleaner and avoids questions
about the expression "s.length() - 1" when the string is empty.
This commit is contained in:
Vadim Zeitlin
2019-11-13 18:38:05 +01:00
parent aa1891e524
commit cbda47ff15

View File

@@ -293,6 +293,8 @@ struct EllipsizeCalculator
m_maxFinalWidthPx(maxFinalWidthPx),
m_replacementWidthPx(replacementWidthPx)
{
size_t expectedOffsetsCount = s.length();
// Where ampersands are used as mnemonic indicator they should not
// affect the overall width of the string and must be removed from the
// measurement. Nonetheless, we need to keep them in the string and
@@ -315,13 +317,25 @@ struct EllipsizeCalculator
it != s.end();
++it, ++n )
{
if ( *it == '&' && !lastWasMnemonic && (it + 1) != s.end() )
if ( *it == '&' && !lastWasMnemonic )
{
int w = m_charOffsetsPx[n];
m_charOffsetsPx.Insert(w, n);
lastWasMnemonic = true;
if ( (it + 1) != s.end() )
{
int w = m_charOffsetsPx[n];
m_charOffsetsPx.Insert(w, n);
lastWasMnemonic = true;
}
else // Last character is an ampersand.
{
// This ampersand is removed by RemoveMnemonics() and
// won't be displayed when this string is drawn
// neither, so we intentionally don't use it for our
// calculations neither -- just account for this in the
// assert below.
expectedOffsetsCount--;
}
}
else
else // Not an ampersand used to introduce a mnemonic.
{
lastWasMnemonic = false;
}
@@ -333,10 +347,8 @@ struct EllipsizeCalculator
}
// Either way, we should end up with the same number of offsets as
// characters in the original string, except where there is a trailing
// ampersand which is ignored.
wxASSERT( m_charOffsetsPx.GetCount() == s.length() ||
m_charOffsetsPx.GetCount() == s.length() - 1);
// characters in the original string.
wxASSERT( m_charOffsetsPx.GetCount() == expectedOffsetsCount );
}
bool IsOk() const { return m_isOk; }