From 0f68ca0217e15d7131c1e6ac6838a9649e416ed6 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 29 Sep 2015 06:07:55 -0700 Subject: Some optimizations for * ? and +. * regex.c (regex_optimize): Simplify compounded uses of repetition operators: RR* -> R, R+? -> R* and so on. --- regex.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/regex.c b/regex.c index baa1d27e..3dcd5172 100644 --- a/regex.c +++ b/regex.c @@ -1804,8 +1804,13 @@ static val reg_optimize(val exp) return cons(sym, xargs); } else if (sym == zeroplus_s) { val arg = reg_optimize(first(args)); - if (consp(arg) && car(arg) == zeroplus_s) - return arg; + if (consp(arg)) { + val arg2 = first(arg); + if (arg2 == zeroplus_s) + return arg; + if (arg2 == oneplus_s || arg2 == optional_s) + return cons(zeroplus_s, cdr(arg)); + } if (reg_matches_all(arg)) return arg; return cons(sym, cons(arg, nil)); @@ -1813,13 +1818,25 @@ static val reg_optimize(val exp) val arg = reg_optimize(first(args)); if (reg_matches_all(arg)) return cons(zeroplus_s, cons(wild_s, nil)); - if (consp(arg) && car(arg) == zeroplus_s) - return arg; + if (consp(arg)) { + val arg2 = first(arg); + if (arg2 == zeroplus_s || arg2 == oneplus_s) + return arg; + if (arg2 == optional_s) + return cons(zeroplus_s, cdr(arg)); + } return cons(sym, cons(arg, nil)); } else if (sym == optional_s) { val arg = reg_optimize(first(args)); if (reg_matches_all(arg)) return arg; + if (consp(arg)) { + val arg2 = first(arg); + if (arg2 == zeroplus_s || arg2 == optional_s) + return arg; + if (arg2 == oneplus_s) + return cons(zeroplus_s, cdr(arg)); + } return cons(sym, cons(arg, nil)); } else if (sym == compl_s) { val arg = reg_optimize(first(args)); -- cgit v1.2.3