ref: 35cffbd08f2428ef2b9be2bc293aa4b345cf082a
parent: 69ad74ec96187d96516f1c7e0f7e95063bdee47f
author: Jacob Moody <moody@posixcafe.org>
date: Sun Dec 7 16:25:15 EST 2025
libc: add utfncmp()
--- a/sys/include/libc.h
+++ b/sys/include/libc.h
@@ -63,6 +63,7 @@
extern char* utfrrune(char*, long);
extern char* utfutf(char*, char*);
extern char* utfecpy(char*, char*, char*);
+extern int utfncmp(char*, char*, long);
extern Rune* runestrcat(Rune*, Rune*);
extern Rune* runestrchr(Rune*, Rune);
--- a/sys/man/2/rune
+++ b/sys/man/2/rune
@@ -39,6 +39,9 @@
.PP
.B
char* utfutf(char *s1, char *s2)
+.PP
+.B
+int utfncmp(char *s1, char *s2, long n)
.SH DESCRIPTION
These routines convert to and from a
.SM UTF
@@ -184,6 +187,24 @@
.I utfutf
returns
.IR s1 .
+.PP
+.I Utfncmp
+compares at most the first
+.I n
+runes contained within the
+.SM UTF
+strings
+.I s1
+and
+.IR s2 .
+Returning less than, equal to, or greater than 0 depending on the lexographical ordering of the runes.
+Callers are responsible for ensuring that
+.I s1
+and
+.I s2
+contain at least
+.I n
+runes.
.SH SOURCE
.B /sys/src/libc/port/rune.c
.br
@@ -198,6 +219,8 @@
.B /sys/src/libc/port/utfnlen.c
.br
.B /sys/src/libc/port/utfutf.c
+.br
+.B /sys/src/libc/port/utfncmp.c
.SH SEE ALSO
.IR utf (6),
.IR tcs (1)
--- a/sys/src/libc/port/mkfile
+++ b/sys/src/libc/port/mkfile
@@ -106,6 +106,7 @@
toupper.c\
utfecpy.c\
utflen.c\
+ utfncmp.c\
utfnlen.c\
utfrune.c\
utfrrune.c\
--- /dev/null
+++ b/sys/src/libc/port/utfncmp.c
@@ -1,0 +1,21 @@
+#include <u.h>
+#include <libc.h>
+
+int
+utfncmp(char *s1, char *s2, long n)
+{+ Rune r1, r2;
+
+ for(; n > 0; n--){+ s1 += chartorune(&r1, s1);
+ s2 += chartorune(&r2, s2);
+ if(r1 != r2){+ if(r1 > r2)
+ return 1;
+ return -1;
+ }
+ if(r1 == 0)
+ break;
+ }
+ return 0;
+}
--
⑨