惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

ccagml

使用valgrind观察luajit进程内存 - ccagml 从生产环境报错学习protobuf编码规则 - ccagml 跟着vscode插件学设计模式-工厂模式 - ccagml 拨开迷雾,探寻深夜游戏集群启动失败真相 - ccagml 从技能状态图标显示错误到给 LuaJIT 报告bug - ccagml 游戏系统MySQl执行超时问题排查 - ccagml lua中pairs和ipairs都做了什么操作 - ccagml lua中#号是怎么计算长度的 - ccagml lua中tonumber做了什么 - ccagml
lua中#号是怎么计算字符串长度的 - ccagml
2022-04-23 · via ccagml

1. 一些简单的例子

 a = "abc"
 print(#a)  // 3

当获取的是字符串的长度时直接获取字符串的len

    setnvalue(ra, cast_num(tsvalue(rb)->len));

我们需要了解字符串是怎么样的结构

typedef union TString
{
    struct
    {
        CommonHeader;
        lu_byte reserved;  // 设置这个是个保留字的标记位, 值会是gafqX_tokens的的第几个 + 1
        unsigned int hash; // 加快查找匹配
        size_t len;        // 不用\0结尾,所以需要长度
    } tsv;
} TString;

这里回忆一下字符串是怎么创建的

TString *gafqS_newlstr(gafq_State *L, const char *str, size_t l)
{
    GCObject *o;
    unsigned int h = cast(unsigned int, l);
    size_t step = (l >> 5) + 1;
    size_t l1;
    for (l1 = l; l1 >= step; l1 -= step) // 计算新字符串的哈希值
        h = h ^ ((h << 5) + (h >> 2) + cast(unsigned char, str[l1 - 1]));
    // lua把字符串放在strt中 取出哈希值相同的链表
    for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; o != NULL; o = o->gch.next)
    {
        TString *ts = rawgco2ts(o);
        if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
        {
            if (isdead(G(L), o))
                changewhite(o);
            return ts;
        }
    }
    return newlstr(L, str, l, h); // 不存在创建新的字符串
}


//如果在全局的strt中的hash没找到字符串,会创建个新的
static TString *newlstr(gafq_State *L, const char *str, size_t l, unsigned int h)
{
    TString *ts;
    stringtable *tb;
    if (l + 1 > (MAX_SIZET - sizeof(TString)) / sizeof(char))
        gafqM_toobig(L);
    ts = cast(TString *, gafqM_malloc(L, (l + 1) * sizeof(char) + sizeof(TString)));
    ts->tsv.len = l;
    ts->tsv.hash = h;
    ts->tsv.marked = gafqC_white(G(L));
    ts->tsv.tt = GAFQ_TSTRING;
    ts->tsv.reserved = 0;
    memcpy(ts + 1, str, l * sizeof(char));
    ((char *)(ts + 1))[l] = '\0';
    tb = &G(L)->strt;  // 打算把新创建的字符串放入strt中
    h = lmod(h, tb->size);
    ts->tsv.next = tb->hash[h];
    tb->hash[h] = obj2gco(ts);
    tb->nuse++;
    if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT / 2)
        gafqS_resize(L, tb->size * 2);
    return ts;
}