From 0b8c7a8ccbde78df13d68dfea813818eb87ec9e9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 1 Mar 2014 00:13:11 +0900 Subject: [PATCH] add pic_intern --- include/picrin.h | 1 + src/symbol.c | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 4557fa30..1a637f5c 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -160,6 +160,7 @@ void pic_defvar(pic_state *, const char *, pic_value); bool pic_equal_p(pic_state *, pic_value, pic_value); +pic_sym pic_intern(pic_state *, const char *, size_t); pic_sym pic_intern_cstr(pic_state *, const char *); const char *pic_symbol_name(pic_state *, pic_sym); pic_sym pic_gensym(pic_state *, pic_sym); diff --git a/src/symbol.c b/src/symbol.c index 7076f3ec..0ef3a868 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -10,24 +10,33 @@ #include "picrin/string.h" pic_sym -pic_intern_cstr(pic_state *pic, const char *str) +pic_intern(pic_state *pic, const char *str, size_t len) { + char *cstr; xh_entry *e; pic_sym id; - e = xh_get(pic->syms, str); + cstr = (char *)pic_malloc(pic, len + 1); + cstr[len] = '\0'; + memcpy(cstr, str, len); + + e = xh_get(pic->syms, cstr); if (e) { return e->val; } - str = pic_strdup(pic, str); - id = pic->sym_cnt++; - xh_put(pic->syms, str, id); - xh_put_int(pic->sym_names, id, (long)str); + xh_put(pic->syms, cstr, id); + xh_put_int(pic->sym_names, id, (long)cstr); return id; } +pic_sym +pic_intern_cstr(pic_state *pic, const char *str) +{ + return pic_intern(pic, str, strlen(str)); +} + pic_sym pic_gensym(pic_state *pic, pic_sym base) {