aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builtins/c_string.c6
-rw-r--r--builtins/functions.c4
-rw-r--r--builtins/integers.c4
-rw-r--r--builtins/range.c2
-rw-r--r--builtins/table.c4
-rw-r--r--builtins/text.c4
6 files changed, 21 insertions, 3 deletions
diff --git a/builtins/c_string.c b/builtins/c_string.c
index bd35b549..47ea4858 100644
--- a/builtins/c_string.c
+++ b/builtins/c_string.c
@@ -21,10 +21,14 @@ public Text_t CString$as_text(const void *c_string, bool colorize, const TypeInf
return Text$concat(colorize ? Text("\x1b[34mCString\x1b[m(") : Text("CString("), Text$quoted(text, colorize), Text(")"));
}
-public int CString$compare(const char **x, const char **y)
+public int32_t CString$compare(const char **x, const char **y)
{
+ if (x == y)
+ return 0;
+
if (!*x != !*y)
return (!*y) - (!*x);
+
return strcmp(*x, *y);
}
diff --git a/builtins/functions.c b/builtins/functions.c
index b6132494..c11dd7e6 100644
--- a/builtins/functions.c
+++ b/builtins/functions.c
@@ -122,6 +122,8 @@ public uint64_t generic_hash(const void *obj, const TypeInfo *type)
public int32_t generic_compare(const void *x, const void *y, const TypeInfo *type)
{
+ if (x == y) return 0;
+
switch (type->tag) {
case PointerInfo: case FunctionInfo: return Pointer$compare(x, y, type);
case TextInfo: return Text$compare(x, y);
@@ -141,6 +143,8 @@ public int32_t generic_compare(const void *x, const void *y, const TypeInfo *typ
public bool generic_equal(const void *x, const void *y, const TypeInfo *type)
{
+ if (x == y) return true;
+
switch (type->tag) {
case PointerInfo: case FunctionInfo: return Pointer$equal(x, y, type);
case TextInfo: return Text$equal(x, y);
diff --git a/builtins/integers.c b/builtins/integers.c
index d9361cf4..a147df72 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -40,13 +40,13 @@ public Text_t Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type) {
public int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type) {
(void)type;
if (__builtin_expect(((x->small | y->small) & 1) == 0, 0))
- return mpz_cmp(*x->big, *y->big);
+ return x->big == y->big ? 0 : mpz_cmp(*x->big, *y->big);
return (x->small > y->small) - (x->small < y->small);
}
public int32_t Int$compare_value(const Int_t x, const Int_t y) {
if (__builtin_expect(((x.small | y.small) & 1) == 0, 0))
- return mpz_cmp(*x.big, *y.big);
+ return x.big == y.big ? 0 : mpz_cmp(*x.big, *y.big);
return (x.small > y.small) - (x.small < y.small);
}
diff --git a/builtins/range.c b/builtins/range.c
index d170e6b7..068d8cea 100644
--- a/builtins/range.c
+++ b/builtins/range.c
@@ -18,6 +18,7 @@
static int32_t Range$compare(const Range_t *x, const Range_t *y, const TypeInfo *type)
{
(void)type;
+ if (x == y) return 0;
int32_t diff = Int$compare(&x->first, &y->first, &$Int);
if (diff != 0) return diff;
diff = Int$compare(&x->last, &y->last, &$Int);
@@ -28,6 +29,7 @@ static int32_t Range$compare(const Range_t *x, const Range_t *y, const TypeInfo
static bool Range$equal(const Range_t *x, const Range_t *y, const TypeInfo *type)
{
(void)type;
+ if (x == y) return true;
return Int$equal(&x->first, &y->first, &$Int) && Int$equal(&x->last, &y->last, &$Int) && Int$equal(&x->step, &y->step, &$Int);
}
diff --git a/builtins/table.c b/builtins/table.c
index 22797775..b1d82c3c 100644
--- a/builtins/table.c
+++ b/builtins/table.c
@@ -395,6 +395,8 @@ public table_t Table$sorted(table_t t, const TypeInfo *type)
public bool Table$equal(const table_t *x, const table_t *y, const TypeInfo *type)
{
+ if (x == y) return true;
+
assert(type->tag == TableInfo);
if (Table$length(*x) != Table$length(*y))
return false;
@@ -407,6 +409,8 @@ public bool Table$equal(const table_t *x, const table_t *y, const TypeInfo *type
public int32_t Table$compare(const table_t *x, const table_t *y, const TypeInfo *type)
{
+ if (x == y) return 0;
+
assert(type->tag == TableInfo);
auto table = type->TableInfo;
if (x->entries.length == 0)
diff --git a/builtins/text.c b/builtins/text.c
index 1871fb1c..5e87027b 100644
--- a/builtins/text.c
+++ b/builtins/text.c
@@ -864,6 +864,8 @@ int32_t get_grapheme(Text_t text, int64_t index)
public int32_t Text$compare(const Text_t *a, const Text_t *b)
{
+ if (a == b) return 0;
+
int64_t len = MAX(a->length, b->length);
iteration_state_t a_state = {0, 0}, b_state = {0, 0};
for (int64_t i = 0; i < len; i++) {
@@ -897,6 +899,8 @@ public int32_t Text$compare(const Text_t *a, const Text_t *b)
public bool Text$equal(const Text_t *a, const Text_t *b)
{
+ if (a == b) return true;
+
if (a->length != b->length || (a->hash != 0 && b->hash != 0 && a->hash != b->hash))
return false;
int64_t len = a->length;