Enhance r_str_const api, allowing to specify target cache

This commit is contained in:
pancake 2017-09-17 02:21:47 +02:00
parent 767cc96496
commit 6ddc76ef37
3 changed files with 31 additions and 17 deletions

View File

@ -1341,7 +1341,7 @@ beach:
r_core_fini (&r);
r_cons_set_raw (0);
free (file);
r_str_const_free ();
r_str_const_free (NULL);
r_cons_free ();
return ret;
}

View File

@ -105,8 +105,9 @@ typedef void(*str_operation)(char *c);
R_API int r_str_do_until_token(str_operation op, char *str, const char tok);
R_API void r_str_const_free(void);
R_API const char *r_str_const(const char *ptr);
R_API const char *r_str_const_at(char ***consts, const char *ptr);
R_API void r_str_const_free(char ***consts);
R_API void r_str_reverse(char *str);
R_API int r_str_re_match(const char *str, const char *reg);

View File

@ -2681,38 +2681,51 @@ R_API const char *r_str_pad(const char ch, int sz) {
return pad;
}
static char **consts = NULL;
static char **__consts = NULL;
R_API const char *r_str_const(const char *ptr) {
R_API const char *r_str_const_at(char ***consts, const char *ptr) {
if (!consts) {
consts = &__consts;
}
int ctr = 0;
if (!ptr) {
return NULL;
}
if (consts) {
if (*consts) {
const char *p;
while ((p = consts[ctr])) {
while ((p = (*consts)[ctr])) {
if (ptr == p || !strcmp (ptr, p)) {
return p;
}
ctr ++;
}
consts = realloc (consts, (2+ctr) * sizeof(void*));
char **res = realloc (*consts, (ctr + 2) * sizeof (void*));
if (res) {
*consts = res;
} else {
return NULL;
}
} else {
consts = malloc (sizeof (void*) * 2);
*consts = malloc (sizeof (void*) * 2);
}
consts[ctr] = strdup (ptr);
consts[ctr+1] = NULL;
return consts[ctr];
(*consts)[ctr] = strdup (ptr);
(*consts)[ctr + 1] = NULL;
return (*consts)[ctr];
}
R_API void r_str_const_free() {
R_API const char *r_str_const(const char *ptr) {
return r_str_const_at (&__consts, ptr);
}
R_API void r_str_const_free(char ***consts) {
int i;
if (consts) {
for (i = 0; consts[i]; i++) {
free (consts[i]);
}
R_FREE (consts);
if (!consts) {
consts = &__consts;
}
for (i = 0; (*consts)[i]; i++) {
free ((*consts)[i]);
}
R_FREE (*consts);
}
R_API char *r_str_between(const char *cmt, const char *prefix, const char *suffix) {