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

Contents of /code/trunk/maketables.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (hide annotations) (download)
Sat Feb 24 21:40:37 2007 UTC (6 years, 3 months ago) by nigel
File MIME type: text/plain
File size: 5173 byte(s)
Load pcre-5.0 into code/trunk.

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12