]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: simplify comparisons
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 18 May 2022 02:17:12 +0000 (19:17 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 18 May 2022 02:30:55 +0000 (19:30 -0700)
* src/comm.c (compare_files):
* src/join.c (keycmp):
* src/ls.c (off_cmp):
* src/ptx.c (compare_words, compare_occurs):
* src/set-fields.c (compare_ranges):
Prefer ((a > b) - (a < b)) to variants like (a < b ? -1 : a > b)
as it’s typically faster these days.

src/comm.c
src/join.c
src/ls.c
src/ptx.c
src/set-fields.c

index 947463638fe035ab6d5af6f7f4881a2fe4931b55..721139cb8d071fedd8eba3da962c5b8b5f12a349 100644 (file)
@@ -321,9 +321,8 @@ compare_files (char **infiles)
               size_t len = min (thisline[0]->length, thisline[1]->length) - 1;
               order = memcmp (thisline[0]->buffer, thisline[1]->buffer, len);
               if (order == 0)
-                order = (thisline[0]->length < thisline[1]->length
-                         ? -1
-                         : thisline[0]->length != thisline[1]->length);
+                order = ((thisline[0]->length > thisline[1]->length)
+                         - (thisline[0]->length < thisline[1]->length));
             }
         }
 
index f2fd1727b3f3945c12a70f4a2bf648349f0f9c86..fde4634f0738e811d2a6c9ddc9a77e4ab51bf361 100644 (file)
@@ -380,7 +380,7 @@ keycmp (struct line const *line1, struct line const *line2,
 
   if (diff)
     return diff;
-  return len1 < len2 ? -1 : len1 != len2;
+  return (len1 > len2) - (len1 < len2);
 }
 
 /* Check that successive input lines PREV and CURRENT from input file
index d15a10367720c45baa8be0a7891513bb125ff997..d48892be7cb20a7419aec92ac8b678fd28373d49 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -3881,7 +3881,7 @@ cmp_btime (struct fileinfo const *a, struct fileinfo const *b,
 static int
 off_cmp (off_t a, off_t b)
 {
-  return a < b ? -1 : a > b;
+  return (a > b) - (a < b);
 }
 
 static int
index 09b54447df713af71606b420335554cddb28db36..d7decc2ace0c2494df27d16a21d57d71dd54eb93 100644 (file)
--- a/src/ptx.c
+++ b/src/ptx.c
@@ -567,7 +567,7 @@ compare_words (const void *void_first, const void *void_second)
         }
     }
 
-  return first->size < second->size ? -1 : first->size > second->size;
+  return (first->size > second->size) - (first->size < second->size);
 #undef first
 #undef second
 }
@@ -587,8 +587,8 @@ compare_occurs (const void *void_first, const void *void_second)
 
   value = compare_words (&first->key, &second->key);
   return (value ? value
-          : first->key.start < second->key.start ? -1
-          : first->key.start > second->key.start);
+          : ((first->key.start > second->key.start)
+             - (first->key.start < second->key.start)));
 #undef first
 #undef second
 }
index 575dc2784ed2a854935022a3664e6fdb33609817..670706f1cbde3dbb01bc0289220ee6253b1c22ad 100644 (file)
@@ -55,13 +55,12 @@ add_range_pair (uintmax_t lo, uintmax_t hi)
 
 
 /* Comparison function for qsort to order the list of
-   struct range_pairs.  */
+   struct field_range_pairs.  */
 static int
 compare_ranges (const void *a, const void *b)
 {
-  int a_start = ((const struct field_range_pair *) a)->lo;
-  int b_start = ((const struct field_range_pair *) b)->lo;
-  return a_start < b_start ? -1 : a_start > b_start;
+  struct field_range_pair const *ap = a, *bp = b;
+  return (ap->lo > bp->lo) - (ap->lo < bp->lo);
 }
 
 /* Reallocate Range Pair entries, with corresponding