【问题标题】:PCRE - offset vector, multiple of 3?PCRE - 偏移向量,3 的倍数?
【发布时间】:2012-08-16 19:15:53
【问题描述】:

我正在学习 PCRE,但我不明白为什么偏移向量必须是 3 的倍数。这是来自 pcredemo.c(rc 是来自 pcre_exec() 的结果):

/* The output vector wasn't big enough */

if (rc == 0) {
    rc = OVECCOUNT / 3;
    printf("ovector only has room for %d captured substrings\n", rc - 1);
}

/* Show substrings stored in the output vector by number. Obviously, in a real
 * application you might want to do things other than print them. */

for (i = 0; i < rc; i++) {
    char *substring_start = subject + ovector[2 * i];
    int substring_length = ovector[2 * i + 1] - ovector[2 * i];
    printf("%2d: %.*s\n", i, substring_length, substring_start);
}

在我看来,ovector 存储str1_start, str1_end, str2_start, str2_end, ...,因此数组可以保存 OVECCOUNT/2 个字符串。为什么是 OVECCOUNT/3?

谢谢。

【问题讨论】:

    标签: c regex pcre


    【解决方案1】:

    The manual:

    向量的前三分之二用于传回捕获 子串,每个子串使用一对整数。 剩下的 在匹配时,pcre_exec() 将向量的三分之一用作工作区 捕获子模式,并且不可用于回传 信息。 ovecsize 中传递的数字应始终为倍数 三个。如果不是,则向下舍入。

    【讨论】:

    • 我在手册方面还没有走那么远。下次我会更好地使用 RTFM。谢谢。
    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    相关资源
    最近更新 更多