feat: add loose string generation/lookup
all lowercase letters are simply converted to capital
This commit is contained in:
parent
ca1293aa8f
commit
29d80f211d
BIN
streecmp.a
Normal file
BIN
streecmp.a
Normal file
Binary file not shown.
41
streecmp.c
41
streecmp.c
|
@ -51,31 +51,35 @@ struct nod *mknod(struct nod *nod, int loc) {
|
||||||
return nod->pool[loc];
|
return nod->pool[loc];
|
||||||
}
|
}
|
||||||
|
|
||||||
int mkstr(struct nod *nod, char *str, int val) {
|
int mkstr(struct nod *nod, char *str, int val, int flag) {
|
||||||
struct nod *target = nod->pool[*str];
|
char ch = (flag & LOOSE_CHECK) &&
|
||||||
|
(*str >= 'a') &&
|
||||||
|
(*str <= 'z') ? *str-32 : *str;
|
||||||
|
|
||||||
|
struct nod *target = nod->pool[ch];
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
if (*str == '\0') {
|
if (*str == '\0') {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return mkstr(target, str+1, val);
|
return mkstr(target, str+1, val, flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct nod *new = mknod(nod, *str);
|
struct nod *new = mknod(nod, ch);
|
||||||
if (new == NULL) {
|
if (new == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
new->val.cval = *str;
|
new->val.cval = ch;
|
||||||
if (*str != '\0') {
|
if (*str != '\0') {
|
||||||
return mkstr(new, str+1, val);
|
return mkstr(new, str+1, val, flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
new->val.ival = val;
|
new->val.ival = val;
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gentree(struct nod *nod, char *strs, char *delim) {
|
int gentree(struct nod *nod, char *strs, char *delim, int flag) {
|
||||||
strs_cnt = 1;
|
strs_cnt = 1;
|
||||||
|
|
||||||
char *strs_cpy = strdup(strs);
|
char *strs_cpy = strdup(strs);
|
||||||
|
@ -89,7 +93,7 @@ int gentree(struct nod *nod, char *strs, char *delim) {
|
||||||
|
|
||||||
for (char *tok = strtok(strs_cpy, delim); tok != NULL; tok
|
for (char *tok = strtok(strs_cpy, delim); tok != NULL; tok
|
||||||
= strtok(NULL, delim)) {
|
= strtok(NULL, delim)) {
|
||||||
int ret = mkstr(nod, tok, strs_cnt);
|
int ret = mkstr(nod, tok, strs_cnt, flag);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto _err_mkstr;
|
goto _err_mkstr;
|
||||||
}
|
}
|
||||||
|
@ -105,8 +109,12 @@ _err:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int streecmp(struct nod *nod, char *str) {
|
int streecmp(struct nod *nod, char *str, int flag) {
|
||||||
struct nod *target = nod->pool[*str];
|
char ch = (flag & LOOSE_CHECK) &&
|
||||||
|
(*str >= 'a') &&
|
||||||
|
(*str <= 'z') ? *str-32 : *str;
|
||||||
|
|
||||||
|
struct nod *target = nod->pool[ch];
|
||||||
if (target == NULL) {
|
if (target == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -115,21 +123,22 @@ int streecmp(struct nod *nod, char *str) {
|
||||||
return target->val.ival;
|
return target->val.ival;
|
||||||
}
|
}
|
||||||
|
|
||||||
return streecmp(target, str+1);
|
return streecmp(target, str+1, flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
int streencmp(struct nod *nod, char *str, int len) {
|
int streencmp(struct nod *nod, char *str, int len, int flag) {
|
||||||
struct nod *nptr = nod;
|
struct nod *nptr = nod;
|
||||||
char *sptr = str;
|
for (char *sptr = str; sptr < str+len; sptr++) {
|
||||||
|
char ch = (flag & LOOSE_CHECK) &&
|
||||||
|
(*sptr >= 'a') &&
|
||||||
|
(*sptr <= 'z') ? *sptr-32 : *sptr;
|
||||||
|
|
||||||
for (int i = 0; i < len; i++) {
|
struct nod *target = nptr->pool[ch];
|
||||||
struct nod *target = nptr->pool[*sptr];
|
|
||||||
if (target == NULL) {
|
if (target == NULL) {
|
||||||
goto _not_found;
|
goto _not_found;
|
||||||
}
|
}
|
||||||
|
|
||||||
nptr = target;
|
nptr = target;
|
||||||
sptr++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nptr = nptr->pool['\0'];
|
nptr = nptr->pool['\0'];
|
||||||
|
|
10
streecmp.h
10
streecmp.h
|
@ -4,6 +4,8 @@
|
||||||
#define POOL_SIZE 256
|
#define POOL_SIZE 256
|
||||||
#define DEF_DELIM "\n"
|
#define DEF_DELIM "\n"
|
||||||
|
|
||||||
|
#define LOOSE_CHECK 0b00000001
|
||||||
|
|
||||||
struct nodval {
|
struct nodval {
|
||||||
int ival;
|
int ival;
|
||||||
char cval;
|
char cval;
|
||||||
|
@ -18,9 +20,9 @@ struct nod {
|
||||||
void frenod(struct nod *nod);
|
void frenod(struct nod *nod);
|
||||||
struct nod *allocnod(void);
|
struct nod *allocnod(void);
|
||||||
struct nod *mknod(struct nod *nod, int loc);
|
struct nod *mknod(struct nod *nod, int loc);
|
||||||
int mkstr(struct nod *nod, char *str, int val);
|
int mkstr(struct nod *nod, char *str, int val, int flag);
|
||||||
int gentree(struct nod *nod, char *strs, char *delim);
|
int gentree(struct nod *nod, char *strs, char *delim, int flag);
|
||||||
int streecmp(struct nod *nod, char *str);
|
int streecmp(struct nod *nod, char *str, int flag);
|
||||||
int streencmp(struct nod *nod, char *str, int len);
|
int streencmp(struct nod *nod, char *str, int len, int flag);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
4
test.c
4
test.c
|
@ -127,7 +127,7 @@ int main(void) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = gentree(rot, strs, NULL);
|
ret = gentree(rot, strs, NULL, LOOSE_CHECK);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "[-] Failed: could not generate tree\n");
|
fprintf(stderr, "[-] Failed: could not generate tree\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -138,7 +138,7 @@ int main(void) {
|
||||||
int cnt = 1;
|
int cnt = 1;
|
||||||
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
|
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
|
||||||
= strtok(NULL, "\n")) {
|
= strtok(NULL, "\n")) {
|
||||||
int found = streecmp(rot, tok);
|
int found = streecmp(rot, tok, LOOSE_CHECK);
|
||||||
if (found == cnt) {
|
if (found == cnt) {
|
||||||
fprintf(stdout, "[+][%d/%d] Success: could find"
|
fprintf(stdout, "[+][%d/%d] Success: could find"
|
||||||
" '%s' in tree\n", cnt, found, tok);
|
" '%s' in tree\n", cnt, found, tok);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user