2013-10-04 14:53:11 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
static int qstrs_alloc;
|
|
|
|
static int qstrs_len;
|
|
|
|
static const char **qstrs;
|
|
|
|
|
2013-10-23 15:20:17 -04:00
|
|
|
void qstr_init(void) {
|
2013-10-04 14:53:11 -04:00
|
|
|
qstrs_alloc = 400;
|
|
|
|
qstrs_len = 1;
|
|
|
|
qstrs = m_new(const char*, qstrs_alloc);
|
|
|
|
qstrs[0] = "nil";
|
|
|
|
}
|
|
|
|
|
|
|
|
static qstr qstr_add(const char *str) {
|
|
|
|
if (qstrs_len >= qstrs_alloc) {
|
2013-12-29 14:33:23 -05:00
|
|
|
qstrs = m_renew(const char*, qstrs, qstrs_alloc, qstrs_alloc * 2);
|
2013-10-04 14:53:11 -04:00
|
|
|
qstrs_alloc *= 2;
|
|
|
|
}
|
|
|
|
qstrs[qstrs_len++] = str;
|
|
|
|
return qstrs_len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
qstr qstr_from_str_static(const char *str) {
|
|
|
|
for (int i = 0; i < qstrs_len; i++) {
|
|
|
|
if (strcmp(qstrs[i], str) == 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return qstr_add(str);
|
|
|
|
}
|
|
|
|
|
2013-12-29 14:33:23 -05:00
|
|
|
qstr qstr_from_str_take(char *str, int alloc_len) {
|
2013-10-04 14:53:11 -04:00
|
|
|
for (int i = 0; i < qstrs_len; i++) {
|
|
|
|
if (strcmp(qstrs[i], str) == 0) {
|
2013-12-29 14:33:23 -05:00
|
|
|
m_del(char, str, alloc_len);
|
2013-10-04 14:53:11 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return qstr_add(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
qstr qstr_from_strn_copy(const char *str, int len) {
|
|
|
|
for (int i = 0; i < qstrs_len; i++) {
|
|
|
|
if (strncmp(qstrs[i], str, len) == 0 && qstrs[i][len] == '\0') {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return qstr_add(strndup(str, len));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *qstr_str(qstr qstr) {
|
|
|
|
return qstrs[qstr];
|
|
|
|
}
|