Пример #1
0
        /*
        ** checks whether short string exists and reuses it or creates a new one
        */
        static TString internshrstr(lua_State L, CharPtr str, uint l)
        {
            TString      ts;
            global_State g    = G(L);
            uint         h    = luaS_hash(str, l, g.seed);
            TStringRef   list = new TStringArrayRef(g.strt.hash, (int)lmod(h, g.strt.size));

            lua_assert(str != null);        /* otherwise 'memcmp'/'memcpy' are undefined */
            for (ts = list.get(); ts != null; ts = ts.u.hnext)
            {
                if (l == ts.shrlen &&
                    (memcmp(str, getstr(ts), l * 1 /*sizeof(char)*/) == 0))      //FIXME:sizeof(char)
                /* found! */
                {
                    if (isdead(g, ts))    /* dead (but not collected yet)? */
                    {
                        changewhite(ts);  /* resurrect it */
                    }
                    return(ts);
                }
            }
            if (g.strt.nuse >= g.strt.size && g.strt.size <= MAX_INT / 2)
            {
                luaS_resize(L, g.strt.size * 2);
                list = new TStringArrayRef(g.strt.hash, (int)lmod(h, g.strt.size));      /* recompute with new size */
            }
            ts = createstrobj(L, l, LUA_TSHRSTR, h);
            memcpy(getstr(ts), str, l * 1 /*sizeof(char)*/);
            ts.shrlen  = cast_byte(l);
            ts.u.hnext = list.get();
            list.set(ts);
            g.strt.nuse++;
            return(ts);
        }
Пример #2
0
        public static void luaS_remove(lua_State L, TString ts)
        {
            stringtable tb = G(L).strt;
            TStringRef  p  = new TStringArrayRef(tb.hash, (int)lmod(ts.hash, tb.size));

            while (p.get() != ts)        /* find previous element */
            {
                p = new TStringPtrRef(p.get());
            }
            p.set(p.get().u.hnext);        /* remove element from its list */
            tb.nuse--;
        }