maketables.c

Go to the documentation of this file.
00001 
00002 /*  $Id: maketables.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /*************************************************
00005 *      Perl-Compatible Regular Expressions       *
00006 *************************************************/
00007 
00008 /*
00009 PCRE is a library of functions to support regular expressions whose syntax
00010 and semantics are as close as possible to those of the Perl 5 language.
00011 
00012 Written by: Philip Hazel <ph10@cam.ac.uk>
00013 
00014            Copyright (c) 1997-2003 University of Cambridge
00015 
00016 -----------------------------------------------------------------------------
00017 Permission is granted to anyone to use this software for any purpose on any
00018 computer system, and to redistribute it freely, subject to the following
00019 restrictions:
00020 
00021 1. This software is distributed in the hope that it will be useful,
00022    but WITHOUT ANY WARRANTY; without even the implied warranty of
00023    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00024 
00025 2. The origin of this software must not be misrepresented, either by
00026    explicit claim or by omission.
00027 
00028 3. Altered versions must be plainly marked as such, and must not be
00029    misrepresented as being the original software.
00030 
00031 4. If PCRE is embedded in any software that is released under the GNU
00032    General Purpose Licence (GPL), then the terms of that licence shall
00033    supersede any condition above with which it is incompatible.
00034 -----------------------------------------------------------------------------
00035 
00036 See the file Tech.Notes for some information on the internals.
00037 */
00038 
00039 
00040 /* This file is compiled on its own as part of the PCRE library. However,
00041 it is also included in the compilation of dftables.c, in which case the macro
00042 DFTABLES is defined. */
00043 
00044 #ifndef DFTABLES
00045 #include "pcre_internal.h"
00046 #endif
00047 
00048 
00049 
00050 /*************************************************
00051 *           Create PCRE character tables         *
00052 *************************************************/
00053 
00054 /* This function builds a set of character tables for use by PCRE and returns
00055 a pointer to them. They are build using the ctype functions, and consequently
00056 their contents will depend upon the current locale setting. When compiled as
00057 part of the library, the store is obtained via pcre_malloc(), but when compiled
00058 inside dftables, use malloc().
00059 
00060 Arguments:   none
00061 Returns:     pointer to the contiguous block of data
00062 */
00063 
00064 const unsigned char *
00065 pcre_maketables(void)
00066 {
00067 unsigned char *yield, *p;
00068 int i;
00069 
00070 #ifndef DFTABLES
00071 yield = (unsigned char*)(pcre_malloc)(tables_length);
00072 #else
00073 yield = (unsigned char*)malloc(tables_length);
00074 #endif
00075 
00076 if (yield == NULL) return NULL;
00077 p = yield;
00078 
00079 /* First comes the lower casing table */
00080 
00081 for (i = 0; i < 256; i++) *p++ = tolower(i);
00082 
00083 /* Next the case-flipping table */
00084 
00085 for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
00086 
00087 /* Then the character class tables. Don't try to be clever and save effort
00088 on exclusive ones - in some locales things may be different. Note that the
00089 table for "space" includes everything "isspace" gives, including VT in the
00090 default locale. This makes it work for the POSIX class [:space:]. */
00091 
00092 memset(p, 0, cbit_length);
00093 for (i = 0; i < 256; i++)
00094   {
00095   if (isdigit(i))
00096     {
00097     p[cbit_digit  + i/8] |= 1 << (i&7);
00098     p[cbit_word   + i/8] |= 1 << (i&7);
00099     }
00100   if (isupper(i))
00101     {
00102     p[cbit_upper  + i/8] |= 1 << (i&7);
00103     p[cbit_word   + i/8] |= 1 << (i&7);
00104     }
00105   if (islower(i))
00106     {
00107     p[cbit_lower  + i/8] |= 1 << (i&7);
00108     p[cbit_word   + i/8] |= 1 << (i&7);
00109     }
00110   if (i == '_')   p[cbit_word   + i/8] |= 1 << (i&7);
00111   if (isspace(i)) p[cbit_space  + i/8] |= 1 << (i&7);
00112   if (isxdigit(i))p[cbit_xdigit + i/8] |= 1 << (i&7);
00113   if (isgraph(i)) p[cbit_graph  + i/8] |= 1 << (i&7);
00114   if (isprint(i)) p[cbit_print  + i/8] |= 1 << (i&7);
00115   if (ispunct(i)) p[cbit_punct  + i/8] |= 1 << (i&7);
00116   if (iscntrl(i)) p[cbit_cntrl  + i/8] |= 1 << (i&7);
00117   }
00118 p += cbit_length;
00119 
00120 /* Finally, the character type table. In this, we exclude VT from the white
00121 space chars, because Perl doesn't recognize it as such for \s and for comments
00122 within regexes. */
00123 
00124 for (i = 0; i < 256; i++)
00125   {
00126   int x = 0;
00127   if (i != 0x0b && isspace(i)) x += ctype_space;
00128   if (isalpha(i)) x += ctype_letter;
00129   if (isdigit(i)) x += ctype_digit;
00130   if (isxdigit(i)) x += ctype_xdigit;
00131   if (isalnum(i) || i == '_') x += ctype_word;
00132   if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta;
00133   *p++ = x;
00134   }
00135 
00136 return yield;
00137 }
00138 
00139 /* End of maketables.c */

Generated on Wed May 31 18:19:54 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6