2013-10-04 19:53:11 +01: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 20:20:17 +01:00
|
|
|
void qstr_init(void) {
|
2013-10-04 19:53:11 +01: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 19:33:23 +00:00
|
|
|
qstrs = m_renew(const char*, qstrs, qstrs_alloc, qstrs_alloc * 2);
|
2013-10-04 19:53:11 +01: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 19:33:23 +00:00
|
|
|
qstr qstr_from_str_take(char *str, int alloc_len) {
|
2013-10-04 19:53:11 +01:00
|
|
|
for (int i = 0; i < qstrs_len; i++) {
|
|
|
|
if (strcmp(qstrs[i], str) == 0) {
|
2013-12-29 19:33:23 +00:00
|
|
|
m_del(char, str, alloc_len);
|
2013-10-04 19:53:11 +01: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];
|
|
|
|
}
|