Refactor string

This commit is contained in:
2022-08-22 12:13:20 +03:00
parent 205c8337c2
commit ca8251973e
19 changed files with 627 additions and 1111 deletions
+66
View File
@@ -0,0 +1,66 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <ctype.h>
#include "include.h"
int string_compare(const str_t* s0, const str_t* s1) {
ssize_t n0, n1;
if (s0 == s1) return 0;
n0 = (!is_null(s0->buffer)) ? strlen(s0->buffer) : 0;
n1 = (!is_null(s1->buffer)) ? strlen(s1->buffer) : 0;
n0 -= n1;
if (n0 || !n1) return n0;
return memcmp(s0->buffer, s1->buffer, n1);
}
int string_case_compare(const str_t* s0, const str_t* s1) {
const char *p0, *p1, *t0, *t1;
ssize_t n0, n1;
u32_t uc0, uc1;
if (s0 == s1) return 0;
p0 = s0->buffer;
p1 = s1->buffer;
n0 = (!is_null(p0)) ? strasciilen(p0) : 0;
n1 = (!is_null(p1)) ? strasciilen(p1) : 0;
n0 -= n1;
if (!n0 && n1) {
do {
n0 = toupper(*(p0++));
n0 -= toupper(*(p1++));
if (n0) return n0;
} while(--n1);
} else return memcmp(s0->buffer, s1->buffer, n1);
while (*p0 && *p1) {
t0 = fromchar_unicode(&uc0, p0);
t1 = fromchar_unicode(&uc1, p1);
if (is_null(t0) || is_null(t1)) {
n0 = (ssize_t)*(unsigned char*)(p0++) - *(unsigned char*)(p1++);
if (n0) return n0;
} else {
n0 = toupper_unicode(uc0);
if ((n0 -= toupper_unicode(uc1)))
return n0;
p0 = t0;
p1 = t1;
}
}
n0 = *(unsigned char*)p0 - *(unsigned char*)p1;
return n0;
}