aboutsummaryrefslogtreecommitdiffstats
path: root/strverscmp.c
diff options
context:
space:
mode:
authorLazaros Koromilas <lostd@2f30.org>2019-04-08 12:48:24 +0300
committerLazaros Koromilas <lostd@2f30.org>2019-04-08 13:20:13 +0300
commitb2f2a6fe76bdbba48c0174b4f1eb05caf1a74b71 (patch)
tree184f77e2f318f3f605edb6a1e916b0b2245dc29b /strverscmp.c
parent6b1ec1959954145d7942ba3b7e65f5cde30485a1 (diff)
downloadnoice-b2f2a6fe76bdbba48c0174b4f1eb05caf1a74b71.tar.gz
Add sort by version number mode
Diffstat (limited to 'strverscmp.c')
-rw-r--r--strverscmp.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/strverscmp.c b/strverscmp.c
new file mode 100644
index 0000000..a1c6a4f
--- /dev/null
+++ b/strverscmp.c
@@ -0,0 +1,47 @@
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include "util.h"
+
+int
+strverscmp(const char *str1, const char *str2)
+{
+ size_t len1 = strlen(str1);
+ size_t len2 = strlen(str2);
+ size_t i1 = 0;
+ size_t i2 = 0;
+ for (; i1 < len1 && i2 < len2; i1++, i2++) {
+ char c1 = str1[i1];
+ char c2 = str2[i2];
+ if (isdigit(c1) && isdigit(c2)) {
+ unsigned long long int num1;
+ unsigned long long int num2;
+ char *end1;
+ char *end2;
+ num1 = strtoull(str1 + i1, &end1, 10);
+ num2 = strtoull(str2 + i2, &end2, 10);
+ DPRINTF_LLU(num1);
+ DPRINTF_LLU(num2);
+ if (num1 < num2)
+ return -1;
+ if (num1 > num2)
+ return 1;
+ i1 = end1 - str1 - 1;
+ i2 = end2 - str2 - 1;
+ if (i1 < i2)
+ return -1;
+ if (i1 > i2)
+ return 1;
+ } else {
+ if (c1 < c2)
+ return -1;
+ if (c1 > c2)
+ return 1;
+ }
+ }
+ if (len1 < len2)
+ return -1;
+ if (len1 > len2)
+ return 1;
+ return 0;
+}
Un proyecto texto-plano.xyz