git: 9front

Download patch

ref: 2143a62fe1eba0986706d0efa2f12f38594fce08
parent: 8f5ab25e8696f51f2be8a6743edc2de68c9451d6
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Dec 8 10:54:43 EST 2025

libregexp: test rregexec() against regexec()

--- a/sys/src/libregexp/test/basic.rc
+++ b/sys/src/libregexp/test/basic.rc
@@ -1,7 +1,5 @@
 #!/bin/rc
 
-x=./$O.regexec
-
 fn fail {
 	echo $* >[1=2]
 	exit $"*
@@ -9,8 +7,14 @@
 
 fn M {
 	sub=`'('{echo $3}
-	if(! match=`'('{$x $1 $2 $#sub}) {
-		fail 'did not match:' $*
+	if(! match=`'('{./$O.regexec $1 $2 $#sub}) {
+		fail 'regexec did not match:' $*
+	}
+	if(! rmatch=`'('{./$O.rregexec $1 $2 $#sub}) {
+		fail 'rregexec did not match:' $*
+	}
+	if(! ~ $"match $"rmatch) {
+		fail 'inconsistent' 'regexec:' $"match 'rregexec:' $"rmatch
 	}
 	match=$match(1-$#sub)
 	if(! ~ $"match $"sub){
--- a/sys/src/libregexp/test/mkfile
+++ b/sys/src/libregexp/test/mkfile
@@ -5,5 +5,6 @@
 </sys/src/cmd/mktest
 
 $O.regexec: regexec.$O
+$O.rregexec: rregexec.$O
 
-%.test: $O.regexec
+%.test: $O.regexec $O.rregexec
--- /dev/null
+++ b/sys/src/libregexp/test/rregexec.c
@@ -1,0 +1,58 @@
+#include <u.h>
+#include <libc.h>
+#include <regexp.h>
+
+Reprog *re;
+Resub m[10];
+Rune buf[8192];
+
+void
+usage(void)
+{
+	fprint(2, "usage: %s pattern string [nsub]\n", argv0);
+	exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+	int i, n;
+	char *cp;
+
+	ARGBEGIN {
+	} ARGEND;
+
+	if(argc < 2)
+		usage();
+	re = regcomp(argv[0]);
+	if(re == nil)
+		sysfatal("regcomp");
+	n = nelem(m);
+	if(argc == 3)
+		n = atoi(argv[2]);
+	if(n > nelem(m))
+		sysfatal("too many substitutions");
+
+	/* convert to rune buffer */
+	cp = argv[1];
+	for(i = 0; *cp != '\0'; i++){
+		if(i >= nelem(buf))
+			sysfatal("string too long");
+		cp += chartorune(&buf[i], cp);
+	}
+
+	if(rregexec(re, buf, m, n) <= 0)
+		exits("no match");
+	for(i = 0; i < n; i++) {
+		if(m[i].rsp == nil)
+			print("(?");
+		else
+			print("(%d", (int)(m[i].rsp - buf));
+		if(m[i].rep == nil)
+			print(",?)");
+		else
+			print(",%d)", (int)(m[i].rep - buf));
+	}
+	print("\n");
+	exits(nil);
+}
--