From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 7 Mar 2024 17:28:00 -0500 Subject: lib: Add mlibc Signed-off-by: Ian Moffett --- lib/mlibc/tests/posix/regex.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/mlibc/tests/posix/regex.c (limited to 'lib/mlibc/tests/posix/regex.c') diff --git a/lib/mlibc/tests/posix/regex.c b/lib/mlibc/tests/posix/regex.c new file mode 100644 index 0000000..6c2ca3f --- /dev/null +++ b/lib/mlibc/tests/posix/regex.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +int main(void) { + char *testString = "mlibc is the best best best libc"; + char *pattern = "\\(be[a-z]t\\) \\1"; + + regex_t reg; + int rc = regcomp(®, pattern, 0); + assert(!rc); + + regmatch_t matches[2]; + rc = regexec(®, testString, 2, matches, 0); + assert(!rc); + + printf("Whole pattern: \"%.*s\" at %zd-%zd.\n", + (int)(matches[0].rm_eo - matches[0].rm_so), &testString[matches[0].rm_so], + (ssize_t)matches[0].rm_so, (ssize_t)(matches[0].rm_eo - 1)); + assert(matches[0].rm_so == 13 && matches[0].rm_eo == 22); + + printf("Substring: \"%.*s\" at %zd-%zd.\n", + (int)(matches[1].rm_eo - matches[1].rm_so), &testString[matches[1].rm_so], + (ssize_t)matches[1].rm_so, (ssize_t)matches[1].rm_eo - 1); + assert(matches[1].rm_so == 13 && matches[1].rm_eo == 17); + + regfree(®); + return 0; +} -- cgit v1.2.3