ViewVC logotype

Contents of /code/trunk/doc/pcreprecompile.3

Parent Directory Parent Directory | Revision Log Revision Log

Revision 75 - (hide annotations) (download)
Sat Feb 24 21:40:37 2007 UTC (8 years, 2 months ago) by nigel
File size: 5299 byte(s)
Load pcre-5.0 into code/trunk.

1 nigel 75 .TH PCRE 3
2     .SH NAME
3     PCRE - Perl-compatible regular expressions
5     .rs
6     .sp
7     If you are running an application that uses a large number of regular
8     expression patterns, it may be useful to store them in a precompiled form
9     instead of having to compile them every time the application is run.
10     If you are not using any private character tables (see the
11     .\" HREF
12     \fBpcre_maketables()\fP
13     .\"
14     documentation), this is relatively straightforward. If you are using private
15     tables, it is a little bit more complicated.
16     .P
17     If you save compiled patterns to a file, you can copy them to a different host
18     and run them there. This works even if the new host has the opposite endianness
19     to the one on which the patterns were compiled. There may be a small
20     performance penalty, but it should be insignificant.
21     .
22     .
24     .rs
25     .sh
26     The value returned by \fBpcre_compile()\fP points to a single block of memory
27     that holds the compiled pattern and associated data. You can find the length of
28     this block in bytes by calling \fBpcre_fullinfo()\fP with an argument of
29     PCRE_INFO_SIZE. You can then save the data in any appropriate manner. Here is
30     sample code that compiles a pattern and writes it to a file. It assumes that
31     the variable \fIfd\fP refers to a file that is open for output:
32     .sp
33     int erroroffset, rc, size;
34     char *error;
35     pcre *re;
36     .sp
37     re = pcre_compile("my pattern", 0, &error, &erroroffset, NULL);
38     if (re == NULL) { ... handle errors ... }
39     rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &size);
40     if (rc < 0) { ... handle errors ... }
41     rc = fwrite(re, 1, size, fd);
42     if (rc != size) { ... handle errors ... }
43     .sp
44     In this example, the bytes that comprise the compiled pattern are copied
45     exactly. Note that this is binary data that may contain any of the 256 possible
46     byte values. On systems that make a distinction between binary and non-binary
47     data, be sure that the file is opened for binary output.
48     .P
49     If you want to write more than one pattern to a file, you will have to devise a
50     way of separating them. For binary data, preceding each pattern with its length
51     is probably the most straightforward approach. Another possibility is to write
52     out the data in hexadecimal instead of binary, one pattern to a line.
53     .P
54     Saving compiled patterns in a file is only one possible way of storing them for
55     later use. They could equally well be saved in a database, or in the memory of
56     some daemon process that passes them via sockets to the processes that want
57     them.
58     .P
59     If the pattern has been studied, it is also possible to save the study data in
60     a similar way to the compiled pattern itself. When studying generates
61     additional information, \fBpcre_study()\fP returns a pointer to a
62     \fBpcre_extra\fP data block. Its format is defined in the
63     .\" HTML <a href="pcreapi.html#extradata">
64     .\" </a>
65     section on matching a pattern
66     .\"
67     in the
68     .\" HREF
69     \fBpcreapi\fP
70     .\"
71     documentation. The \fIstudy_data\fP field points to the binary study data, and
72     this is what you must save (not the \fBpcre_extra\fP block itself). The length
73     of the study data can be obtained by calling \fBpcre_fullinfo()\fP with an
74     argument of PCRE_INFO_STUDYSIZE. Remember to check that \fBpcre_study()\fP did
75     return a non-NULL value before trying to save the study data.
76     .
77     .
79     .rs
80     .sp
81     Re-using a precompiled pattern is straightforward. Having reloaded it into main
82     memory, you pass its pointer to \fBpcre_exec()\fP in the usual way. This should
83     work even on another host, and even if that host has the opposite endianness to
84     the one where the pattern was compiled.
85     .P
86     However, if you passed a pointer to custom character tables when the pattern
87     was compiled (the \fItableptr\fP argument of \fBpcre_compile()\fP), you must
88     now pass a similar pointer to \fBpcre_exec()\fP, because the value saved with
89     the compiled pattern will obviously be nonsense. A field in a
90     \fBpcre_extra()\fP block is used to pass this data, as described in the
91     .\" HTML <a href="pcreapi.html#extradata">
92     .\" </a>
93     section on matching a pattern
94     .\"
95     in the
96     .\" HREF
97     \fBpcreapi\fP
98     .\"
99     documentation.
100     .P
101     If you did not provide custom character tables when the pattern was compiled,
102     the pointer in the compiled pattern is NULL, which causes \fBpcre_exec()\fP to
103     use PCRE's internal tables. Thus, you do not need to take any special action at
104     run time in this case.
105     .P
106     If you saved study data with the compiled pattern, you need to create your own
107     \fBpcre_extra\fP data block and set the \fIstudy_data\fP field to point to the
108     reloaded study data. You must also set the PCRE_EXTRA_STUDY_DATA bit in the
109     \fIflags\fP field to indicate that study data is present. Then pass the
110     \fBpcre_extra\fP block to \fBpcre_exec()\fP in the usual way.
111     .
112     .
114     .rs
115     .sp
116     The layout of the control block that is at the start of the data that makes up
117     a compiled pattern was changed for release 5.0. If you have any saved patterns
118     that were compiled with previous releases (not a facility that was previously
119     advertised), you will have to recompile them for release 5.0. However, from now
120     on, it should be possible to make changes in a compabible manner.
121     .P
122     .in 0
123     Last updated: 10 September 2004
124     .br
125     Copyright (c) 1997-2004 University of Cambridge.

ViewVC Help
Powered by ViewVC 1.1.12