]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - correct.c
Get rid of correct_strndup and correct_concat altogether, reduces each generated...
[xonotic/gmqcc.git] / correct.c
index ded5144656d2a8a7585546bae3558f17d32b1d59..3a4f5c029cf55e125120e067c676881b8a24f0d8 100644 (file)
--- a/correct.c
+++ b/correct.c
@@ -93,32 +93,6 @@ 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;
-
-    if (!(nstr = mem_a(lim + 1)))
-        return NULL;
-
-    strncpy(nstr, str+off, lim);
-    nstr[lim] = '\0';
-
-    return nstr;
-}
-
-static char *correct_concat(char *str1, char *str2) {
-    if (!str1) str1 = mem_a(1), *str1 = '\0';
-    if (!str2) str2 = mem_a(1), *str2 = '\0';
-
-    str1 = mem_r(str1, strlen(str1) + strlen(str2) + 1);
-    strcat(str1, str2);
-
-    return str1;
-}
 
 /*
  * correcting logic for the following forms of transformations:
@@ -132,10 +106,10 @@ static size_t correct_deletion(const char *ident, char **array, size_t index) {
     size_t len = strlen(ident);
 
     for (itr = 0; itr < len; itr++) {
-        array[index + itr] = correct_concat (
-            correct_substr (ident, 0,     itr),
-            correct_substr (ident, itr+1, len-(itr+1))
-        );
+        char *a = (char*)mem_a(len+1);
+        memcpy(a, ident, itr);
+        memcpy(a + itr, ident + itr + 1, len - itr);
+        array[index + itr] = a;
     }
 
     return itr;
@@ -146,16 +120,13 @@ static size_t correct_transposition(const char *ident, char **array, size_t inde
     size_t len = strlen(ident);
 
     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_concat (
-                correct_substr(ident, itr,   1),
-                correct_substr(ident, itr+2, len-(itr+2))
-            )
-        );
+        char  tmp;
+        char *a = (char*)mem_a(len+1);
+        memcpy(a, ident, len+1);
+        tmp      = a[itr];
+        a[itr  ] = a[itr+1];
+        a[itr+1] = tmp;
+        array[index + itr] = a;
     }
 
     return itr;
@@ -166,22 +137,13 @@ static size_t correct_alteration(const char *ident, char **array, size_t index)
     size_t jtr;
     size_t ktr;
     size_t len    = strlen(ident);
-    char   cct[2] = { 0, 0 }; /* char code table, for concatenation */
 
     for (itr = 0, ktr = 0; itr < len; itr++) {
-        for (jtr = 0; jtr < sizeof(correct_alpha); jtr++, ktr++) {
-            *cct = correct_alpha[jtr];
-            array[index + ktr] = correct_concat (
-                correct_concat (
-                    correct_substr(ident, 0, itr),
-                    (char *) &cct
-                ),
-                correct_substr (
-                    ident,
-                    itr + 1,
-                    len - (itr + 1)
-                )
-            );
+        for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
+            char *a = (char*)mem_a(len+1);
+            memcpy(a, ident, len+1);
+            a[itr] = correct_alpha[jtr];
+            array[index + ktr] = a;
         }
     }
 
@@ -193,22 +155,14 @@ static size_t correct_insertion(const char *ident, char **array, size_t index) {
     size_t jtr;
     size_t ktr;
     size_t len    = strlen(ident);
-    char   cct[2] = { 0, 0 }; /* char code table, for concatenation */
 
     for (itr = 0, ktr = 0; itr <= len; itr++) {
-        for (jtr = 0; jtr < sizeof(correct_alpha); jtr++, ktr++) {
-            *cct = correct_alpha[jtr];
-            array[index + ktr] = correct_concat (
-                correct_concat (
-                    correct_substr (ident, 0, itr),
-                    (char *) &cct
-                ),
-                correct_substr (
-                    ident,
-                    itr,
-                    len - itr
-                )
-            );
+        for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
+            char *a = (char*)mem_a(len+2);
+            memcpy(a, ident, itr);
+            a[itr] = correct_alpha[jtr];
+            memcpy(a + itr + 1, ident + itr, len - itr + 1);
+            array[index + ktr] = a;
         }
     }
 
@@ -224,12 +178,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)-1)) + ((len + 1) * (sizeof(correct_alpha)-1));
 }
 
 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 +226,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 +258,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 +283,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 +328,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();
-
-}