Re: [pcre-dev] Using pcre: the /g behaviour

トップ ページ
このメッセージを削除
著者: Philip Hazel
日付:  
To: jonetsu
CC: pcre-dev
題目: Re: [pcre-dev] Using pcre: the /g behaviour
On Thu, 23 Feb 2012, jonetsu wrote:

> /* Loop for second and subsequent matches */
> for (;;)
> {
>
> [ snip ]
>
> // Added bit:
>
>   (void)pcre_fullinfo(
>     re,
>     NULL,
>     PCRE_INFO_NAMETABLE,
>     &name_table);

>
> // Back to original code:
>
>   if (namecount <= 0) printf("No named substrings\n"); else
>     {
>     unsigned char *tabptr = name_table;
>     printf("Named substrings\n");
>     for (i = 0; i < namecount; i++)
>       {
>    int n = (tabptr[0] << 8) | tabptr[1];


Which version of PCRE are you using? That code does not agree with the
latest version of pcredemo, which reads as follows:


(void)pcre_fullinfo(                                                           
  re,                   /* the compiled pattern */                      
  NULL,                 /* no extra data - we didn't study the pattern */
  PCRE_INFO_NAMECOUNT,  /* number of named substrings */
  &namecount);          /* where to put the answer */


if (namecount <= 0) printf("No named substrings\n"); else                      
  {                                                                        
  unsigned char *tabptr;                                                   
  printf("Named substrings\n");                                           


/* Before we can access the substrings, we must extract the table for
translating names to numbers, and the size of each entry in the table. */

  (void)pcre_fullinfo(
    re,                       /* the compiled pattern */
    NULL,                     /* no extra data - we didn't study the pattern */
    PCRE_INFO_NAMETABLE,      /* address of the table */
    &name_table);             /* where to put the answer */


  (void)pcre_fullinfo(         
    re,                       /* the compiled pattern */
    NULL,                     /* no extra data - we didn't study the pattern */
    PCRE_INFO_NAMEENTRYSIZE,  /* size of each entry in the table */
    &name_entry_size);        /* where to put the answer */


/* Now we can scan the table and, for each entry, print the number, the name,
and the substring itself. */


In other words, the code you suggest is already present. And indeed,
your example does not crash when I test it.

Philip

--
Philip Hazel