* 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 (!str1) {
+ str1 = mem_a(1);
+ *str1 = '\0';
+ }
- str1 = mem_r(str1, strlen(str1) + strlen(str2) + 1);
- strcat(str1, str2);
+ str1 = mem_r (str1, strlen(str1) + strlen(str2) + 1);
+ ret = strcat(str1, str2);
- return str1;
+ if (str2 && next)
+ mem_d(str2);
+
+ return ret;
}
/*
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
);
}
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
);
}
*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 + 1,
+ correct_strndup (
+ ident+itr+1,
len - (itr + 1)
- )
+ ),
+ true
);
}
}
*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
);
}
}
*/
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;
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))
correct_add(t, &d, "world");
printf("found identifiers: (2)\n");
- printf(" 1: hello\n");
- printf(" 2: world\n");
+ printf(" 1: hellobain\n");
+ printf(" 2: ellor\n");
+ printf(" 3: world\n");
char *b = correct_correct(t, "rld");
char *a = correct_correct(t, "ello");
+ char *c = correct_correct(t, "helbain");
- printf("%s, did you mean `%s` ?\n", "ello", a);
- printf("%s, did you mean `%s` ?\n", "rld", b);
+ printf("invalid identifier: `%s` (did you mean: `%s`?)\n", "ello", a);
+ printf("invalid identifier: `%s` (did you mean: `%s`?)\n", "rld", b);
+ printf("invalid identifier: `%s` (did you mean: `%s`?)\n", "helbain", c);
correct_del(t, d);
mem_d(b);
mem_d(a);
+ mem_d(c);
- util_meminfo();
+ /*util_meminfo();*/
}