From ba39c5b41e2993a39d752c7282847a53cbdb3787 Mon Sep 17 00:00:00 2001 From: "K.Kosako" Date: Wed, 28 Feb 2018 11:22:45 +0900 Subject: [PATCH] add two arguments into (*COUNT{[+,-],[p,s,f]}) and remove (*FAIL_COUNT) --- sample/count.c | 2 +- src/ascii.c | 10 +++++++--- src/regexec.c | 35 +++++++++++++++++++++++++++++++---- src/regint.h | 8 ++++---- src/utf16_be.c | 14 +++++++++----- src/utf16_le.c | 14 +++++++++----- 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/sample/count.c b/sample/count.c index d43ff46..5b894f5 100644 --- a/sample/count.c +++ b/sample/count.c @@ -105,7 +105,7 @@ extern int main(int argc, char* argv[]) test(encs[0], mp, "abc(.(*COUNT[x]))*(*FAIL)", "abcdefg"); test(encs[0], mp, "abc(.(*COUNT[_any_]))*(.(*COUNT[x]))*d", "abcdefg"); - test(encs[0], mp, "abc(.(*FAIL_COUNT[x]))*f", "abcdefg"); + test(encs[0], mp, "abc(.(*COUNT[x]{-,f}))*f", "abcdefg"); test(encs[0], mp, "a(.(*COUNT[x]))*z", "abcd\nabcdz"); test(encs[1], mp, "\000a\000b\000c\000(\000.\000(\000*\000C\000O\000U\000N\000T\000[\000x\000]\000)\000)\000*\000(\000*\000F\000A\000I\000L\000)\000\000", "\000a\000b\000c\000d\000e\000f\000g\000\000"); diff --git a/src/ascii.c b/src/ascii.c index bb18e4e..88c217c 100644 --- a/src/ascii.c +++ b/src/ascii.c @@ -42,6 +42,7 @@ init(void) char* name; OnigType t_int; OnigType t_long; + OnigType args[4]; OnigValue opts[4]; enc = ONIG_ENCODING_ASCII; @@ -52,12 +53,15 @@ init(void) name = "SUCCESS"; BC0_P(name, success); name = "ABORT"; BC0_P(name, abort); name = "ERROR"; BC1_P(name, error, &t_int); - name = "COUNT"; BC0_P(name, count); - name = "FAIL_COUNT"; BC0_R(name, count); name = "MAX"; BC1_B(name, max, &t_long); + name = "COUNT"; + args[0] = ONIG_TYPE_CHAR; + args[1] = ONIG_TYPE_CHAR; + opts[0].cp = ' '; opts[1].cp = 'p'; + BC_B_O(name, count, 2, args, 2, opts); name = "ONLY"; opts[0].l = 1L; - BC1_B_O(name, max, &t_long, opts); + BC_B_O(name, max, 1, &t_long, 1, opts); #endif /* USE_CALLOUT */ diff --git a/src/regexec.c b/src/regexec.c index 69adde4..c62077a 100644 --- a/src/regexec.c +++ b/src/regexec.c @@ -5299,10 +5299,28 @@ onig_builtin_count(OnigCalloutArgs* args, void* user_data ARG_UNUSED) int slot; OnigType type; OnigValue val; - - (void )onig_check_callout_data_and_clear_old_values(args); + OnigValue aval; + int is_total; + OnigCodePoint count_type; num = args->num; + + r = onig_get_arg_of_callout_args(args, 0, &type, &aval); + if (r != ONIG_NORMAL) return r; + + is_total = (aval.cp == '+' ? 1 : 0); + + r = onig_get_arg_of_callout_args(args, 1, &type, &aval); + if (r != ONIG_NORMAL) return r; + + count_type = aval.cp; + if (count_type != 'p' && count_type != 's' && count_type != 'f') + return ONIGERR_INVALID_CALLOUT_ARG; + + if (! is_total) { + (void )onig_check_callout_data_and_clear_old_values(args); + } + slot = 0; r = onig_get_callout_data_by_callout_num(args->regex, args->msa->mp, num, slot, &type, &val); @@ -5314,10 +5332,19 @@ onig_builtin_count(OnigCalloutArgs* args, void* user_data ARG_UNUSED) val.l = 0; } - val.l++; + if (args->in == ONIG_CALLOUT_IN_RETRACTION) { + if (count_type == 'f') + val.l++; + else if (count_type == 's') + val.l--; + } + else { + if (count_type != 'f') + val.l++; + } r = onig_set_callout_data_by_callout_num(args->regex, args->msa->mp, num, slot, - type, &val); + ONIG_TYPE_LONG, &val); if (r != ONIG_NORMAL) return r; return ONIG_CALLOUT_SUCCESS; diff --git a/src/regint.h b/src/regint.h index 2c783b5..515f922 100644 --- a/src/regint.h +++ b/src/regint.h @@ -864,12 +864,12 @@ extern CalloutListEntry* onig_reg_callout_list_at(regex_t* reg, int num); if (id < 0) return id;\ } while(0) -#define BC1_B_O(name, func, ts, opts) do {\ +#define BC_B_O(name, func, nts, ts, nopts, opts) do {\ int len = onigenc_str_bytelen_null(enc, (UChar* )name);\ id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,\ - (UChar* )(name), (UChar* )((name) + len),\ - ONIG_CALLOUT_IN_BOTH,\ - onig_builtin_ ## func, 0, 1, (ts), 1, opts);\ + (UChar* )(name), (UChar* )((name) + len),\ + ONIG_CALLOUT_IN_BOTH,\ + onig_builtin_ ## func, 0, (nts), (ts), (nopts), (opts));\ if (id < 0) return id;\ } while(0) diff --git a/src/utf16_be.c b/src/utf16_be.c index e002f0b..0e80b0c 100644 --- a/src/utf16_be.c +++ b/src/utf16_be.c @@ -43,6 +43,7 @@ init(void) char* name; OnigType t_int; OnigType t_long; + OnigType args[4]; OnigValue opts[4]; enc = ONIG_ENCODING_UTF16_BE; @@ -53,13 +54,16 @@ init(void) name = "\000S\000U\000C\000C\000E\000S\000S\000\000"; BC0_P(name, success); name = "\000A\000B\000O\000R\000T\000\000"; BC0_P(name, abort); name = "\000E\000R\000R\000O\000R\000\000"; BC1_P(name, error, &t_int); - name = "\000C\000O\000U\000N\000T\000\000"; BC0_P(name, count); - name = "\000F\000A\000I\000L\000_\000C\000O\000U\000N\000T\000\000"; - BC0_R(name, count); - name = "\000M\000A\000X\000\000"; BC1_B(name, max, &t_long); + + name = "\000C\000O\000U\000N\000T\000\000"; + args[0] = ONIG_TYPE_CHAR; + args[1] = ONIG_TYPE_CHAR; + opts[0].cp = ' '; opts[1].cp = 'p'; + BC_B_O(name, count, 2, args, 2, opts); + name = "\000O\000N\000L\000Y\000\000"; opts[0].l = 1L; - BC1_B_O(name, max, &t_long, opts); + BC_B_O(name, max, 1, &t_long, 1, opts); #endif /* USE_CALLOUT */ diff --git a/src/utf16_le.c b/src/utf16_le.c index 67fdbee..6c7a333 100644 --- a/src/utf16_le.c +++ b/src/utf16_le.c @@ -43,6 +43,7 @@ init(void) char* name; OnigType t_int; OnigType t_long; + OnigType args[4]; OnigValue opts[4]; enc = ONIG_ENCODING_UTF16_LE; @@ -53,13 +54,16 @@ init(void) name = "S\000U\000C\000C\000E\000S\000S\000\000\000"; BC0_P(name, success); name = "A\000B\000O\000R\000T\000\000\000"; BC0_P(name, abort); name = "E\000R\000R\000O\000R\000\000\000"; BC1_P(name, error, &t_int); - name = "C\000O\000U\000N\000T\000\000\000"; BC0_P(name, count); - name = "F\000A\000I\000L\000_\000C\000O\000U\000N\000T\000\000\000"; - BC0_R(name, count); - name = "M\000A\000X\000\000\000"; BC1_B(name, max, &t_long); + + name = "C\000O\000U\000N\000T\000\000\000"; + args[0] = ONIG_TYPE_CHAR; + args[1] = ONIG_TYPE_CHAR; + opts[0].cp = ' '; opts[1].cp = 'p'; + BC_B_O(name, count, 2, args, 2, opts); + name = "O\000N\000L\000Y\000\000\000"; opts[0].l = 1L; - BC1_B_O(name, max, &t_long, opts); + BC_B_O(name, max, 1, &t_long, 1, opts); #endif /* USE_CALLOUT */ inited = 1; -- 2.40.0