diff options
author | ph10 <ph10@6239d852-aaf2-0410-a92c-79f79f948069> | 2019-07-19 15:31:54 +0000 |
---|---|---|
committer | ph10 <ph10@6239d852-aaf2-0410-a92c-79f79f948069> | 2019-07-19 15:31:54 +0000 |
commit | 361e123dd630523e542dfca697ca84f23dc148f8 (patch) | |
tree | 2124140b37e6a9944920bd19c09ddd6aad3a026e /src/pcre2demo.c | |
parent | 5299fd33a26a5ca52e58b04c53375a206cf771b2 (diff) | |
download | pcre2-361e123dd630523e542dfca697ca84f23dc148f8.tar.gz |
Update pcre2demo with match_data block size information.
git-svn-id: svn://vcs.exim.org/pcre2/code/trunk@1139 6239d852-aaf2-0410-a92c-79f79f948069
Diffstat (limited to 'src/pcre2demo.c')
-rw-r--r-- | src/pcre2demo.c | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/src/pcre2demo.c b/src/pcre2demo.c index 5d9b321..a49f1f8 100644 --- a/src/pcre2demo.c +++ b/src/pcre2demo.c @@ -87,12 +87,11 @@ uint32_t newline; 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 @@ if (argc - i != 2) 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 (re == NULL) /************************************************************************* -* 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 @@ if (rc < 0) 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 @@ application you might want to do things other than print them. */ 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); } |