diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-08-12 21:06:31 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-08-12 21:06:31 +0200 |
commit | 0dbf3761f6b98e5c16c9bfa0306ae5d3257d081f (patch) | |
tree | b122da5e29b81eb5489734f477afb645422980d0 /sample/posix.c |
Imported Upstream version 5.9.5upstream/5.9.5
Diffstat (limited to 'sample/posix.c')
-rw-r--r-- | sample/posix.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/sample/posix.c b/sample/posix.c new file mode 100644 index 0000000..d24ee35 --- /dev/null +++ b/sample/posix.c @@ -0,0 +1,93 @@ +/* + * posix.c + */ +#include <stdio.h> +#include "onigposix.h" + +typedef unsigned char UChar; + +static int x(regex_t* reg, unsigned char* pattern, unsigned char* str) +{ + int r, i; + char buf[200]; + regmatch_t pmatch[20]; + + r = regexec(reg, (char* )str, reg->re_nsub + 1, pmatch, 0); + if (r != 0 && r != REG_NOMATCH) { + regerror(r, reg, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + + if (r == REG_NOMATCH) { + fprintf(stderr, "FAIL: /%s/ '%s'\n", pattern, str); + } + else { + fprintf(stderr, "OK: /%s/ '%s'\n", pattern, str); + for (i = 0; i <= (int )reg->re_nsub; i++) { + fprintf(stderr, "%d: %d-%d\n", i, pmatch[i].rm_so, pmatch[i].rm_eo); + } + } + return 0; +} + +extern int main(int argc, char* argv[]) +{ + int r; + char buf[200]; + regex_t reg; + UChar* pattern; + + /* default syntax (ONIG_SYNTAX_RUBY) */ + pattern = (UChar* )"^a+b{2,7}[c-f]?$|uuu"; + r = regcomp(®, (char* )pattern, REG_EXTENDED); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"aaabbbbd"); + + /* POSIX Basic RE (REG_EXTENDED is not specified.) */ + pattern = (UChar* )"^a+b{2,7}[c-f]?|uuu"; + r = regcomp(®, (char* )pattern, 0); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"a+b{2,7}d?|uuu"); + + /* POSIX Basic RE (REG_EXTENDED is not specified.) */ + pattern = (UChar* )"^a*b\\{2,7\\}\\([c-f]\\)$"; + r = regcomp(®, (char* )pattern, 0); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"aaaabbbbbbd"); + + /* POSIX Extended RE */ + onig_set_default_syntax(ONIG_SYNTAX_POSIX_EXTENDED); + pattern = (UChar* )"^a+b{2,7}[c-f]?)$|uuu"; + r = regcomp(®, (char* )pattern, REG_EXTENDED); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"aaabbbbd)"); + + pattern = (UChar* )"^b."; + r = regcomp(®, (char* )pattern, REG_EXTENDED | REG_NEWLINE); + if (r) { + regerror(r, ®, buf, sizeof(buf)); + fprintf(stderr, "ERROR: %s\n", buf); + return -1; + } + x(®, pattern, (UChar* )"a\nb\n"); + + regfree(®); + return 0; +} |