]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - correct.c
Remove constant string literal for correction check, use parse_tokval of the current...
[xonotic/gmqcc.git] / correct.c
index ded5144656d2a8a7585546bae3558f17d32b1d59..e32678e754e3770ebb5d142d06a9fc7c0a283ec4 100644 (file)
--- a/correct.c
+++ b/correct.c
@@ -93,31 +93,38 @@ static int correct_update(ht *table, const char *word) {
  * alpha character.
  */
 static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
-static char *correct_substr(const char *str, size_t off, size_t lim) {
-    char   *nstr;
-    size_t  slen = strlen(str);
 
-    /* lots of compares */
-    if ((lim > slen) || ((off + lim) > slen) || (slen < 1) || (!lim))
-        return NULL;
+static char *correct_strndup(const char *src, size_t n) {
+    char   *ret;
+    size_t  len = strlen(src);
 
-    if (!(nstr = mem_a(lim + 1)))
-        return NULL;
+    if (n < len)
+        len = n;
 
-    strncpy(nstr, str+off, lim);
-    nstr[lim] = '\0';
+    if (!(ret = (char*)mem_a(len + 1)))
+        return NULL;
 
-    return nstr;
+    ret[len] = '\0';
+    return (char*)memcpy(ret, src, len);
 }
 
-static char *correct_concat(char *str1, char *str2) {
-    if (!str1) str1 = mem_a(1), *str1 = '\0';
-    if (!str2) str2 = mem_a(1), *str2 = '\0';
+static char *correct_concat(char *str1, char *str2, bool next) {
+    char *ret = NULL;
+
+#if 0
+    if (!str1) {
+         str1 = mem_a(1);
+        *str1 = '\0';
+    }
+#endif
+
+    str1 = mem_r (str1, strlen(str1) + strlen(str2) + 1);
+    ret  = strcat(str1, str2);
 
-    str1 = mem_r(str1, strlen(str1) + strlen(str2) + 1);
-    strcat(str1, str2);
+    if (str2 && next)
+        mem_d(str2);
 
-    return str1;
+    return ret;
 }
 
 /*
@@ -133,8 +140,9 @@ static size_t correct_deletion(const char *ident, char **array, size_t index) {
 
     for (itr = 0; itr < len; itr++) {
         array[index + itr] = correct_concat (
-            correct_substr (ident, 0,     itr),
-            correct_substr (ident, itr+1, len-(itr+1))
+            correct_strndup (ident,       itr),
+            correct_strndup (ident+itr+1, len-(itr+1)),
+            true
         );
     }
 
@@ -148,13 +156,16 @@ static size_t correct_transposition(const char *ident, char **array, size_t inde
     for (itr = 0; itr < len - 1; itr++) {
         array[index + itr] = correct_concat (
             correct_concat (
-                correct_substr(ident, 0,     itr),
-                correct_substr(ident, itr+1, 1)
+                correct_strndup(ident,     itr),
+                correct_strndup(ident+itr+1, 1),
+                true
             ),
             correct_concat (
-                correct_substr(ident, itr,   1),
-                correct_substr(ident, itr+2, len-(itr+2))
-            )
+                correct_strndup(ident+itr,   1),
+                correct_strndup(ident+itr+2, len-(itr+2)),
+                true
+            ),
+            true
         );
     }
 
@@ -173,14 +184,15 @@ static size_t correct_alteration(const char *ident, char **array, size_t index)
             *cct = correct_alpha[jtr];
             array[index + ktr] = correct_concat (
                 correct_concat (
-                    correct_substr(ident, 0, itr),
-                    (char *) &cct
+                    correct_strndup(ident, itr),
+                    (char *) &cct,
+                    false
+                ),
+                correct_strndup (
+                    ident + (itr+1),
+                    len   - (itr+1)
                 ),
-                correct_substr (
-                    ident,
-                    itr + 1,
-                    len - (itr + 1)
-                )
+                true
             );
         }
     }
@@ -200,14 +212,15 @@ static size_t correct_insertion(const char *ident, char **array, size_t index) {
             *cct = correct_alpha[jtr];
             array[index + ktr] = correct_concat (
                 correct_concat (
-                    correct_substr (ident, 0, itr),
-                    (char *) &cct
+                    correct_strndup (ident, itr),
+                    (char *) &cct,
+                    false
                 ),
-                correct_substr (
-                    ident,
-                    itr,
+                correct_strndup (
+                    ident+itr,
                     len - itr
-                )
+                ),
+                true
             );
         }
     }
@@ -224,12 +237,12 @@ static GMQCC_INLINE size_t correct_size(const char *ident) {
      */   
 
     register size_t len = strlen(ident);
-    return (len) + (len - 1) + (len * sizeof(correct_alpha)) + (len + 1) * sizeof(correct_alpha);
+    return (len) + (len - 1) + (len * sizeof(correct_alpha)) + ((len + 1) * sizeof(correct_alpha));
 }
 
 static char **correct_edit(const char *ident) {
     size_t next;
-    char **find = mem_a(correct_size(ident) * sizeof(char*));
+    char **find = (char**)mem_a(correct_size(ident) * sizeof(char*));
 
     if (!find)
         return NULL;
@@ -272,8 +285,12 @@ static char **correct_known(ht table, char **array, size_t rows, size_t *next) {
             if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) {
                 res        = mem_r(res, sizeof(char*) * (len + 1));
                 res[len++] = end[jtr];
+            } else {
+                mem_d(end[jtr]);
             }
         }
+
+        mem_d(end);
     }
 
     *next = len;
@@ -300,6 +317,8 @@ static void correct_cleanup(char **array, size_t rows) {
     size_t itr;
     for (itr = 0; itr < rows; itr++)
         mem_d(array[itr]);
+
+    mem_d(array);
 }
 
 /*
@@ -323,41 +342,39 @@ void correct_add(ht table, size_t ***size, const char *ident) {
     }
 }
 
-char *correct_correct(ht table, const char *ident) {
+char *correct_str(ht table, const char *ident) {
     char **e1;
     char **e2;
     char  *e1ident;
     char  *e2ident;
     char  *found = util_strdup(ident);
 
-    size_t e1rows;
-    size_t e2rows;
+    size_t e1rows = 0;
+    size_t e2rows = 0;
 
     /* needs to be allocated for free later */
     if (correct_find(table, ident))
         return found;
 
-    mem_d(found);
     if ((e1rows = correct_size(ident))) {
         e1      = correct_edit(ident);
 
         if ((e1ident = correct_maximum(table, e1, e1rows))) {
+            mem_d(found);
             found = util_strdup(e1ident);
             correct_cleanup(e1, e1rows);
-            mem_d(e1);
             return found;
         }
     }
 
     e2 = correct_known(table, e1, e1rows, &e2rows);
-    if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows))))
+    if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) {
+        mem_d(found);
         found = util_strdup(e2ident);
+    }
     
     correct_cleanup(e1, e1rows);
     correct_cleanup(e2, e2rows);
-
-    mem_d(e1);
-    mem_d(e2);
     
     return found;
 }
@@ -370,33 +387,3 @@ void correct_del(ht dictonary, size_t **data) {
     vec_free(data);
     util_htdel(dictonary);
 }
-
-int main() {
-    opts.debug = true;
-    opts.memchk = true;
-    con_init();
-
-    ht       t = util_htnew(1024);
-    size_t **d = NULL;
-
-    correct_add(t, &d, "hellobain");
-    correct_add(t, &d, "ellor");
-    correct_add(t, &d, "world");
-
-    printf("found identifiers: (2)\n");
-    printf(" 1: hello\n");
-    printf(" 2: world\n");
-
-    char *b = correct_correct(t, "rld");
-    char *a = correct_correct(t, "ello");
-
-    printf("%s, did you mean `%s` ?\n", "ello", a);
-    printf("%s, did you mean `%s` ?\n", "rld", b);
-
-    correct_del(t, d);
-    mem_d(b);
-    mem_d(a);
-
-    util_meminfo();
-
-}