| 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 |
|
|
Permission is granted to anyone to use this software for any purpose on any |
| 15 |
|
|
computer system, and to redistribute it freely, subject to the following |
| 16 |
|
|
restrictions: |
| 17 |
|
|
|
| 18 |
|
|
1. This software is distributed in the hope that it will be useful, |
| 19 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 21 |
|
|
|
| 22 |
|
|
2. The origin of this software must not be misrepresented, either by |
| 23 |
|
|
explicit claim or by omission. |
| 24 |
|
|
|
| 25 |
|
|
3. Altered versions must be plainly marked as such, and must not be |
| 26 |
|
|
misrepresented as being the original software. |
| 27 |
nigel |
29 |
|
| 28 |
|
|
4. If PCRE is embedded in any software that is released under the GNU |
| 29 |
|
|
General Purpose Licence (GPL), then the terms of that licence shall |
| 30 |
|
|
supersede any condition above with which it is incompatible. |
| 31 |
nigel |
3 |
----------------------------------------------------------------------------- |
| 32 |
|
|
|
| 33 |
|
|
See the file Tech.Notes for some information on the internals. |
| 34 |
|
|
*/ |
| 35 |
|
|
|
| 36 |
|
|
|
| 37 |
nigel |
25 |
/* This file is compiled on its own as part of the PCRE library. However, |
| 38 |
nigel |
27 |
it is also included in the compilation of dftables.c, in which case the macro |
| 39 |
|
|
DFTABLES is defined. */ |
| 40 |
nigel |
3 |
|
| 41 |
nigel |
27 |
#ifndef DFTABLES |
| 42 |
nigel |
3 |
#include "internal.h" |
| 43 |
nigel |
25 |
#endif |
| 44 |
nigel |
3 |
|
| 45 |
nigel |
25 |
|
| 46 |
|
|
|
| 47 |
|
|
/************************************************* |
| 48 |
|
|
* Create PCRE character tables * |
| 49 |
|
|
*************************************************/ |
| 50 |
|
|
|
| 51 |
|
|
/* This function builds a set of character tables for use by PCRE and returns |
| 52 |
|
|
a pointer to them. They are build using the ctype functions, and consequently |
| 53 |
|
|
their contents will depend upon the current locale setting. When compiled as |
| 54 |
|
|
part of the library, the store is obtained via pcre_malloc(), but when compiled |
| 55 |
nigel |
27 |
inside dftables, use malloc(). |
| 56 |
nigel |
25 |
|
| 57 |
|
|
Arguments: none |
| 58 |
|
|
Returns: pointer to the contiguous block of data |
| 59 |
|
|
*/ |
| 60 |
|
|
|
| 61 |
nigel |
53 |
const unsigned char * |
| 62 |
nigel |
25 |
pcre_maketables(void) |
| 63 |
nigel |
3 |
{ |
| 64 |
nigel |
25 |
unsigned char *yield, *p; |
| 65 |
nigel |
3 |
int i; |
| 66 |
|
|
|
| 67 |
nigel |
27 |
#ifndef DFTABLES |
| 68 |
nigel |
37 |
yield = (unsigned char*)(pcre_malloc)(tables_length); |
| 69 |
nigel |
25 |
#else |
| 70 |
nigel |
37 |
yield = (unsigned char*)malloc(tables_length); |
| 71 |
nigel |
25 |
#endif |
| 72 |
nigel |
3 |
|
| 73 |
nigel |
25 |
if (yield == NULL) return NULL; |
| 74 |
|
|
p = yield; |
| 75 |
nigel |
3 |
|
| 76 |
nigel |
25 |
/* First comes the lower casing table */ |
| 77 |
nigel |
3 |
|
| 78 |
nigel |
25 |
for (i = 0; i < 256; i++) *p++ = tolower(i); |
| 79 |
nigel |
3 |
|
| 80 |
nigel |
25 |
/* Next the case-flipping table */ |
| 81 |
nigel |
3 |
|
| 82 |
nigel |
25 |
for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i); |
| 83 |
nigel |
3 |
|
| 84 |
nigel |
43 |
/* Then the character class tables. Don't try to be clever and save effort |
| 85 |
nigel |
63 |
on exclusive ones - in some locales things may be different. Note that the |
| 86 |
|
|
table for "space" includes everything "isspace" gives, including VT in the |
| 87 |
|
|
default locale. This makes it work for the POSIX class [:space:]. */ |
| 88 |
nigel |
25 |
|
| 89 |
|
|
memset(p, 0, cbit_length); |
| 90 |
nigel |
3 |
for (i = 0; i < 256; i++) |
| 91 |
|
|
{ |
| 92 |
nigel |
43 |
if (isdigit(i)) |
| 93 |
|
|
{ |
| 94 |
|
|
p[cbit_digit + i/8] |= 1 << (i&7); |
| 95 |
|
|
p[cbit_word + i/8] |= 1 << (i&7); |
| 96 |
|
|
} |
| 97 |
|
|
if (isupper(i)) |
| 98 |
|
|
{ |
| 99 |
|
|
p[cbit_upper + i/8] |= 1 << (i&7); |
| 100 |
|
|
p[cbit_word + i/8] |= 1 << (i&7); |
| 101 |
|
|
} |
| 102 |
|
|
if (islower(i)) |
| 103 |
|
|
{ |
| 104 |
|
|
p[cbit_lower + i/8] |= 1 << (i&7); |
| 105 |
|
|
p[cbit_word + i/8] |= 1 << (i&7); |
| 106 |
|
|
} |
| 107 |
|
|
if (i == '_') p[cbit_word + i/8] |= 1 << (i&7); |
| 108 |
nigel |
25 |
if (isspace(i)) p[cbit_space + i/8] |= 1 << (i&7); |
| 109 |
nigel |
43 |
if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7); |
| 110 |
|
|
if (isgraph(i)) p[cbit_graph + i/8] |= 1 << (i&7); |
| 111 |
|
|
if (isprint(i)) p[cbit_print + i/8] |= 1 << (i&7); |
| 112 |
|
|
if (ispunct(i)) p[cbit_punct + i/8] |= 1 << (i&7); |
| 113 |
|
|
if (iscntrl(i)) p[cbit_cntrl + i/8] |= 1 << (i&7); |
| 114 |
nigel |
3 |
} |
| 115 |
nigel |
25 |
p += cbit_length; |
| 116 |
nigel |
3 |
|
| 117 |
nigel |
63 |
/* Finally, the character type table. In this, we exclude VT from the white |
| 118 |
|
|
space chars, because Perl doesn't recognize it as such for \s and for comments |
| 119 |
|
|
within regexes. */ |
| 120 |
nigel |
3 |
|
| 121 |
|
|
for (i = 0; i < 256; i++) |
| 122 |
|
|
{ |
| 123 |
|
|
int x = 0; |
| 124 |
nigel |
63 |
if (i != 0x0b && isspace(i)) x += ctype_space; |
| 125 |
nigel |
3 |
if (isalpha(i)) x += ctype_letter; |
| 126 |
|
|
if (isdigit(i)) x += ctype_digit; |
| 127 |
|
|
if (isxdigit(i)) x += ctype_xdigit; |
| 128 |
|
|
if (isalnum(i) || i == '_') x += ctype_word; |
| 129 |
|
|
|
| 130 |
nigel |
73 |
/* Note: strchr includes the terminating zero in the characters it considers. |
| 131 |
|
|
In this instance, that is ok because we want binary zero to be flagged as a |
| 132 |
|
|
meta-character, which in this sense is any character that terminates a run |
| 133 |
|
|
of data characters. */ |
| 134 |
|
|
|
| 135 |
|
|
if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta; *p++ = x; } |
| 136 |
|
|
|
| 137 |
nigel |
25 |
return yield; |
| 138 |
nigel |
3 |
} |
| 139 |
|
|
|
| 140 |
|
|
/* End of maketables.c */ |