diff options
Diffstat (limited to 'lua-squarkdb.c')
-rw-r--r-- | lua-squarkdb.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/lua-squarkdb.c b/lua-squarkdb.c index dbac6d0..09d4afe 100644 --- a/lua-squarkdb.c +++ b/lua-squarkdb.c @@ -227,7 +227,7 @@ static int Lsqdb_map_strings(lua_State *L) { struct sqdb *db; const char *str; - char *ptr; + unsigned char *ptr; size_t len, total, pos; db = Lsqdb_checkarg(L, 1); @@ -238,7 +238,9 @@ static int Lsqdb_map_strings(lua_State *L) lua_pushnil(L); while (lua_next(L, 2) != 0) { str = luaL_checklstring(L, -2, &len); - total += len + 1; + total += len; + if (len >= (1 << SQDB_LENGTH_BITS)) + total++; lua_pop(L, 1); } @@ -252,15 +254,20 @@ static int Lsqdb_map_strings(lua_State *L) lua_pushnil(L); while (lua_next(L, 2) != 0) { str = lua_tolstring(L, -2, &len); - memcpy(&ptr[pos], str, len + 1); lua_pop(L, 1); - /* table[key] = pos */ + /* table[key] = encoded_string_pointer */ lua_pushvalue(L, -1); - lua_pushinteger(L, pos); - lua_rawset(L, 2); + if (len >= (1 << SQDB_LENGTH_BITS)) { + lua_pushinteger(L, pos << SQDB_LENGTH_BITS); + ptr[pos++] = len; + } else { + lua_pushinteger(L, (pos << SQDB_LENGTH_BITS) + len); + } + memcpy(&ptr[pos], str, len); + pos += len; - pos += len + 1; + lua_rawset(L, 2); } return 0; |