diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-09-29 06:07:55 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-09-29 06:07:55 -0700 |
commit | 0f68ca0217e15d7131c1e6ac6838a9649e416ed6 (patch) | |
tree | 7ab39e4de7a343a6b4bac33815cf8a81ede37004 | |
parent | 7f5f6b9bd9d5443c95888ebdd2726f7fb45f9b20 (diff) | |
download | txr-0f68ca0217e15d7131c1e6ac6838a9649e416ed6.tar.gz txr-0f68ca0217e15d7131c1e6ac6838a9649e416ed6.tar.bz2 txr-0f68ca0217e15d7131c1e6ac6838a9649e416ed6.zip |
Some optimizations for * ? and +.
* regex.c (regex_optimize): Simplify compounded
uses of repetition operators: RR* -> R, R+? -> R*
and so on.
-rw-r--r-- | regex.c | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -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)); |