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

David Lutterkort lutter at redhat.com
Mon Mar 16 03:08:52 UTC 2009


---
 src/fa.c       |   18 +++++++++++++++---
 src/fa.h       |    9 ++++++++-
 src/lens.c     |    3 ++-
 tests/fatest.c |   14 +++++++++-----
 4 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/src/fa.c b/src/fa.c
index 01c36b5..afe907c 100644
--- a/src/fa.c
+++ b/src/fa.c
@@ -2293,10 +2293,19 @@ static struct fa *expand_alphabet(struct fa *fa, int add_marker,
 /* This algorithm is due to Anders Moeller, and can be found in class
  * AutomatonOperations in dk.brics.grammar
  */
-char *fa_ambig_example(struct fa *fa1, struct fa *fa2, char **pv, char **v) {
+int fa_ambig_example(struct fa *fa1, struct fa *fa2,
+                     char **upv, size_t *upv_len,
+                     char **pv, char **v) {
     static const char X = '\001';
     static const char Y = '\002';
 
+    *upv = NULL;
+    *upv_len = 0;
+    if (pv != NULL)
+        *pv = NULL;
+    if (v != NULL)
+        *v = NULL;
+
 #define Xs "\001"
 #define Ys "\002"
 #define MPs Ys Xs "(" Xs "(.|\n))+"
@@ -2349,7 +2358,7 @@ char *fa_ambig_example(struct fa *fa1, struct fa *fa2, char **pv, char **v) {
     fa_free(amb);
 
     if (s == NULL)
-        return NULL;
+        return -1;
 
     char *result, *t;
     CALLOC(result, (strlen(s)-1)/2 + 1);
@@ -2371,7 +2380,10 @@ char *fa_ambig_example(struct fa *fa1, struct fa *fa2, char **pv, char **v) {
         *t++ = s[2*i + 1];
 
     free(s);
-    return result;
+    *upv = result;
+    if (result != NULL)
+        *upv_len = strlen(result);
+    return 0;
 }
 
 /*
diff --git a/src/fa.h b/src/fa.h
index 477ad0d..5b0e4f1 100644
--- a/src/fa.h
+++ b/src/fa.h
@@ -173,8 +173,15 @@ int fa_example(struct fa *fa, char **example, size_t *example_len);
  * Neither the language of FA1 or of FA2 may contain words with the
  * characters '\001' and '\002', as they are used during construction of
  * the ambiguous word.
+ *
+ * UPV_LEN will be set to the length of the entire string UPV
+ *
+ * Returns 0 on success, and a negative number on failure. On failure, UPV,
+ * PV, and V will be NULL
  */
-char *fa_ambig_example(struct fa *fa1, struct fa *fa2, char **pv, char **v);
+int fa_ambig_example(struct fa *fa1, struct fa *fa2,
+                     char **upv, size_t *upv_len,
+                     char **pv, char **v);
 
 /* Convert the finite automaton FA into a regular expression and set REGEXP
  * to point to that. When REGEXP is compiled into another automaton, it is
diff --git a/src/lens.c b/src/lens.c
index 5757b3f..07f44cb 100644
--- a/src/lens.c
+++ b/src/lens.c
@@ -413,7 +413,8 @@ static struct value *typecheck_union(struct info *info,
 static struct value *ambig_check(struct info *info, struct fa *fa1, struct fa *fa2,
                                  const char *msg) {
     char *upv, *pv, *v;
-    upv = fa_ambig_example(fa1, fa2, &pv, &v);
+    size_t upv_len;
+    fa_ambig_example(fa1, fa2, &upv, &upv_len, &pv, &v);
     struct value *exn = NULL;
 
     if (upv != NULL) {
diff --git a/tests/fatest.c b/tests/fatest.c
index 19593ae..af8e2d2 100644
--- a/tests/fatest.c
+++ b/tests/fatest.c
@@ -163,10 +163,11 @@ static void testMonster(CuTest *tc) {
 
     struct fa *fa, *fas;
     char *upv, *pv, *v;
+    size_t upv_len;
 
     fa = make_good_fa(tc, monster);
 
-    upv = fa_ambig_example(fa, fa, &pv, &v);
+    fa_ambig_example(fa, fa, &upv, &upv_len, &pv, &v);
 
     /* Monster can't be concatenated with itself */
     CuAssertStrEquals(tc, "AAA", upv);
@@ -185,7 +186,7 @@ static void testMonster(CuTest *tc) {
        incidental details like sorting of transitions.
      */
     fa_minimize(fas);
-    upv = fa_ambig_example(fas, fa, &pv, &v);
+    fa_ambig_example(fas, fa, &upv, &upv_len, &pv, &v);
 
     CuAssertStrEquals(tc, "AA", upv);
     CuAssertStrEquals(tc, "AA", pv);
@@ -347,8 +348,9 @@ static void assertAmbig(CuTest *tc, const char *regexp1, const char *regexp2,
 
     struct fa *fa1 = make_good_fa(tc, regexp1);
     struct fa *fa2 = make_good_fa(tc, regexp2);
-    char *pv, *v;
-    char *upv = fa_ambig_example(fa1, fa2, &pv, &v);
+    char *upv, *pv, *v;
+    size_t upv_len;
+    fa_ambig_example(fa1, fa2, &upv, &upv_len, &pv, &v);
     CuAssertPtrNotNull(tc, upv);
     CuAssertPtrNotNull(tc, pv);
     CuAssertPtrNotNull(tc, v);
@@ -363,7 +365,9 @@ static void assertNotAmbig(CuTest *tc, const char *regexp1,
                            const char *regexp2) {
     struct fa *fa1 = make_good_fa(tc, regexp1);
     struct fa *fa2 = make_good_fa(tc, regexp2);
-    char *upv = fa_ambig_example(fa1, fa2, NULL, NULL);
+    char *upv;
+    size_t upv_len;
+    fa_ambig_example(fa1, fa2, &upv, &upv_len, NULL, NULL);
     CuAssertPtrEquals(tc, NULL, upv);
 }
 
-- 
1.6.0.6




More information about the augeas-devel mailing list