]> Sergey Matveev's repositories - nnn.git/commitdiff
Simplify numeric value check
authorArun Prakash Jana <engineerarun@gmail.com>
Sat, 31 Aug 2019 20:14:19 +0000 (01:44 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sun, 1 Sep 2019 06:23:17 +0000 (11:53 +0530)
src/nnn.c

index 21c93c6662897a7592a21ffa4f7ca50f2979f3c3..e3b77ee8084753400773a695a3b3b0ffd634318f 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -180,6 +180,7 @@ typedef unsigned long ulong;
 typedef unsigned int uint;
 typedef unsigned char uchar;
 typedef unsigned short ushort;
+typedef long long int ll;
 
 /* STRUCTURES */
 
@@ -1314,14 +1315,6 @@ static bool write_lastdir(const char *curpath)
        return ret;
 }
 
-static int digit_compare(const char *a, const char *b)
-{
-       while (*a && *b && *a == *b)
-               ++a, ++b;
-
-       return *a - *b;
-}
-
 /*
  * We assume none of the strings are NULL.
  *
@@ -1332,73 +1325,32 @@ static int digit_compare(const char *a, const char *b)
  */
 static int xstricmp(const char * const s1, const char * const s2)
 {
-       const char *c1 = s1, *c2 = s2, *m1, *m2;
-       int count1 = 0, count2 = 0, bias;
-       char sign[2] = {'+', '+'};
-
-       while (ISBLANK(*c1))
-               ++c1;
-
-       while (ISBLANK(*c2))
-               ++c2;
-
-       if (*c1 == '-' || *c1 == '+') {
-               if (*c1 == '-')
-                       sign[0] = '-';
-               ++c1;
-       }
-
-       if (*c2 == '-' || *c2 == '+') {
-               if (*c2 == '-')
-                       sign[1] = '-';
-               ++c2;
-       }
-
-       if (xisdigit(*c1) && xisdigit(*c2)) {
-               while (*c1 == '0')
-                       ++c1;
-               m1 = c1;
+       char *p1, *p2;
 
-               while (*c2 == '0')
-                       ++c2;
-               m2 = c2;
+       ll v1 = strtoll(s1, &p1, 10);
+       ll v2 = strtoll(s2, &p2, 10);
 
-               while (xisdigit(*c1)) {
-                       ++count1;
-                       ++c1;
-               }
-               while (ISBLANK(*c1))
-                       ++c1;
+       /* Check if at least 1 string is numeric */
+       if (s1 != p1 || s2 != p2) {
+               /* Handle both pure numeric */
+               if (s1 != p1 && s2 != p2) {
+                       if (v2 > v1)
+                               return -1;
 
-               while (xisdigit(*c2)) {
-                       ++count2;
-                       ++c2;
+                       if (v1 > v2)
+                               return 1;
                }
-               while (ISBLANK(*c2))
-                       ++c2;
 
-               if (*c1 && !*c2)
+               /* Only first string non-numeric */
+               if (s1 == p1)
                        return 1;
 
-               if (!*c1 && *c2)
+               /* Only second string non-numeric */
+               if (s2 == p2)
                        return -1;
-
-               if (!*c1 && !*c2) {
-                       if (sign[0] != sign[1])
-                               return ((sign[0] == '+') ? 1 : -1);
-
-                       if (count1 > count2)
-                               return 1;
-
-                       if (count1 < count2)
-                               return -1;
-
-                       bias = digit_compare(m1, m2);
-                       if (bias)
-                               return bias;
-               }
        }
 
+       /* Handle 1. all non-numeric and 2. both same numeric value cases */
        return strcoll(s1, s2);
 }