[augeas-devel] [PATCH 3 of 3] typecheck_maybe: less convoluted check for empty match
David Lutterkort
lutter at redhat.com
Tue Sep 2 23:08:57 UTC 2008
3 files changed, 9 insertions(+), 17 deletions(-)
src/lens.c | 19 ++-----------------
src/regexp.c | 4 ++++
src/syntax.h | 3 +++
# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1220396908 25200
# Node ID fc1837dd0fadc99be4d031b8ccbcee0b0b681cb5
# Parent 359034962e09d7f86154f3f7225ef8091ed503c8
typecheck_maybe: less convoluted check for empty match
There's no need to construct an FA to check if a regular expression matches
the empty string.
diff -r 359034962e09 -r fc1837dd0fad src/lens.c
--- a/src/lens.c Tue Sep 02 16:06:57 2008 -0700
+++ b/src/lens.c Tue Sep 02 16:08:28 2008 -0700
@@ -466,20 +466,13 @@
static struct value *typecheck_maybe(struct info *info, struct lens *l) {
/* Check (r)? as (<e>|r) where <e> is the empty language */
- fa_t fa, eps;
struct value *exn = NULL;
- fa = regexp_to_fa(l->ctype);
- if (fa == NULL)
- return make_exn_value(ref(info), "Internal error: regexp_to_fa failed");
-
- eps = fa_make_basic(FA_EPSILON);
- if (fa_contains(eps, fa)) {
+ if (regexp_matches_empty(l->ctype)) {
exn = make_exn_value(ref(info),
"illegal optional expression: /%s/ matches the empty word",
l->ctype->pattern->str);
}
- fa_free(fa);
/* Typecheck the put direction; the check passes if
(1) the atype does not match the empty string, because we can tell
@@ -488,19 +481,11 @@
depending on whether the current node has a non NULL value or not
*/
if (exn == NULL && ! l->value) {
- fa = regexp_to_fa(l->atype);
- if (fa == NULL)
- return make_exn_value(ref(info),
- "Internal error: regexp_to_fa failed");
-
- eps = fa_make_basic(FA_EPSILON);
- if (fa_contains(eps, fa)) {
+ if (regexp_matches_empty(l->atype)) {
exn = make_exn_value(ref(info),
"optional expression matches the empty tree");
}
- fa_free(fa);
}
- fa_free(eps);
return exn;
}
diff -r 359034962e09 -r fc1837dd0fad src/regexp.c
--- a/src/regexp.c Tue Sep 02 16:06:57 2008 -0700
+++ b/src/regexp.c Tue Sep 02 16:08:28 2008 -0700
@@ -274,6 +274,10 @@
return re_match(r->re, string, size, start, regs);
}
+int regexp_matches_empty(struct regexp *r) {
+ return regexp_match(r, "", 0, 0, NULL) == 0;
+}
+
int regexp_nsub(struct regexp *r) {
if (r->re == NULL)
if (regexp_compile(r) == -1)
diff -r 359034962e09 -r fc1837dd0fad src/syntax.h
--- a/src/syntax.h Tue Sep 02 16:06:57 2008 -0700
+++ b/src/syntax.h Tue Sep 02 16:08:28 2008 -0700
@@ -171,6 +171,9 @@
*/
int regexp_match(struct regexp *r, const char *string, const int size,
const int start, struct re_registers *regs);
+
+/* Return 1 if R matches the empty string, 0 otherwise */
+int regexp_matches_empty(struct regexp *r);
/* Return the number of subexpressions (parentheses) inside R. May cause
* compilation of R; return -1 if compilation fails.
More information about the augeas-devel
mailing list