src/string.c

changeset 1073
13c8a92625d4
parent 1071
028cb6d22197
child 1074
e8e2813cdda6
equal deleted inserted replaced
1072:c89283cd559b 1073:13c8a92625d4
34 #include <assert.h> 34 #include <assert.h>
35 #include <errno.h> 35 #include <errno.h>
36 #include <limits.h> 36 #include <limits.h>
37 #include <float.h> 37 #include <float.h>
38 38
39 #ifndef _WIN32 39 #ifdef _WIN32
40 40 #define cx_strcasecmp_impl _strnicmp
41 #include <strings.h> // for strncasecmp() 41 #else
42 42 #include <strings.h>
43 #endif // _WIN32 43 #define cx_strcasecmp_impl strncasecmp
44 #endif
44 45
45 cxmutstr cx_mutstr(char *cstring) { 46 cxmutstr cx_mutstr(char *cstring) {
46 return (cxmutstr) {cstring, strlen(cstring)}; 47 return (cxmutstr) {cstring, strlen(cstring)};
47 } 48 }
48 49
461 462
462 int cx_strcmp( 463 int cx_strcmp(
463 cxstring s1, 464 cxstring s1,
464 cxstring s2 465 cxstring s2
465 ) { 466 ) {
466 int r = strncmp(s1.ptr, s2.ptr, s1.length);
467 if (r != 0) return r;
468 if (s1.length == s2.length) { 467 if (s1.length == s2.length) {
469 return 0; 468 return strncmp(s1.ptr, s2.ptr, s1.length);
470 } else if (s1.length > s2.length) { 469 } else if (s1.length > s2.length) {
470 int r = strncmp(s1.ptr, s2.ptr, s2.length);
471 if (r != 0) return r;
471 return 1; 472 return 1;
472 } else { 473 } else {
474 int r = strncmp(s1.ptr, s2.ptr, s1.length);
475 if (r != 0) return r;
473 return -1; 476 return -1;
474 } 477 }
475 } 478 }
476 479
477 int cx_strcasecmp( 480 int cx_strcasecmp(
478 cxstring s1, 481 cxstring s1,
479 cxstring s2 482 cxstring s2
480 ) { 483 ) {
481 #ifdef _WIN32
482 int r = _strnicmp(s1.ptr, s2.ptr, s1.length);
483 #else
484 int r = strncasecmp(s1.ptr, s2.ptr, s1.length);
485 #endif
486 if (r != 0) return r;
487 if (s1.length == s2.length) { 484 if (s1.length == s2.length) {
488 return 0; 485 return cx_strcasecmp_impl(s1.ptr, s2.ptr, s1.length);
489 } else if (s1.length > s2.length) { 486 } else if (s1.length > s2.length) {
487 int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s2.length);
488 if (r != 0) return r;
490 return 1; 489 return 1;
491 } else { 490 } else {
491 int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s1.length);
492 if (r != 0) return r;
492 return -1; 493 return -1;
493 } 494 }
494 } 495 }
495 496
496 int cx_strcmp_p( 497 int cx_strcmp_p(

mercurial