{"id":522,"date":"2022-04-23T16:42:41","date_gmt":"2022-04-23T08:42:41","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=522"},"modified":"2022-09-06T16:43:50","modified_gmt":"2022-09-06T08:43:50","slug":"lua%e4%b8%ad%e5%8f%b7%e6%98%af%e6%80%8e%e4%b9%88%e8%ae%a1%e7%ae%97%e5%ad%97%e7%ac%a6%e4%b8%b2%e9%95%bf%e5%ba%a6%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=522","title":{"rendered":"lua\u4e2d#\u53f7\u662f\u600e\u4e48\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u7684"},"content":{"rendered":"<h1>1. \u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50<\/h1>\n<pre><code class=\"line-numbers\"> a = \"abc\"\n print(#a)  \/\/ 3\n\n<\/code><\/pre>\n<h1>\u5f53\u83b7\u53d6\u7684\u662f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u65f6\u76f4\u63a5\u83b7\u53d6\u5b57\u7b26\u4e32\u7684len<\/h1>\n<pre><code class=\"line-numbers\">    setnvalue(ra, cast_num(tsvalue(rb)-&gt;len));\n\n<\/code><\/pre>\n<h1>\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5b57\u7b26\u4e32\u662f\u600e\u4e48\u6837\u7684\u7ed3\u6784<\/h1>\n<pre><code class=\"line-numbers\">typedef union TString\n{\n    struct\n    {\n        CommonHeader;\n        lu_byte reserved;  \/\/ \u8bbe\u7f6e\u8fd9\u4e2a\u662f\u4e2a\u4fdd\u7559\u5b57\u7684\u6807\u8bb0\u4f4d, \u503c\u4f1a\u662fgafqX_tokens\u7684\u7684\u7b2c\u51e0\u4e2a + 1\n        unsigned int hash; \/\/ \u52a0\u5feb\u67e5\u627e\u5339\u914d\n        size_t len;        \/\/ \u4e0d\u7528\\0\u7ed3\u5c3e,\u6240\u4ee5\u9700\u8981\u957f\u5ea6\n    } tsv;\n} TString;\n\n<\/code><\/pre>\n<h1>\u8fd9\u91cc\u56de\u5fc6\u4e00\u4e0b\u5b57\u7b26\u4e32\u662f\u600e\u4e48\u521b\u5efa\u7684<\/h1>\n<pre><code class=\"line-numbers\">TString *gafqS_newlstr(gafq_State *L, const char *str, size_t l)\n{\n    GCObject *o;\n    unsigned int h = cast(unsigned int, l);\n    size_t step = (l &gt;&gt; 5) + 1;\n    size_t l1;\n    for (l1 = l; l1 &gt;= step; l1 -= step) \/\/ \u8ba1\u7b97\u65b0\u5b57\u7b26\u4e32\u7684\u54c8\u5e0c\u503c\n        h = h ^ ((h &lt;&lt; 5) + (h &gt;&gt; 2) + cast(unsigned char, str[l1 - 1]));\n    \/\/ lua\u628a\u5b57\u7b26\u4e32\u653e\u5728strt\u4e2d \u53d6\u51fa\u54c8\u5e0c\u503c\u76f8\u540c\u7684\u94fe\u8868\n    for (o = G(L)-&gt;strt.hash[lmod(h, G(L)-&gt;strt.size)]; o != NULL; o = o-&gt;gch.next)\n    {\n        TString *ts = rawgco2ts(o);\n        if (ts-&gt;tsv.len == l &amp;&amp; (memcmp(str, getstr(ts), l) == 0))\n        {\n            if (isdead(G(L), o))\n                changewhite(o);\n            return ts;\n        }\n    }\n    return newlstr(L, str, l, h); \/\/ \u4e0d\u5b58\u5728\u521b\u5efa\u65b0\u7684\u5b57\u7b26\u4e32\n}\n\n\n\/\/\u5982\u679c\u5728\u5168\u5c40\u7684strt\u4e2d\u7684hash\u6ca1\u627e\u5230\u5b57\u7b26\u4e32,\u4f1a\u521b\u5efa\u4e2a\u65b0\u7684\nstatic TString *newlstr(gafq_State *L, const char *str, size_t l, unsigned int h)\n{\n    TString *ts;\n    stringtable *tb;\n    if (l + 1 &gt; (MAX_SIZET - sizeof(TString)) \/ sizeof(char))\n        gafqM_toobig(L);\n    ts = cast(TString *, gafqM_malloc(L, (l + 1) * sizeof(char) + sizeof(TString)));\n    ts-&gt;tsv.len = l;\n    ts-&gt;tsv.hash = h;\n    ts-&gt;tsv.marked = gafqC_white(G(L));\n    ts-&gt;tsv.tt = GAFQ_TSTRING;\n    ts-&gt;tsv.reserved = 0;\n    memcpy(ts + 1, str, l * sizeof(char));\n    ((char *)(ts + 1))[l] = '\\0';\n    tb = &amp;G(L)-&gt;strt;  \/\/ \u6253\u7b97\u628a\u65b0\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u653e\u5165strt\u4e2d\n    h = lmod(h, tb-&gt;size);\n    ts-&gt;tsv.next = tb-&gt;hash[h];\n    tb-&gt;hash[h] = obj2gco(ts);\n    tb-&gt;nuse++;\n    if (tb-&gt;nuse &gt; cast(lu_int32, tb-&gt;size) &amp;&amp; tb-&gt;size &lt;= MAX_INT \/ 2)\n        gafqS_resize(L, tb-&gt;size * 2);\n    return ts;\n}\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50 a = &#8220;abc&#8221; print(#a) \/\/ 3 \u5f53\u83b7\u53d6\u7684\u662f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u65f6\u76f4\u63a5\u83b7\u53d6\u5b57\u7b26\u4e32<a href=\"https:\/\/www.ccagml.com\/?p=522\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">lua\u4e2d#\u53f7\u662f\u600e\u4e48\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u7684<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/522"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=522"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":523,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/522\/revisions\/523"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}