/[pcre]/code/tags/pcre-5.0/maketables.c
ViewVC logotype

Diff of /code/tags/pcre-5.0/maketables.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by nigel, Sat Feb 24 21:38:01 2007 UTC revision 63 by nigel, Sat Feb 24 21:40:03 2007 UTC
# Line 8  and semantics are as close as possible t Line 8  and semantics are as close as possible t
8    
9  Written by: Philip Hazel <ph10@cam.ac.uk>  Written by: Philip Hazel <ph10@cam.ac.uk>
10    
11             Copyright (c) 1997 University of Cambridge             Copyright (c) 1997-2003 University of Cambridge
12    
13  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
14  Permission is granted to anyone to use this software for any purpose on any  Permission is granted to anyone to use this software for any purpose on any
# Line 24  restrictions: Line 24  restrictions:
24    
25  3. Altered versions must be plainly marked as such, and must not be  3. Altered versions must be plainly marked as such, and must not be
26     misrepresented as being the original software.     misrepresented as being the original software.
27    
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  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
32    
33  See the file Tech.Notes for some information on the internals.  See the file Tech.Notes for some information on the internals.
34  */  */
35    
36    
37  /* This is a support program to generate the file chartables.c, containing  /* This file is compiled on its own as part of the PCRE library. However,
38  character tables of various kinds. They are built according to the local C  it is also included in the compilation of dftables.c, in which case the macro
39  locale. */  DFTABLES is defined. */
   
 #include <ctype.h>  
 #include <stdio.h>  
 #include <string.h>  
40    
41    #ifndef DFTABLES
42  #include "internal.h"  #include "internal.h"
43    #endif
44    
45    
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    inside dftables, use malloc().
56    
57    Arguments:   none
58    Returns:     pointer to the contiguous block of data
59    */
60    
61  int main(void)  const unsigned char *
62    pcre_maketables(void)
63  {  {
64    unsigned char *yield, *p;
65  int i;  int i;
 unsigned char cbits[cbit_length];  
66    
67  printf(  #ifndef DFTABLES
68    "/*************************************************\n"  yield = (unsigned char*)(pcre_malloc)(tables_length);
69    "*      Perl-Compatible Regular Expressions       *\n"  #else
70    "*************************************************/\n\n"  yield = (unsigned char*)malloc(tables_length);
71    "/* This file is automatically written by the makechartables auxiliary \n"  #endif
   "program. If you edit it by hand, you might like to edit the Makefile to \n"  
   "prevent its ever being regenerated. */\n\n"  
   "/* This table is a lower casing table. */\n\n"  
   "unsigned char pcre_lcc[] = {\n");  
72    
73  printf("  ");  if (yield == NULL) return NULL;
74  for (i = 0; i < 256; i++)  p = yield;
   {  
   if ((i & 7) == 0 && i != 0) printf("\n  ");  
   printf("%3d", tolower(i));  
   if (i != 255) printf(",");  
   }  
 printf(" };\n\n");  
75    
76  printf(  /* First comes the lower casing table */
   "/* This table is a case flipping table. */\n\n"  
   "unsigned char pcre_fcc[] = {\n");  
77    
78  printf("  ");  for (i = 0; i < 256; i++) *p++ = tolower(i);
 for (i = 0; i < 256; i++)  
   {  
   if ((i & 7) == 0 && i != 0) printf("\n  ");  
   printf("%3d", islower(i)? toupper(i) : tolower(i));  
   if (i != 255) printf(",");  
   }  
 printf(" };\n\n");  
79    
80  printf(  /* Next the case-flipping table */
   "/* This table contains bit maps for digits, letters, 'word' chars, and\n"  
   "white space. Each map is 32 bytes long and the bits run from the least\n"  
   "significant end of each byte. */\n\n"  
   "unsigned char pcre_cbits[] = {\n");  
81    
82  memset(cbits, 0, sizeof(cbits));  for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
83    
84  for (i = 0; i < 256; i++)  /* Then the character class tables. Don't try to be clever and save effort
85    {  on exclusive ones - in some locales things may be different. Note that the
86    if (isdigit(i)) cbits[cbit_digit  + i/8] |= 1 << (i&7);  table for "space" includes everything "isspace" gives, including VT in the
87    if (isalpha(i)) cbits[cbit_letter + i/8] |= 1 << (i&7);  default locale. This makes it work for the POSIX class [:space:]. */
   if (isalnum(i) || i == '_')  
                   cbits[cbit_word   + i/8] |= 1 << (i&7);  
   if (isspace(i)) cbits[cbit_space  + i/8] |= 1 << (i&7);  
   }  
88    
89  printf("  ");  memset(p, 0, cbit_length);
90  for (i = 0; i < cbit_length; i++)  for (i = 0; i < 256; i++)
91    {    {
92    if ((i & 7) == 0 && i != 0)    if (isdigit(i))
93      {      {
94      if ((i & 31) == 0) printf("\n");      p[cbit_digit  + i/8] |= 1 << (i&7);
95      printf("\n  ");      p[cbit_word   + i/8] |= 1 << (i&7);
96      }      }
97    printf("0x%02x", cbits[i]);    if (isupper(i))
98    if (i != cbit_length - 1) printf(",");      {
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      if (isspace(i)) p[cbit_space  + i/8] |= 1 << (i&7);
109      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    }    }
115  printf(" };\n\n");  p += cbit_length;
   
 printf(  
   "/* This table identifies various classes of character by individual bits:\n"  
   "  0x%02x   white space character\n"  
   "  0x%02x   letter\n"  
   "  0x%02x   decimal digit\n"  
   "  0x%02x   hexadecimal digit\n"  
   "  0x%02x   alphanumeric or '_'\n"  
   "  0x%02x   regular expression metacharacter or binary zero\n*/\n\n",  
   ctype_space, ctype_letter, ctype_digit, ctype_xdigit, ctype_word,  
   ctype_meta);  
116    
117  printf("unsigned char pcre_ctypes[] = {\n");  /* 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    
 printf("  ");  
121  for (i = 0; i < 256; i++)  for (i = 0; i < 256; i++)
122    {    {
123    int x = 0;    int x = 0;
124    if (isspace(i)) x += ctype_space;    if (i != 0x0b && isspace(i)) x += ctype_space;
125    if (isalpha(i)) x += ctype_letter;    if (isalpha(i)) x += ctype_letter;
126    if (isdigit(i)) x += ctype_digit;    if (isdigit(i)) x += ctype_digit;
127    if (isxdigit(i)) x += ctype_xdigit;    if (isxdigit(i)) x += ctype_xdigit;
128    if (isalnum(i) || i == '_') x += ctype_word;    if (isalnum(i) || i == '_') x += ctype_word;
129    if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta;    if (strchr("*+?{^.$|()[", i) != 0) x += ctype_meta;
130      *p++ = x;
   if ((i & 7) == 0 && i != 0)  
     {  
     printf(" /* ");  
     if (isprint(i-8)) printf(" %c -", i-8);  
       else printf("%3d-", i-8);  
     if (isprint(i-1)) printf(" %c ", i-1);  
       else printf("%3d", i-1);  
     printf(" */\n  ");  
     }  
   printf("0x%02x", x);  
   if (i != 255) printf(",");  
131    }    }
132    
133  printf("};/* ");  return yield;
 if (isprint(i-8)) printf(" %c -", i-8);  
   else printf("%3d-", i-8);  
 if (isprint(i-1)) printf(" %c ", i-1);  
   else printf("%3d", i-1);  
 printf(" */\n\n/* End of chartables.c */\n");  
   
 return 0;  
134  }  }
135    
136  /* End of maketables.c */  /* End of maketables.c */

Legend:
Removed from v.3  
changed lines
  Added in v.63

  ViewVC Help
Powered by ViewVC 1.1.5