commit bdfa5ee
uint
·
2026-10-18 23:02:16 +0000 UTC
parent 7daf886
UTF: Add uleutf cmp functions
2 files changed,
+41,
-2
+9,
-2
1@@ -30,6 +30,15 @@ char* uleutf_cat(char* dst, const char* src);
2 /* Find the first match of the utf8 codepoint chr in the utf8 string src */
3 char* uleutf_chr(const char* src, ule_i32 chr);
4
5+/* Return less than 0, 0, greater than 0 if src1 < src2, src1 == src2,
6+ src1 > src2 respectively */
7+int uleutf_cmp(const char* src1, const char* src2, size_t n);
8+
9+/* Return less than 0, 0, greater than 0 if src1 < src2,
10+ src1 == src2, src1 > src2 respectively. Checking at most n
11+ bytes of each utf8 string */
12+int uleutf_ncmp(const char* src1, const char* src2, size_t n);
13+
14 /* The position of the utf8 string needle in the utf8 string haystack */
15 char* uleutf_str(const char* haystack, const char* needle);
16
17@@ -50,7 +59,6 @@ size_t uleutf_codepointsize(ule_i32 chr);
18 int uleutf_codepointvisualwidth(ule_i32 chr);
19
20 /* TODO */
21-int uleutf_cmp(const char* src1, const char* src2);
22 char* uleutf_cpy(char* dst, const char* src);
23 size_t uleutf_cspn(const char* src, const char* reject);
24 char* uleutf_dup(const char* src);
25@@ -58,7 +66,6 @@ size_t uleutf_len(const char* str);
26 size_t uleutf_nlen(const char* str, size_t n);
27 int uleutf_ncasecmp(const char* src1, const char* src2, size_t n);
28 char* uleutf_ncat(char* dst, const char* src, size_t n);
29-int uleutf_ncmp(const char* src1, const char* src2, size_t n);
30 char* uleutf_ncpy(char* dst, const char* src, size_t n);
31 char* uleutf_ndup(const char* src, size_t n);
32 char* uleutf_pbrk(const char* str, const char* accept);
+32,
-0
1@@ -96,6 +96,38 @@ char* uleutf_chr(const char* src, ule_i32 chr)
2 return uleutf_str(src, c);
3 }
4
5+int uleutf_cmp(const char* src1, const char* src2, size_t n)
6+{
7+ while (('\0' != *src1) || ('\0' != *src2)) {
8+ if ((unsigned char)*src1 < (unsigned char)*src2)
9+ return -1;
10+ else if ((unsigned char)*src1 > (unsigned char)*src2)
11+ return 1;
12+
13+ src1++;
14+ src2++;
15+ }
16+
17+ /* both utf8 strings matched */
18+ return 0;
19+}
20+
21+int uleutf_ncmp(const char* src1, const char* src2, size_t n)
22+{
23+ while ((0 != n--) && (('\0' != *src1) || ('\0' != *src2))) {
24+ if ((unsigned char)*src1 < (unsigned char)*src2)
25+ return -1;
26+ else if ((unsigned char)*src1 > (unsigned char)*src2)
27+ return 1;
28+
29+ src1++;
30+ src2++;
31+ }
32+
33+ /* both utf8 strings matched */
34+ return 0;
35+}
36+
37 char* uleutf_str(const char* haystack, const char* needle)
38 {
39 ule_i32 throwaway_codepoint = 0;