From: K.Kosako Date: Thu, 5 May 2016 10:35:30 +0000 (+0900) Subject: add user_property.c X-Git-Tag: v6.0.0^2~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bfa0245fb7fb4f39f369ca1aede7d365a6f1fe1a;p=onig add user_property.c --- diff --git a/sample/.gitignore b/sample/.gitignore index 8b92ae0..963d2e4 100644 --- a/sample/.gitignore +++ b/sample/.gitignore @@ -6,4 +6,5 @@ /simple /sql /syntax +/user_property /log* diff --git a/sample/Makefile.am b/sample/Makefile.am index 8729e83..53f0d08 100644 --- a/sample/Makefile.am +++ b/sample/Makefile.am @@ -6,9 +6,9 @@ LDADD = $(lib_onig) AM_LDFLAGS = -L$(prefix)/lib AM_CPPFLAGS = -I../src -I$(includedir) -TESTS = encode listcap names posix simple sql syntax +TESTS = encode listcap names posix simple sql syntax user_property -check_PROGRAMS = encode listcap names posix simple sql syntax +check_PROGRAMS = encode listcap names posix simple sql syntax user_property encode_SOURCES = encode.c listcap_SOURCES = listcap.c @@ -17,11 +17,11 @@ posix_SOURCES = posix.c simple_SOURCES = simple.c sql_SOURCES = sql.c syntax_SOURCES = syntax.c - +user_property_SOURCES = user_property.c sampledir = . -test: encode listcap names posix simple sql syntax +test: encode listcap names posix simple sql syntax user_property $(sampledir)/encode $(sampledir)/listcap $(sampledir)/names @@ -29,3 +29,4 @@ test: encode listcap names posix simple sql syntax $(sampledir)/simple $(sampledir)/sql $(sampledir)/syntax + $(sampledir)/user_property diff --git a/sample/user_property.c b/sample/user_property.c new file mode 100644 index 0000000..5750b58 --- /dev/null +++ b/sample/user_property.c @@ -0,0 +1,85 @@ +/* + * user_property.c + */ +#include +#include +#include "oniguruma.h" + +extern int +main(int argc, char* argv[]) +{ + int r; + unsigned char *start, *range, *end; + regex_t* reg; + OnigErrorInfo einfo; + OnigRegion *region; + + static OnigCodePoint handakuon_hiragana[] = { + 5, + 0x3071, 0x3071, /* PA */ + 0x3074, 0x3074, /* PI */ + 0x3077, 0x3077, /* PU */ + 0x307a, 0x307a, /* PE */ + 0x307d, 0x307d /* PO */ + }; + + static UChar* pattern = (UChar* )"\\A(\\p{HandakuonHiragana}{5})\\p{^HandakuonHiragana}\\z"; + /* "PA PI PU PE PO a" */ + static UChar* str = (UChar* )"\343\201\261\343\201\264\343\201\267\343\201\272\343\201\275a"; + + OnigEncoding use_encs[] = { ONIG_ENCODING_UTF8 }; + onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0])); + + r = onig_unicode_define_user_property("HandakuonHiragana", + handakuon_hiragana); + if (r == ONIG_NORMAL) { + fprintf(stdout, "define HandakuonHiragana\n"); + } + else { + char s[ONIG_MAX_ERROR_MESSAGE_LEN]; + onig_error_code_to_str(s, r); + fprintf(stderr, "ERROR: %s\n", s); + return -1; + } + + r = onig_new(®, pattern, pattern + strlen((char* )pattern), + ONIG_OPTION_DEFAULT, ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo); + if (r == ONIG_NORMAL) { + fprintf(stdout, "onig_new: success.\n"); + } + else { + char s[ONIG_MAX_ERROR_MESSAGE_LEN]; + onig_error_code_to_str(s, r, &einfo); + fprintf(stderr, "onig_new: ERROR: %s\n", s); + return -1; + } + + region = onig_region_new(); + + end = str + strlen((char* )str); + start = str; + range = end; + r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE); + if (r >= 0) { + int i; + + fprintf(stderr, "match at %d\n", r); + for (i = 0; i < region->num_regs; i++) { + fprintf(stderr, "%d: (%d-%d)\n", i, region->beg[i], region->end[i]); + } + } + else if (r == ONIG_MISMATCH) { + fprintf(stderr, "search fail\n"); + } + else { /* error */ + char s[ONIG_MAX_ERROR_MESSAGE_LEN]; + onig_error_code_to_str(s, r); + fprintf(stderr, "ERROR: %s\n", s); + return -1; + } + + onig_region_free(region, 1 /* 1:free self, 0:free contents only */); + onig_free(reg); + onig_end(); + return 0; +}