KSpread: Add natural comparison operators.

Natural comparison does not depend on the type of data (whether it's a string or a number).

Based on Calligra Sheets code originally from revision 551470.

Signed-off-by: Mavridis Philippe <mavridisf@gmail.com>
pull/66/head
Mavridis Philippe 8 months ago committed by TDE Gitea
parent c0bcd64f7f
commit 4f21f4038f

@ -433,9 +433,70 @@ bool ValueCalc::approxEqual (const Value &a, const Value &b)
return (x < 0.0 ? -x : x) < ((aa < 0.0 ? -aa : aa) * DBL_EPSILON);
}
bool ValueCalc::strEqual (const Value &a, const Value &b)
bool ValueCalc::strEqual (const Value &a, const Value &b, bool CS)
{
return (converter->asString (a).asString() == converter->asString (b).asString());
TQString aa = converter->asString(a).asString();
TQString bb = converter->asString(b).asString();
if (!CS) {
aa = aa.lower();
bb = bb.lower();
}
return (aa == bb);
}
bool ValueCalc::strGreater (const Value &a, const Value &b, bool CS)
{
TQString aa = converter->asString(a).asString();
TQString bb = converter->asString(b).asString();
if (!CS) {
aa = aa.lower();
bb = bb.lower();
}
return (aa > bb);
}
bool ValueCalc::strGequal (const Value &a, const Value &b, bool CS)
{
TQString aa = converter->asString(a).asString();
TQString bb = converter->asString(b).asString();
if (!CS) {
aa = aa.lower();
bb = bb.lower();
}
return (aa >= bb);
}
bool ValueCalc::strLower (const Value &a, const Value &b, bool CS)
{
return strGreater (b, a, CS);
}
bool ValueCalc::naturalEqual (const Value &a, const Value &b, bool CS)
{
if (a.isNumber() && b.isNumber())
return approxEqual(a, b);
if (a.allowComparison(b))
return a.equal(b);
return strEqual(a, b, CS);
}
bool ValueCalc::naturalGreater (const Value &a, const Value &b, bool CS)
{
if (a.isNumber() && b.isNumber())
return greater(a, b);
if (a.allowComparison(b))
return a.greater(b);
return strGreater(a, b, CS);
}
bool ValueCalc::naturalGequal (const Value &a, const Value &b, bool CS)
{
return naturalGreater(a, b, CS) || naturalEqual(a, b, CS);
}
bool ValueCalc::naturalLower (const Value &a, const Value &b, bool CS)
{
return naturalGreater(b, a, CS);
}
bool ValueCalc::greater (const Value &a, const Value &b)

@ -106,7 +106,22 @@ class ValueCalc {
/** numerical comparison */
bool lower (const Value &a, const Value &b);
/** string comparison */
bool strEqual (const Value &a, const Value &b);
bool strEqual (const Value &a, const Value &b, bool CS = true);
/** string comparison */
bool strGreater (const Value &a, const Value &b, bool CS = true);
/** string comparison - greater or equal */
bool strGequal (const Value &a, const Value &b, bool CS = true);
/** string comparison */
bool strLower (const Value &a, const Value &b, bool CS = true);
/** string or numerical comparison */
bool naturalEqual (const Value &a, const Value &b, bool CS = true);
/** string or numerical comparison */
bool naturalGreater (const Value &a, const Value &b, bool CS = true);
/** string or numerical comparison - greater or equal */
bool naturalGequal (const Value &a, const Value &b, bool CS = true);
/** string or numerical comparison */
bool naturalLower (const Value &a, const Value &b, bool CS = true);
int sign (const Value &a);
/** rounding */

Loading…
Cancel
Save