2012-08-16 35 views
4

我leraning PCRE,我不明白爲什麼偏移向量必須是多重的3這是pcredemo.c(rcpcre_exec()結果):PCRE - 偏移矢量,3的倍數?

/* 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?

謝謝。

回答

4

The manual

第一向量的三分之二被用於回傳捕獲 串,使用一對整數的每個子字符串。 載體的其餘 第三由pcre_exec用作工作空間(),同時匹配 捕獲的子模式,並且不能用於傳回 信息。在ovecsize中傳遞的數字應始終爲3的倍數 。如果不是,則向下舍入。

+0

我沒有在手冊那麼遠,。我下次更好地RTFM。謝謝。 – woky 2012-08-16 19:40:43