From ca1293aa8f123adb91d80f687933ea5c596c114a Mon Sep 17 00:00:00 2001 From: 0xdeadbeer Date: Tue, 3 Sep 2024 21:01:26 +0200 Subject: [PATCH] fix: tiny refactor of mkstr, reset strs_cnt --- streecmp.c | 24 +++++++++++++----------- streecmp.h | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/streecmp.c b/streecmp.c index 6c904a4..5d8c5f0 100644 --- a/streecmp.c +++ b/streecmp.c @@ -4,7 +4,7 @@ #include #include "streecmp.h" -int strs_cnt = 0; +static int strs_cnt = 1; void frenod(struct nod *nod) { for (int i = 0; i < POOL_SIZE; i++) { @@ -51,14 +51,14 @@ struct nod *mknod(struct nod *nod, int loc) { return nod->pool[loc]; } -int mkstr(struct nod *nod, char *str) { +int mkstr(struct nod *nod, char *str, int val) { struct nod *target = nod->pool[*str]; if (target != NULL) { if (*str == '\0') { return 0; } - return mkstr(target, str+1); + return mkstr(target, str+1, val); } struct nod *new = mknod(nod, *str); @@ -68,15 +68,16 @@ int mkstr(struct nod *nod, char *str) { new->val.cval = *str; if (*str != '\0') { - return mkstr(new, str+1); + return mkstr(new, str+1, val); } - strs_cnt++; - new->val.ival = strs_cnt; - return strs_cnt; + new->val.ival = val; + return val; } int gentree(struct nod *nod, char *strs, char *delim) { + strs_cnt = 1; + char *strs_cpy = strdup(strs); if (strs_cpy == NULL) { goto _err; @@ -88,10 +89,11 @@ int gentree(struct nod *nod, char *strs, char *delim) { for (char *tok = strtok(strs_cpy, delim); tok != NULL; tok = strtok(NULL, delim)) { - int ret = mkstr(nod, tok); + int ret = mkstr(nod, tok, strs_cnt); if (ret < 0) { goto _err_mkstr; } + strs_cnt++; } free(strs_cpy); @@ -130,12 +132,12 @@ int streencmp(struct nod *nod, char *str, int len) { sptr++; } - struct nod *end = nptr->pool['\0']; - if (end == NULL) { + nptr = nptr->pool['\0']; + if (nptr == NULL) { goto _not_found; } - return end->val.ival; + return nptr->val.ival; _not_found: return 0; diff --git a/streecmp.h b/streecmp.h index 92a12a3..6d35e0d 100644 --- a/streecmp.h +++ b/streecmp.h @@ -18,7 +18,7 @@ struct nod { void frenod(struct nod *nod); struct nod *allocnod(void); struct nod *mknod(struct nod *nod, int loc); -int mkstr(struct nod *nod, char *str); +int mkstr(struct nod *nod, char *str, int val); int gentree(struct nod *nod, char *strs, char *delim); int streecmp(struct nod *nod, char *str); int streencmp(struct nod *nod, char *str, int len);