/[pcre]/code/trunk/doc/pcreprecompile.3
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 79 - (hide annotations) (download)
Sat Feb 24 21:40:52 2007 UTC (7 years, 6 months ago) by nigel
File size: 5383 byte(s)
Load pcre-6.1 into code/trunk.

1 nigel 79 .TH PCREPRECOMPILE 3
2 nigel 75 .SH NAME
3     PCRE - Perl-compatible regular expressions
4     .SH "SAVING AND RE-USING PRECOMPILED PCRE PATTERNS"
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     .
23     .SH "SAVING A COMPILED PATTERN"
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     .
78     .SH "RE-USING A PRECOMPILED PATTERN"
79     .rs
80     .sp
81     Re-using a precompiled pattern is straightforward. Having reloaded it into main
82 nigel 77 memory, you pass its pointer to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP in
83     the usual way. This should work even on another host, and even if that host has
84     the opposite endianness to the one where the pattern was compiled.
85 nigel 75 .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 nigel 77 now pass a similar pointer to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP,
89     because the value saved with the compiled pattern will obviously be nonsense. A
90     field in a \fBpcre_extra()\fP block is used to pass this data, as described in
91     the
92 nigel 75 .\" HTML <a href="pcreapi.html#extradata">
93     .\" </a>
94     section on matching a pattern
95     .\"
96     in the
97     .\" HREF
98     \fBpcreapi\fP
99     .\"
100     documentation.
101     .P
102     If you did not provide custom character tables when the pattern was compiled,
103     the pointer in the compiled pattern is NULL, which causes \fBpcre_exec()\fP to
104     use PCRE's internal tables. Thus, you do not need to take any special action at
105     run time in this case.
106     .P
107     If you saved study data with the compiled pattern, you need to create your own
108     \fBpcre_extra\fP data block and set the \fIstudy_data\fP field to point to the
109     reloaded study data. You must also set the PCRE_EXTRA_STUDY_DATA bit in the
110     \fIflags\fP field to indicate that study data is present. Then pass the
111 nigel 77 \fBpcre_extra\fP block to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP in the
112     usual way.
113 nigel 75 .
114     .
115     .SH "COMPATIBILITY WITH DIFFERENT PCRE RELEASES"
116     .rs
117     .sp
118     The layout of the control block that is at the start of the data that makes up
119     a compiled pattern was changed for release 5.0. If you have any saved patterns
120     that were compiled with previous releases (not a facility that was previously
121     advertised), you will have to recompile them for release 5.0. However, from now
122 nigel 79 on, it should be possible to make changes in a compatible manner.
123 nigel 75 .P
124     .in 0
125 nigel 77 Last updated: 28 February 2005
126 nigel 75 .br
127 nigel 77 Copyright (c) 1997-2005 University of Cambridge.

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12