{"id":1655,"date":"2024-11-05T16:21:28","date_gmt":"2024-11-05T08:21:28","guid":{"rendered":"https:\/\/www.madbull.site\/?p=1655"},"modified":"2024-11-06T15:08:31","modified_gmt":"2024-11-06T07:08:31","slug":"jit%e5%8a%a0%e9%80%9fpcre%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=1655","title":{"rendered":"JIT\u52a0\u901f\u6b63\u5219\u5339\u914d"},"content":{"rendered":"\n<p>pcre\u57fa\u672c\u4f7f\u7528\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=1648\">https:\/\/www.madbull.site\/?p=1648<\/a> C\u8bed\u8a00\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5728 PCRE2 \u5e93\u4e2d\uff0cJIT \u7528\u4e8e\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u6027\u80fd\u3002\u901a\u8fc7 JIT \u7f16\u8bd1\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u88ab\u8f6c\u6362\u4e3a\u673a\u5668\u7801\uff0c\u4ece\u800c\u663e\u8457\u52a0\u5feb\u5339\u914d\u901f\u5ea6\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>1\u3001\u5728\u6a21\u5f0f\u7f16\u8bd1\u540e\uff0c\u542f\u52a8 JIT \u7f16\u8bd1\u529f\u80fd\uff1b<\/p>\n\n\n\n<p>2\u3001\u7533\u8bf7 JIT \u6808\uff0c\u5206\u914d\u7ed9\u5339\u914d\u4e0a\u4e0b\u6587\uff1b<\/p>\n\n\n\n<p>3\u3001\u4f7f\u7528\u5339\u914d\u4e0a\u4e0b\u6587\u5bf9\u88ab\u68c0\u6d4b\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6d4b\u8bd5\u4ee3\u7801 tpjit.c\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc -Wall -o test tpjit.c -lpcre2-8\n\n#ifndef PCRE2_CODE_UNIT_WIDTH\n#define PCRE2_CODE_UNIT_WIDTH 8\n#endif\n\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;pcre2.h&gt;\n\nint main() {\n    \/\/ 1\u3001\u521b\u5efa\u7f16\u8bd1\u4e0a\u4e0b\u6587\n    pcre2_compile_context *compile_context = pcre2_compile_context_create(NULL);\n\n    \/\/ \u8bbe\u7f6e\u6362\u884c\u7b26\u7c7b\u578b\u4e3a\u4efb\u4f55 Unicode \u6362\u884c\u7b26\n    int result = pcre2_set_newline(compile_context, PCRE2_NEWLINE_ANY);\n\n    \/\/ 2\u3001\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\n    const char *pattern = \"a?b(cd|gg)ef\";\n    int errorcode;\n    PCRE2_SIZE erroroffset;\n    pcre2_code *re_code = pcre2_compile((PCRE2_SPTR8)pattern, PCRE2_ZERO_TERMINATED, PCRE2_DOTALL|PCRE2_CASELESS, &amp;errorcode, &amp;erroroffset, compile_context);\n    if (!re_code) {\n        fprintf(stderr, \"Failed to compile pattern\\n\");\n        pcre2_compile_context_free(compile_context);\n        return 1;\n    }\n\n    \/\/ 3\u3001\u542f\u7528 JIT \u7f16\u8bd1\n    result = pcre2_jit_compile(re_code, PCRE2_JIT_COMPLETE);\n\n    \/\/ 4\u3001\u521b\u5efa JIT \u6808\n    pcre2_jit_stack *jit_stack = pcre2_jit_stack_create(128 * 1024, 1024 * 1024, NULL);\n\n    \/\/ 5\u3001\u521b\u5efa\u5339\u914d\u4e0a\u4e0b\u6587\n    pcre2_match_context *match_context = pcre2_match_context_create(NULL);\n\n    \/\/ 6\u3001\u7ed9\u5339\u914d\u4e0a\u4e0b\u6587\u5206\u914d JIT \u6808\n    pcre2_jit_stack_assign(match_context, NULL, jit_stack);\n\n    \/\/ \u521b\u5efa\u5339\u914d\u7ed3\u679c\u6570\u636e\n    pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re_code, NULL);\n\n    \/\/ 7\u3001\u5339\u914d\u76ee\u6807\u5b57\u7b26\u4e32\n    const char *subject = \"uiuiuiuibcDEFhijabCDefklmn\";\n    result = pcre2_match(re_code, (PCRE2_SPTR)subject, strlen(subject), 0, 0, match_data, match_context);\n    if (result &gt;= 0) {\n        \/\/ \u5339\u914d\u6210\u529f\uff0c\u89e3\u6790\u68c0\u6d4b\u7ed3\u679c\n        PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);\n        for (int i = 0; i &lt; result; i++) {\n            printf(\"match %d: '%.*s'\\n\", i, (int)(ovector&#91;2 * i + 1] - ovector&#91;2 * i]), subject + ovector&#91;2 * i]);\n        }\n    } else {\n        \/\/ \u5904\u7406\u9519\u8bef\n        fprintf(stderr, \"Failed to match pattern\\n\");\n    }\n\n    subject = \"123456abcDEFhijklmn\";\n    result = pcre2_match(re_code, (PCRE2_SPTR)subject, strlen(subject), 0, 0, match_data, match_context);\n    if (result &gt;= 0) {\n        \/\/ \u5339\u914d\u6210\u529f\uff0c\u89e3\u6790\u68c0\u6d4b\u7ed3\u679c\n        PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);\n        for (int i = 0; i &lt; result; i++) {\n            printf(\"match %d: '%.*s'\\n\", i, (int)(ovector&#91;2 * i + 1] - ovector&#91;2 * i]), subject + ovector&#91;2 * i]);\n        }\n    } else {\n        \/\/ \u5904\u7406\u9519\u8bef\n        fprintf(stderr, \"Failed to match pattern\\n\");\n    }\n\n    \/\/ \u6e05\u7406\u8d44\u6e90\n    pcre2_match_data_free(match_data);\n    pcre2_match_context_free(match_context);\n    pcre2_jit_stack_free(jit_stack);\n    pcre2_code_free(re_code);\n    pcre2_compile_context_free(compile_context);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"396\" height=\"151\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-3.png\" alt=\"\" class=\"wp-image-1656\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-3.png 396w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-3-300x114.png 300w\" sizes=\"auto, (max-width: 396px) 100vw, 396px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PCRE2 \u5e93\u4e2d\uff0cJIT \u7528\u4e8e\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u6027\u80fd\u3002\u901a\u8fc7 JIT \u7f16\u8bd1\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u88ab\u8f6c\u6362\u4e3a\u673a\u5668\u7801\uff0c\u4ece\u800c\u663e\u8457\u52a0\u5feb\u5339\u914d\u901f\u5ea6\u3002<br \/>\n\u4f7f\u7528\u65b9\u6cd5\uff1a<br \/>\n1\u3001\u5728\u90fd\u6a21\u5f0f\u7f16\u8bd1\u540e\uff0c\u542f\u52a8 JIT \u7f16\u8bd1\u529f\u80fd\uff1b<br \/>\n2\u3001\u7533\u8bf7 JIT \u6808\uff0c\u5206\u914d\u7ed9\u5339\u914d\u4e0a\u4e0b\u6587\uff1b<br \/>\n3\u3001\u4f7f\u7528\u5339\u914d\u4e0a\u4e0b\u6587\u5bf9\u88ab\u68c0\u6d4b\u5b57\u7b26\u4e32\u8fdb\u884c\u5339\u914d<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[535,532,536,534,533,531,256],"class_list":["post-1655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-compile","tag-jit","tag-jit_stack","tag-match","tag-pcre2","tag-531","tag-256"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1655"}],"version-history":[{"count":4,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1655\/revisions"}],"predecessor-version":[{"id":1673,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1655\/revisions\/1673"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}