noice

small file browser
git clone git://git.2f30.org/noice
Log | Files | Refs | README | LICENSE

strverscmp.c (979B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <ctype.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include "util.h"
      7 
      8 int
      9 strverscmp(const char *str1, const char *str2)
     10 {
     11 	size_t len1 = strlen(str1);
     12 	size_t len2 = strlen(str2);
     13 	size_t i1 = 0;
     14 	size_t i2 = 0;
     15 	for (; i1 < len1 && i2 < len2; i1++, i2++) {
     16 		unsigned char c1 = str1[i1];
     17 		unsigned char c2 = str2[i2];
     18 		if (isdigit(c1) && isdigit(c2)) {
     19 			unsigned long long int num1;
     20 			unsigned long long int num2;
     21 			char *end1;
     22 			char *end2;
     23 			num1 = strtoull(str1 + i1, &end1, 10);
     24 			num2 = strtoull(str2 + i2, &end2, 10);
     25 			DPRINTF_LLU(num1);
     26 			DPRINTF_LLU(num2);
     27 			if (num1 < num2)
     28 				return -1;
     29 			if (num1 > num2)
     30 				return 1;
     31 			i1 = end1 - str1 - 1;
     32 			i2 = end2 - str2 - 1;
     33 			if (i1 < i2)
     34 				return -1;
     35 			if (i1 > i2)
     36 				return 1;
     37 		} else {
     38 			if (c1 < c2)
     39 				return -1;
     40 			if (c1 > c2)
     41 				return 1;
     42 		}
     43 	}
     44 	if (len1 < len2)
     45 		return -1;
     46 	if (len1 > len2)
     47 		return 1;
     48 	return 0;
     49 }