[Pcre-svn] [1139] code/trunk: Update pcre2demo with match_da…

Top Page
Delete this message
Author: Subversion repository
Date:  
To: pcre-svn
Subject: [Pcre-svn] [1139] code/trunk: Update pcre2demo with match_data block size information.
Revision: 1139
          http://www.exim.org/viewvc/pcre2?view=rev&revision=1139
Author:   ph10
Date:     2019-07-19 16:31:54 +0100 (Fri, 19 Jul 2019)
Log Message:
-----------
Update pcre2demo with match_data block size information.


Modified Paths:
--------------
    code/trunk/doc/html/pcre2demo.html
    code/trunk/doc/pcre2demo.3
    code/trunk/src/pcre2demo.c


Modified: code/trunk/doc/html/pcre2demo.html
===================================================================
--- code/trunk/doc/html/pcre2demo.html    2019-07-18 17:20:29 UTC (rev 1138)
+++ code/trunk/doc/html/pcre2demo.html    2019-07-19 15:31:54 UTC (rev 1139)
@@ -104,12 +104,11 @@


PCRE2_SIZE erroroffset;
PCRE2_SIZE *ovector;
+PCRE2_SIZE subject_length;

-size_t subject_length;
pcre2_match_data *match_data;


-
/**************************************************************************
* First, sort out the command line. There is only one possible option at *
* the moment, "-g" to request repeated matching to find all occurrences, *
@@ -138,12 +137,14 @@
return 1;
}

-/* As pattern and subject are char arguments, they can be straightforwardly
-cast to PCRE2_SPTR as we are working in 8-bit code units. */
+/* Pattern and subject are char arguments, so they can be straightforwardly
+cast to PCRE2_SPTR because we are working in 8-bit code units. The subject
+length is cast to PCRE2_SIZE for completeness, though PCRE2_SIZE is in fact
+defined to be size_t. */

pattern = (PCRE2_SPTR)argv[i];
subject = (PCRE2_SPTR)argv[i+1];
-subject_length = strlen((char *)subject);
+subject_length = (PCRE2_SIZE)strlen((char *)subject);


/*************************************************************************
@@ -172,17 +173,22 @@


 /*************************************************************************
-* If the compilation succeeded, we call PCRE again, in order to do a     *
+* If the compilation succeeded, we call PCRE2 again, in order to do a    *
 * pattern match against the subject string. This does just ONE match. If *
 * further matching is needed, it will be done below. Before running the  *
-* match we must set up a match_data block for holding the result.        *
+* match we must set up a match_data block for holding the result. Using  *
+* pcre2_match_data_create_from_pattern() ensures that the block is       *
+* exactly the right size for the number of capturing parentheses in the  *
+* pattern. If you need to know the actual size of a match_data block as  *
+* a number of bytes, you can find it like this:                          *
+*                                                                        *
+* PCRE2_SIZE match_data_size = pcre2_get_match_data_size(match_data);    *
 *************************************************************************/


-/* Using this function ensures that the block is exactly the right size for
-the number of capturing parentheses in the pattern. */
-
match_data = pcre2_match_data_create_from_pattern(re, NULL);

+/* Now run the match. */
+
 rc = pcre2_match(
   re,                   /* the compiled pattern */
   subject,              /* the subject string */
@@ -205,7 +211,7 @@
     default: printf("Matching error %d\n", rc); break;
     }
   pcre2_match_data_free(match_data);   /* Release memory used for the match */
-  pcre2_code_free(re);                 /* data and the compiled pattern. */
+  pcre2_code_free(re);                 /*   data and the compiled pattern. */
   return 1;
   }


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


Modified: code/trunk/doc/pcre2demo.3
===================================================================
--- code/trunk/doc/pcre2demo.3    2019-07-18 17:20:29 UTC (rev 1138)
+++ code/trunk/doc/pcre2demo.3    2019-07-19 15:31:54 UTC (rev 1139)
@@ -104,12 +104,11 @@


PCRE2_SIZE erroroffset;
PCRE2_SIZE *ovector;
+PCRE2_SIZE subject_length;

-size_t subject_length;
pcre2_match_data *match_data;


-
/**************************************************************************
* First, sort out the command line. There is only one possible option at *
* the moment, "-g" to request repeated matching to find all occurrences, *
@@ -138,12 +137,14 @@
return 1;
}

-/* As pattern and subject are char arguments, they can be straightforwardly
-cast to PCRE2_SPTR as we are working in 8-bit code units. */
+/* Pattern and subject are char arguments, so they can be straightforwardly
+cast to PCRE2_SPTR because we are working in 8-bit code units. The subject
+length is cast to PCRE2_SIZE for completeness, though PCRE2_SIZE is in fact
+defined to be size_t. */

pattern = (PCRE2_SPTR)argv[i];
subject = (PCRE2_SPTR)argv[i+1];
-subject_length = strlen((char *)subject);
+subject_length = (PCRE2_SIZE)strlen((char *)subject);


/*************************************************************************
@@ -172,17 +173,22 @@


 /*************************************************************************
-* If the compilation succeeded, we call PCRE again, in order to do a     *
+* If the compilation succeeded, we call PCRE2 again, in order to do a    *
 * pattern match against the subject string. This does just ONE match. If *
 * further matching is needed, it will be done below. Before running the  *
-* match we must set up a match_data block for holding the result.        *
+* match we must set up a match_data block for holding the result. Using  *
+* pcre2_match_data_create_from_pattern() ensures that the block is       *
+* exactly the right size for the number of capturing parentheses in the  *
+* pattern. If you need to know the actual size of a match_data block as  *
+* a number of bytes, you can find it like this:                          *
+*                                                                        *
+* PCRE2_SIZE match_data_size = pcre2_get_match_data_size(match_data);    *
 *************************************************************************/


-/* Using this function ensures that the block is exactly the right size for
-the number of capturing parentheses in the pattern. */
-
match_data = pcre2_match_data_create_from_pattern(re, NULL);

+/* Now run the match. */
+
 rc = pcre2_match(
   re,                   /* the compiled pattern */
   subject,              /* the subject string */
@@ -205,7 +211,7 @@
     default: printf("Matching error %d\en", rc); break;
     }
   pcre2_match_data_free(match_data);   /* Release memory used for the match */
-  pcre2_code_free(re);                 /* data and the compiled pattern. */
+  pcre2_code_free(re);                 /*   data and the compiled pattern. */
   return 1;
   }


@@ -249,7 +255,7 @@
for (i = 0; i < rc; i++)
{
PCRE2_SPTR substring_start = subject + ovector[2*i];
- size_t substring_length = ovector[2*i+1] - ovector[2*i];
+ PCRE2_SIZE substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: %.*s\en", i, (int)substring_length, (char *)substring_start);
}


Modified: code/trunk/src/pcre2demo.c
===================================================================
--- code/trunk/src/pcre2demo.c    2019-07-18 17:20:29 UTC (rev 1138)
+++ code/trunk/src/pcre2demo.c    2019-07-19 15:31:54 UTC (rev 1139)
@@ -87,12 +87,11 @@


PCRE2_SIZE erroroffset;
PCRE2_SIZE *ovector;
+PCRE2_SIZE subject_length;

-size_t subject_length;
pcre2_match_data *match_data;


-
/**************************************************************************
* First, sort out the command line. There is only one possible option at *
* the moment, "-g" to request repeated matching to find all occurrences, *
@@ -121,12 +120,14 @@
return 1;
}

-/* As pattern and subject are char arguments, they can be straightforwardly
-cast to PCRE2_SPTR as we are working in 8-bit code units. */
+/* Pattern and subject are char arguments, so they can be straightforwardly
+cast to PCRE2_SPTR because we are working in 8-bit code units. The subject
+length is cast to PCRE2_SIZE for completeness, though PCRE2_SIZE is in fact
+defined to be size_t. */

pattern = (PCRE2_SPTR)argv[i];
subject = (PCRE2_SPTR)argv[i+1];
-subject_length = strlen((char *)subject);
+subject_length = (PCRE2_SIZE)strlen((char *)subject);


/*************************************************************************
@@ -155,17 +156,22 @@


 /*************************************************************************
-* If the compilation succeeded, we call PCRE again, in order to do a     *
+* If the compilation succeeded, we call PCRE2 again, in order to do a    *
 * pattern match against the subject string. This does just ONE match. If *
 * further matching is needed, it will be done below. Before running the  *
-* match we must set up a match_data block for holding the result.        *
+* match we must set up a match_data block for holding the result. Using  *
+* pcre2_match_data_create_from_pattern() ensures that the block is       *
+* exactly the right size for the number of capturing parentheses in the  *
+* pattern. If you need to know the actual size of a match_data block as  *
+* a number of bytes, you can find it like this:                          *
+*                                                                        *
+* PCRE2_SIZE match_data_size = pcre2_get_match_data_size(match_data);    *
 *************************************************************************/


-/* Using this function ensures that the block is exactly the right size for
-the number of capturing parentheses in the pattern. */
-
match_data = pcre2_match_data_create_from_pattern(re, NULL);

+/* Now run the match. */
+
 rc = pcre2_match(
   re,                   /* the compiled pattern */
   subject,              /* the subject string */
@@ -188,7 +194,7 @@
     default: printf("Matching error %d\n", rc); break;
     }
   pcre2_match_data_free(match_data);   /* Release memory used for the match */
-  pcre2_code_free(re);                 /* data and the compiled pattern. */
+  pcre2_code_free(re);                 /*   data and the compiled pattern. */
   return 1;
   }


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