[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] [PATCH] * src/fa.h (fa_example): cleaner prototype



---
 src/fa.c       |   15 ++++++++++++---
 src/fa.h       |    8 +++++++-
 src/lens.c     |    4 +++-
 tests/fatest.c |   19 +++++++++++++------
 4 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/src/fa.c b/src/fa.c
index e5f8ea7..01c36b5 100644
--- a/src/fa.c
+++ b/src/fa.c
@@ -2187,7 +2187,10 @@ static char pick_char(struct trans *t) {
 /* Generate an example string for FA. Traverse all transitions and record
  * at each turn the "best" word found for that state.
  */
-char *fa_example(struct fa *fa) {
+int fa_example(struct fa *fa, char **example, size_t *example_len) {
+    *example = NULL;
+    *example_len = 0;
+
     /* Sort to avoid any ambiguity because of reordering of transitions */
     sort_transition_intervals(fa);
 
@@ -2230,7 +2233,11 @@ char *fa_example(struct fa *fa) {
     }
     state_set_free(path);
     state_set_free(worklist);
-    return word;
+    /* FIXME: handle embedded nul's */
+    if (word != NULL)
+        *example_len = strlen(word);
+    *example = word;
+    return 0;
 }
 
 /* Expand the automaton FA by replacing every transition s(c) -> p from
@@ -2336,7 +2343,9 @@ char *fa_ambig_example(struct fa *fa1, struct fa *fa2, char **pv, char **v) {
     fa_free(b1);
     fa_free(b2);
 
-    char *s = fa_example(amb);
+    size_t s_len = 0;
+    char *s = NULL;
+    fa_example(amb, &s, &s_len);
     fa_free(amb);
 
     if (s == NULL)
diff --git a/src/fa.h b/src/fa.h
index e28e3b2..477ad0d 100644
--- a/src/fa.h
+++ b/src/fa.h
@@ -153,8 +153,14 @@ struct fa *fa_overlap(struct fa *fa1, struct fa *fa2);
  * necessarily the shortest possible. The implementation works very hard to
  * have printable characters (preferrably alphanumeric) in the example, and
  * to avoid just an empty word.
+ *
+ * *EXAMPLE will be the example, which may be NULL. If it is non-NULL,
+ *  EXAMPLE_LEN will hold the length of the example.
+ *
+ * Return 0 on success, and a negative numer on error. On error, *EXAMPLE
+ * will be NULL
  */
-char *fa_example(struct fa *fa);
+int fa_example(struct fa *fa, char **example, size_t *example_len);
 
 /* Produce an example of an ambiguous word for the concatenation of the
  * languages of FA1 and FA2. The return value is such a word (which must be
diff --git a/src/lens.c b/src/lens.c
index 7d905e8..5757b3f 100644
--- a/src/lens.c
+++ b/src/lens.c
@@ -372,7 +372,9 @@ static struct value *disjoint_check(struct info *info, const char *msg,
 
     fa = fa_intersect(fa1, fa2);
     if (! fa_is_basic(fa, FA_EMPTY)) {
-        char *xmpl = fa_example(fa);
+        size_t xmpl_len;
+        char *xmpl;
+        fa_example(fa, &xmpl, &xmpl_len);
         exn = make_exn_value(ref(info),
                              "overlapping lenses in %s", msg);
 
diff --git a/tests/fatest.c b/tests/fatest.c
index d153da4..19593ae 100644
--- a/tests/fatest.c
+++ b/tests/fatest.c
@@ -304,7 +304,9 @@ static void testOverlap(CuTest *tc) {
 
 static void assertExample(CuTest *tc, const char *regexp, const char *exp) {
     struct fa *fa = make_good_fa(tc, regexp);
-    char *xmpl = fa_example(fa);
+    size_t xmpl_len;
+    char *xmpl;
+    fa_example(fa, &xmpl, &xmpl_len);
     CuAssertStrEquals(tc, exp, xmpl);
     free(xmpl);
 }
@@ -328,12 +330,15 @@ static void testExample(CuTest *tc) {
     assertExample(tc, "\001((\001.)*\002)+\002", "\001\002\002");
 
     struct fa *fa1 = mark(fa_make_basic(FA_EMPTY));
-    CuAssertPtrEquals(tc, NULL, fa_example(fa1));
+    size_t xmpl_len;
+    char *xmpl;
+    fa_example(fa1, &xmpl, &xmpl_len);
+    CuAssertPtrEquals(tc, NULL, xmpl);
 
     fa1 = mark(fa_make_basic(FA_EPSILON));
-    char *s = fa_example(fa1);
-    CuAssertStrEquals(tc, "", s);
-    free(s);
+    fa_example(fa1, &xmpl, &xmpl_len);
+    CuAssertStrEquals(tc, "", xmpl);
+    free(xmpl);
 }
 
 static void assertAmbig(CuTest *tc, const char *regexp1, const char *regexp2,
@@ -457,7 +462,9 @@ int main(int argc, char **argv) {
             print_regerror(r, argv[i]);
         } else {
             dot(fa);
-            char *s = fa_example(fa);
+            size_t s_len;
+            char *s;
+            fa_example(fa, &s, &s_len);
             printf("Example for %s: %s\n", argv[i], s);
             free(s);
             char *re;
-- 
1.6.0.6



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]