174989affSNico Weber // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
274989affSNico Weber //
3*975fa725SPaul Robinson // UNSUPPORTED: darwin, target={{.*solaris.*}}
474989affSNico Weber
574989affSNico Weber #include <assert.h>
674989affSNico Weber #include <regex.h>
774989affSNico Weber #include <stdio.h>
874989affSNico Weber #include <stdlib.h>
974989affSNico Weber
1074989affSNico Weber #ifndef __arraycount
1174989affSNico Weber #define __arraycount(a) ((sizeof(a) / sizeof(a[0])))
1274989affSNico Weber #endif
1374989affSNico Weber
test_matched(const regex_t * preg,const char * string)1474989affSNico Weber void test_matched(const regex_t *preg, const char *string) {
1574989affSNico Weber int rv = regexec(preg, string, 0, NULL, 0);
1674989affSNico Weber if (!rv)
1774989affSNico Weber printf("%s: matched\n", string);
1874989affSNico Weber else if (rv == REG_NOMATCH)
1974989affSNico Weber printf("%s: not-matched\n", string);
2074989affSNico Weber else
2174989affSNico Weber abort();
2274989affSNico Weber }
2374989affSNico Weber
test_print_matches(const regex_t * preg,const char * string)2474989affSNico Weber void test_print_matches(const regex_t *preg, const char *string) {
2574989affSNico Weber regmatch_t rm[10];
2674989affSNico Weber int rv = regexec(preg, string, __arraycount(rm), rm, 0);
2774989affSNico Weber if (!rv) {
2874989affSNico Weber for (size_t i = 0; i < __arraycount(rm); i++) {
2974989affSNico Weber // This condition shall be simplified, but verify that the data fields
3074989affSNico Weber // are accessible.
3174989affSNico Weber if (rm[i].rm_so == -1 && rm[i].rm_eo == -1)
3274989affSNico Weber continue;
3374989affSNico Weber printf("matched[%zu]='%.*s'\n", i, (int)(rm[i].rm_eo - rm[i].rm_so),
3474989affSNico Weber string + rm[i].rm_so);
3574989affSNico Weber }
3674989affSNico Weber } else if (rv == REG_NOMATCH)
3774989affSNico Weber printf("%s: not-matched\n", string);
3874989affSNico Weber else
3974989affSNico Weber abort();
4074989affSNico Weber }
4174989affSNico Weber
main(void)4274989affSNico Weber int main(void) {
4374989affSNico Weber printf("regex\n");
4474989affSNico Weber
459a42715aSTavian Barnes {
4674989affSNico Weber regex_t regex;
4774989affSNico Weber int rv = regcomp(®ex, "[[:upper:]]\\([[:upper:]]\\)", 0);
4874989affSNico Weber assert(!rv);
4974989affSNico Weber
5074989affSNico Weber test_matched(®ex, "abc");
5174989affSNico Weber test_matched(®ex, "ABC");
5274989affSNico Weber
5374989affSNico Weber test_print_matches(®ex, "ABC");
5474989affSNico Weber
5574989affSNico Weber regfree(®ex);
569a42715aSTavian Barnes }
5774989affSNico Weber
589a42715aSTavian Barnes {
599a42715aSTavian Barnes regex_t regex;
609a42715aSTavian Barnes int rv = regcomp(®ex, "[[:upp:]]", 0);
6174989affSNico Weber assert(rv);
6274989affSNico Weber
6374989affSNico Weber char errbuf[1024];
6474989affSNico Weber regerror(rv, ®ex, errbuf, sizeof errbuf);
6574989affSNico Weber printf("error: %s\n", errbuf);
6674989affSNico Weber
679a42715aSTavian Barnes regfree(®ex);
689a42715aSTavian Barnes }
699a42715aSTavian Barnes
7074989affSNico Weber // CHECK: regex
7174989affSNico Weber // CHECK: abc: not-matched
7274989affSNico Weber // CHECK: ABC: matched
7374989affSNico Weber // CHECK: matched[0]='AB'
7474989affSNico Weber // CHECK: matched[1]='B'
7574989affSNico Weber // CHECK: error:{{.*}}
7674989affSNico Weber
7774989affSNico Weber return 0;
7874989affSNico Weber }
79