/[pcre]/code/trunk/pcre_maketables.c
ViewVC logotype

Contents of /code/trunk/pcre_maketables.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (hide annotations) (download)
Sat Feb 24 21:40:45 2007 UTC (7 years, 4 months ago) by nigel
File MIME type: text/plain
File size: 5310 byte(s)
Load pcre-6.0 into code/trunk.

1 nigel 77 /*************************************************
2     * Perl-Compatible Regular Expressions *
3     *************************************************/
4    
5     /* PCRE is a library of functions to support regular expressions whose syntax
6     and semantics are as close as possible to those of the Perl 5 language.
7    
8     Written by Philip Hazel
9     Copyright (c) 1997-2005 University of Cambridge
10    
11     -----------------------------------------------------------------------------
12     Redistribution and use in source and binary forms, with or without
13     modification, are permitted provided that the following conditions are met:
14    
15     * Redistributions of source code must retain the above copyright notice,
16     this list of conditions and the following disclaimer.
17    
18     * Redistributions in binary form must reproduce the above copyright
19     notice, this list of conditions and the following disclaimer in the
20     documentation and/or other materials provided with the distribution.
21    
22     * Neither the name of the University of Cambridge nor the names of its
23     contributors may be used to endorse or promote products derived from
24     this software without specific prior written permission.
25    
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36     POSSIBILITY OF SUCH DAMAGE.
37     -----------------------------------------------------------------------------
38     */
39    
40    
41     /* This module contains the external function pcre_maketables(), which builds
42     character tables for PCRE in the current locale. The file is compiled on its
43     own as part of the PCRE library. However, it is also included in the
44     compilation of dftables.c, in which case the macro DFTABLES is defined. */
45    
46    
47     #ifndef DFTABLES
48     #include "pcre_internal.h"
49     #endif
50    
51    
52     /*************************************************
53     * Create PCRE character tables *
54     *************************************************/
55    
56     /* This function builds a set of character tables for use by PCRE and returns
57     a pointer to them. They are build using the ctype functions, and consequently
58     their contents will depend upon the current locale setting. When compiled as
59     part of the library, the store is obtained via pcre_malloc(), but when compiled
60     inside dftables, use malloc().
61    
62     Arguments: none
63     Returns: pointer to the contiguous block of data
64     */
65    
66     const unsigned char *
67     pcre_maketables(void)
68     {
69     unsigned char *yield, *p;
70     int i;
71    
72     #ifndef DFTABLES
73     yield = (unsigned char*)(pcre_malloc)(tables_length);
74     #else
75     yield = (unsigned char*)malloc(tables_length);
76     #endif
77    
78     if (yield == NULL) return NULL;
79     p = yield;
80    
81     /* First comes the lower casing table */
82    
83     for (i = 0; i < 256; i++) *p++ = tolower(i);
84    
85     /* Next the case-flipping table */
86    
87     for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
88    
89     /* Then the character class tables. Don't try to be clever and save effort
90     on exclusive ones - in some locales things may be different. Note that the
91     table for "space" includes everything "isspace" gives, including VT in the
92     default locale. This makes it work for the POSIX class [:space:]. */
93    
94     memset(p, 0, cbit_length);
95     for (i = 0; i < 256; i++)
96     {
97     if (isdigit(i))
98     {
99     p[cbit_digit + i/8] |= 1 << (i&7);
100     p[cbit_word + i/8] |= 1 << (i&7);
101     }
102     if (isupper(i))
103     {
104     p[cbit_upper + i/8] |= 1 << (i&7);
105     p[cbit_word + i/8] |= 1 << (i&7);
106     }
107     if (islower(i))
108     {
109     p[cbit_lower + i/8] |= 1 << (i&7);
110     p[cbit_word + i/8] |= 1 << (i&7);
111     }
112     if (i == '_') p[cbit_word + i/8] |= 1 << (i&7);
113     if (isspace(i)) p[cbit_space + i/8] |= 1 << (i&7);
114     if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7);
115     if (isgraph(i)) p[cbit_graph + i/8] |= 1 << (i&7);
116     if (isprint(i)) p[cbit_print + i/8] |= 1 << (i&7);
117     if (ispunct(i)) p[cbit_punct + i/8] |= 1 << (i&7);
118     if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1 << (i&7);
119     }
120     p += cbit_length;
121    
122     /* Finally, the character type table. In this, we exclude VT from the white
123     space chars, because Perl doesn't recognize it as such for \s and for comments
124     within regexes. */
125    
126     for (i = 0; i < 256; i++)
127     {
128     int x = 0;
129     if (i != 0x0b && isspace(i)) x += ctype_space;
130     if (isalpha(i)) x += ctype_letter;
131     if (isdigit(i)) x += ctype_digit;
132     if (isxdigit(i)) x += ctype_xdigit;
133     if (isalnum(i) || i == '_') x += ctype_word;
134    
135     /* Note: strchr includes the terminating zero in the characters it considers.
136     In this instance, that is ok because we want binary zero to be flagged as a
137     meta-character, which in this sense is any character that terminates a run
138     of data characters. */
139    
140     if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta; *p++ = x; }
141    
142     return yield;
143     }
144    
145     /* End of pcre_maketables.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12