Enable NUL (`\0`) again.

parent 52507b10
...@@ -13,13 +13,13 @@ end ...@@ -13,13 +13,13 @@ end
%w[size length].each do |n| %w[size length].each do |n|
assert("Symbol##{n}") do assert("Symbol##{n}") do
assert_equal 5, :hello.__send__(n) assert_equal 5, :hello.__send__(n)
assert_equal 4, :"aA b".__send__(n) assert_equal 4, :"aA\0b".__send__(n)
if __ENCODING__ == "UTF-8" if __ENCODING__ == "UTF-8"
assert_equal 8, :"こんにちは世界!".__send__(n) assert_equal 8, :"こんにちは世界!".__send__(n)
assert_equal 4, :"aあ b".__send__(n) assert_equal 4, :"aあ\0b".__send__(n)
else else
assert_equal 22, :"こんにちは世界!".__send__(n) assert_equal 22, :"こんにちは世界!".__send__(n)
assert_equal 6, :"aあ b".__send__(n) assert_equal 6, :"aあ\0b".__send__(n)
end end
end end
end end
......
...@@ -16,13 +16,16 @@ ...@@ -16,13 +16,16 @@
#undef MRB_PRESYM_CSYM #undef MRB_PRESYM_CSYM
#undef MRB_PRESYM_QSYM #undef MRB_PRESYM_QSYM
#undef MRB_PRESYM_SYM #undef MRB_PRESYM_SYM
#define MRB_PRESYM_CSYM(sym, num) #sym, #define MRB_PRESYM_CSYM(sym, num) {#sym,sizeof(#sym)-1},
#define MRB_PRESYM_QSYM(str, name, num) str, #define MRB_PRESYM_QSYM(str, name, num) {str,sizeof(str)-1},
#define MRB_PRESYM_SYM(str, num) str, #define MRB_PRESYM_SYM(str, num) {str,sizeof(str)-1},
static const char *presym_table[] = { static const struct {
const char *name;
uint16_t len;
} presym_table[] = {
#include <../build/presym.inc> #include <../build/presym.inc>
NULL {0,0}
}; };
static mrb_sym static mrb_sym
...@@ -33,9 +36,17 @@ presym_find(const char *name, size_t len) ...@@ -33,9 +36,17 @@ presym_find(const char *name, size_t len)
while (start<=end) { while (start<=end) {
int mid = (start+end)/2; int mid = (start+end)/2;
int cmp = strncmp(name, presym_table[mid], len); size_t plen = presym_table[mid].len;
size_t tlen = (plen > len) ? len : plen;
int cmp;
cmp = memcmp(name, presym_table[mid].name, tlen);
if (cmp == 0) {
if (len > plen) cmp = 1;
else if (len < plen) cmp = -1;
}
if (cmp == 0 && presym_table[mid][len] == '\0') { if (cmp == 0) {
return mid+1; return mid+1;
} else if (cmp > 0) { } else if (cmp > 0) {
start = mid+1; start = mid+1;
...@@ -47,10 +58,11 @@ presym_find(const char *name, size_t len) ...@@ -47,10 +58,11 @@ presym_find(const char *name, size_t len)
} }
static const char* static const char*
presym_sym2name(mrb_sym sym) presym_sym2name(mrb_sym sym, mrb_int *lenp)
{ {
if (sym > MRB_PRESYM_MAX) return NULL; if (sym > MRB_PRESYM_MAX) return NULL;
return presym_table[sym-1]; if (lenp) *lenp = presym_table[sym-1].len;
return presym_table[sym-1].name;
} }
/* ------------------------------------------------------ */ /* ------------------------------------------------------ */
...@@ -307,11 +319,8 @@ sym2name_len(mrb_state *mrb, mrb_sym sym, char *buf, mrb_int *lenp) ...@@ -307,11 +319,8 @@ sym2name_len(mrb_state *mrb, mrb_sym sym, char *buf, mrb_int *lenp)
sym >>= SYMBOL_NORMAL_SHIFT; sym >>= SYMBOL_NORMAL_SHIFT;
{ {
const char *name = presym_sym2name(sym); const char *name = presym_sym2name(sym, lenp);
if (name) { if (name) return name;
if (lenp) *lenp = strlen(name);
return name;
}
} }
sym -= MRB_PRESYM_MAX; sym -= MRB_PRESYM_MAX;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment