aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/exess
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/exess')
-rw-r--r--subprojects/exess/include/exess/exess.h16
-rw-r--r--subprojects/exess/src/variant.c126
2 files changed, 139 insertions, 3 deletions
diff --git a/subprojects/exess/include/exess/exess.h b/subprojects/exess/include/exess/exess.h
index f327eb2e..13e9cf82 100644
--- a/subprojects/exess/include/exess/exess.h
+++ b/subprojects/exess/include/exess/exess.h
@@ -1415,6 +1415,11 @@ EXESS_PURE_API
const uint8_t* EXESS_NULLABLE
exess_get_ubyte(const ExessVariant* EXESS_NONNULL variant);
+/// Return a pointer to the value if `variant` is a date, otherwise null
+EXESS_PURE_API
+const ExessBlob* EXESS_NULLABLE
+exess_get_blob(const ExessVariant* EXESS_NONNULL variant);
+
/// Return a pointer to the value if `variant` is a duration, otherwise null
EXESS_PURE_API
const ExessDuration* EXESS_NULLABLE
@@ -1435,10 +1440,15 @@ EXESS_PURE_API
const ExessDate* EXESS_NULLABLE
exess_get_date(const ExessVariant* EXESS_NONNULL variant);
-/// Return a pointer to the value if `variant` is a date, otherwise null
+/**
+ @}
+ @defgroup exess_variant_coparison Comparison
+ @{
+*/
+
EXESS_PURE_API
-const ExessBlob* EXESS_NULLABLE
-exess_get_blob(const ExessVariant* EXESS_NONNULL variant);
+int
+exess_compare(ExessVariant lhs, ExessVariant rhs);
/**
@}
diff --git a/subprojects/exess/src/variant.c b/subprojects/exess/src/variant.c
index d8977abd..b32f27d6 100644
--- a/subprojects/exess/src/variant.c
+++ b/subprojects/exess/src/variant.c
@@ -275,6 +275,132 @@ exess_get_date(const ExessVariant* const variant)
return variant->datatype == EXESS_DATE ? &variant->value.as_date : NULL;
}
+// Comparison
+
+int
+exess_compare(const ExessVariant lhs, const ExessVariant rhs)
+{
+ if (lhs.datatype != rhs.datatype) {
+ return !lhs.datatype ? -1
+ : !rhs.datatype ? 1
+ : strcmp(exess_datatype_uri(lhs.datatype),
+ exess_datatype_uri(rhs.datatype));
+ }
+
+ switch (lhs.datatype) {
+ case EXESS_NOTHING:
+ return 0;
+
+ case EXESS_BOOLEAN:
+ return lhs.value.as_bool < rhs.value.as_bool ? -1
+ : lhs.value.as_bool > rhs.value.as_bool ? 1
+ : 0;
+
+ case EXESS_DECIMAL:
+ case EXESS_DOUBLE:
+ return lhs.value.as_double < rhs.value.as_double ? -1
+ : lhs.value.as_double > rhs.value.as_double ? 1
+ : 0;
+
+ case EXESS_FLOAT:
+ return lhs.value.as_float < rhs.value.as_float ? -1
+ : lhs.value.as_float > rhs.value.as_float ? 1
+ : 0;
+
+ case EXESS_INTEGER:
+ case EXESS_NON_POSITIVE_INTEGER:
+ case EXESS_NEGATIVE_INTEGER:
+ case EXESS_LONG:
+ return lhs.value.as_long < rhs.value.as_long ? -1
+ : lhs.value.as_long > rhs.value.as_long ? 1
+ : 0;
+
+ case EXESS_INT:
+ return lhs.value.as_int < rhs.value.as_int ? -1
+ : lhs.value.as_int > rhs.value.as_int ? 1
+ : 0;
+
+ case EXESS_SHORT:
+ return lhs.value.as_short < rhs.value.as_short ? -1
+ : lhs.value.as_short > rhs.value.as_short ? 1
+ : 0;
+
+ case EXESS_BYTE:
+ return lhs.value.as_byte < rhs.value.as_byte ? -1
+ : lhs.value.as_byte > rhs.value.as_byte ? 1
+ : 0;
+
+ case EXESS_NON_NEGATIVE_INTEGER:
+ case EXESS_ULONG:
+ return lhs.value.as_ulong < rhs.value.as_ulong ? -1
+ : lhs.value.as_ulong > rhs.value.as_ulong ? 1
+ : 0;
+
+ case EXESS_UINT:
+ return lhs.value.as_uint < rhs.value.as_uint ? -1
+ : lhs.value.as_uint > rhs.value.as_uint ? 1
+ : 0;
+
+ case EXESS_USHORT:
+ return lhs.value.as_ushort < rhs.value.as_ushort ? -1
+ : lhs.value.as_ushort > rhs.value.as_ushort ? 1
+ : 0;
+
+ case EXESS_UBYTE:
+ return lhs.value.as_ubyte < rhs.value.as_ubyte ? -1
+ : lhs.value.as_ubyte > rhs.value.as_ubyte ? 1
+ : 0;
+
+ case EXESS_POSITIVE_INTEGER:
+ return lhs.value.as_ulong < rhs.value.as_ulong ? -1
+ : lhs.value.as_ulong > rhs.value.as_ulong ? 1
+ : 0;
+
+ case EXESS_DURATION:
+ return lhs.value.as_duration.months < rhs.value.as_duration.months ? -1
+ : lhs.value.as_duration.months > rhs.value.as_duration.months ? 1
+ : lhs.value.as_duration.seconds < rhs.value.as_duration.seconds ? -1
+ : lhs.value.as_duration.seconds > rhs.value.as_duration.seconds ? 1
+ : lhs.value.as_duration.nanoseconds <
+ rhs.value.as_duration.nanoseconds
+ ? -1
+ : lhs.value.as_duration.nanoseconds >
+ rhs.value.as_duration.nanoseconds
+ ? 1
+ : 0;
+
+ case EXESS_DATETIME:
+
+ case EXESS_TIME:
+ case EXESS_DATE:
+ case EXESS_HEX:
+ case EXESS_BASE64:
+ break;
+ }
+
+ /* return exess_make_nothing(EXESS_UNSUPPORTED); */
+
+ /* if (lhs.datatype == EXESS_BOOL) { */
+ /* return lhs.value.as_long < rhs.value.as_long ? -1 */
+ /* : lhs.value.as_long > rhs.value.as_long ? 1 */
+ /* : 0; */
+ /* } */
+
+ /* if (lhs.datatype == EXESS_LONG) { */
+ /* return lhs.value.as_long < rhs.value.as_long ? -1 */
+ /* : lhs.value.as_long > rhs.value.as_long ? 1 */
+ /* : 0; */
+ /* } */
+
+ /* if (lhs.datatype == EXESS_ULONG) { */
+ /* return lhs.value.as_long < rhs.value.as_long ? -1 */
+ /* : lhs.value.as_long > rhs.value.as_long ? 1 */
+ /* : 0; */
+ /* } */
+
+ return 0;
+}
+
// Reading and Writing
ExessResult