aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-02-10 21:38:48 +0800
committerzeertzjq <zeertzjq@outlook.com>2024-02-10 21:55:57 +0800
commit00e785b17fde8c476031e3c24ea77bed45b88a89 (patch)
treeec1bde5294d6227a435c20979e32c7bfd4e74ec8
parentf3982ad3f3bc866c9369edfbd4125aaa092215a9 (diff)
downloadrneovim-00e785b17fde8c476031e3c24ea77bed45b88a89.tar.gz
rneovim-00e785b17fde8c476031e3c24ea77bed45b88a89.tar.bz2
rneovim-00e785b17fde8c476031e3c24ea77bed45b88a89.zip
refactor: don't use subtraction in qsort() comparison functions
-rw-r--r--src/nvim/channel.c7
-rw-r--r--src/nvim/decoration.c16
-rw-r--r--src/nvim/normal.c2
-rw-r--r--src/nvim/shada.c4
-rw-r--r--src/nvim/sign.c20
-rw-r--r--src/nvim/winfloat.c4
6 files changed, 36 insertions, 17 deletions
diff --git a/src/nvim/channel.c b/src/nvim/channel.c
index 05148801f4..1dc1208696 100644
--- a/src/nvim/channel.c
+++ b/src/nvim/channel.c
@@ -962,10 +962,11 @@ Dictionary channel_info(uint64_t id)
}
/// Simple int64_t comparison function for use with qsort()
-static int int64_t_cmp(const void *a, const void *b)
+static int int64_t_cmp(const void *pa, const void *pb)
{
- int64_t diff = *(int64_t *)a - *(int64_t *)b;
- return (diff < 0) ? -1 : (diff > 0);
+ const int64_t a = *(const int64_t *)pa;
+ const int64_t b = *(const int64_t *)pb;
+ return a == b ? 0 : a > b ? 1 : -1;
}
Array channel_all_info(void)
diff --git a/src/nvim/decoration.c b/src/nvim/decoration.c
index 9742a2020a..9767c44897 100644
--- a/src/nvim/decoration.c
+++ b/src/nvim/decoration.c
@@ -673,10 +673,20 @@ int sign_item_cmp(const void *p1, const void *p2)
{
const SignItem *s1 = (SignItem *)p1;
const SignItem *s2 = (SignItem *)p2;
- int n = s2->sh->priority - s1->sh->priority;
- return n ? n : (n = (int)(s2->id - s1->id))
- ? n : (s2->sh->sign_add_id - s1->sh->sign_add_id);
+ if (s1->sh->priority != s2->sh->priority) {
+ return s1->sh->priority < s2->sh->priority ? 1 : -1;
+ }
+
+ if (s1->id != s2->id) {
+ return s1->id < s2->id ? 1 : -1;
+ }
+
+ if (s1->sh->sign_add_id != s2->sh->sign_add_id) {
+ return s1->sh->sign_add_id > s2->sh->sign_add_id ? 1 : -1;
+ }
+
+ return 0;
}
static const uint32_t sign_filter[4] = {[kMTMetaSignText] = kMTFilterSelect,
diff --git a/src/nvim/normal.c b/src/nvim/normal.c
index 8b6ef62873..d69e43e6b3 100644
--- a/src/nvim/normal.c
+++ b/src/nvim/normal.c
@@ -375,7 +375,7 @@ static int nv_compare(const void *s1, const void *s2)
if (c2 < 0) {
c2 = -c2;
}
- return c1 - c2;
+ return c1 == c2 ? 0 : c1 > c2 ? 1 : -1;
}
/// Initialize the nv_cmd_idx[] table.
diff --git a/src/nvim/shada.c b/src/nvim/shada.c
index 61c43e271b..2c8685adc7 100644
--- a/src/nvim/shada.c
+++ b/src/nvim/shada.c
@@ -1875,9 +1875,7 @@ static int compare_file_marks(const void *a, const void *b)
const FileMarks *const *const b_fms = b;
return ((*a_fms)->greatest_timestamp == (*b_fms)->greatest_timestamp
? 0
- : ((*a_fms)->greatest_timestamp > (*b_fms)->greatest_timestamp
- ? -1
- : 1));
+ : ((*a_fms)->greatest_timestamp > (*b_fms)->greatest_timestamp ? -1 : 1));
}
/// Parse msgpack object that has given length
diff --git a/src/nvim/sign.c b/src/nvim/sign.c
index 9caacd8115..bd7979153e 100644
--- a/src/nvim/sign.c
+++ b/src/nvim/sign.c
@@ -172,20 +172,28 @@ int sign_cmp(const void *p1, const void *p2)
{
const MTKey *s1 = (MTKey *)p1;
const MTKey *s2 = (MTKey *)p2;
- int n = s1->pos.row - s2->pos.row;
- if (n) {
- return n;
+ if (s1->pos.row != s2->pos.row) {
+ return s1->pos.row > s2->pos.row ? 1 : -1;
}
DecorSignHighlight *sh1 = decor_find_sign(mt_decor(*s1));
DecorSignHighlight *sh2 = decor_find_sign(mt_decor(*s2));
assert(sh1 && sh2);
- n = sh2->priority - sh1->priority;
+ if (sh1->priority != sh2->priority) {
+ return sh1->priority < sh2->priority ? 1 : -1;
+ }
+
+ if (s1->id != s2->id) {
+ return s1->id < s2->id ? 1 : -1;
+ }
+
+ if (sh1->sign_add_id != sh2->sign_add_id) {
+ return sh1->sign_add_id < sh2->sign_add_id ? 1 : -1;
+ }
- return n ? n : (n = (int)(s2->id - s1->id))
- ? n : (sh2->sign_add_id - sh1->sign_add_id);
+ return 0;
}
/// Delete the specified sign(s)
diff --git a/src/nvim/winfloat.c b/src/nvim/winfloat.c
index f22c0f3cfa..1c9eb4ec5c 100644
--- a/src/nvim/winfloat.c
+++ b/src/nvim/winfloat.c
@@ -233,7 +233,9 @@ void win_config_float(win_T *wp, WinConfig fconfig)
static int float_zindex_cmp(const void *a, const void *b)
{
- return (*(win_T **)b)->w_float_config.zindex - (*(win_T **)a)->w_float_config.zindex;
+ int za = (*(win_T **)a)->w_float_config.zindex;
+ int zb = (*(win_T **)b)->w_float_config.zindex;
+ return za == zb ? 0 : za < zb ? 1 : -1;
}
void win_float_remove(bool bang, int count)