PCRE - 偏移向量,是3的倍数吗?

7

我正在学习PCRE,但我不明白为什么偏移向量必须是3的倍数。这段代码来自pcredemo.c文件(rcpcre_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 呢?
谢谢。
1个回答

6

手册:

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


我还没有在手册中找到那部分内容。下次我最好多看看手册。谢谢。 - woky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接