-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu8_compare.c
58 lines (52 loc) · 1.12 KB
/
u8_compare.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "u8.h"
int u8_rune_compare(const char *r1, const char *r2) {
int r1_length = u8_rune_length(r1);
int r2_length = u8_rune_length(r2);
if(r1_length == r2_length) {
for(int i = 0; i < r1_length; i++) {
if(r1[i] != r2[i]) {
return r1[i] < r2[i] ? -1 : 1;
}
}
return 0;
} else {
return r1_length < r2_length ? -1 : 1;
}
}
int u8_rune_qcompare(void *r1, void *r2) {
return u8_rune_compare((const char *)r1, (const char *)r2);
}
int u8_compare(const char *s1, const char *s2) {
while(*s1 != '\0' && *s2 != '\0') {
int r = u8_rune_compare(s1, s2);
if(r != 0) {
return r;
}
s1 = u8_next(s1);
s2 = u8_next(s2);
}
if(*s1 == '\0' && *s2 == '\0') {
return 0;
} else {
return *s1 == '\0' ? -1 : 1;
}
}
int u8_ncompare(const char *s1, const char *s2, unsigned int n) {
while(*s1 != '\0' && *s2 != '\0' && n > 0) {
int r = u8_rune_compare(s1, s2);
if(r != 0) {
return r;
}
s1 = u8_next(s1);
s2 = u8_next(s2);
n--;
}
if(n == 0) {
return 0;
} else {
return *s1 == '\0' ? -1 : 1;
}
}
int u8_qcompare(void *s1, void *s2) {
return u8_compare((const char *)s1, (const char *)s2);
}