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

时间:2012-08-16 19:15:54

标签: c regex pcre

我正在学习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 个答案:

答案 0 :(得分:5)

The manual

  

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