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>
(cherry picked from commit 4f21f4038f)
r14.1.x
Mavridis Philippe 7 months ago
parent 950f6aa09f
commit 979ee566e7
No known key found for this signature in database
GPG Key ID: F8D2D7E2F989A494

@ -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