1 /* This is JavaScriptCore's variant of the PCRE library. While this library
2 started out as a copy of PCRE, many of the features of PCRE have been
3 removed. This library now supports only the regular expression features
4 required by the JavaScript language specification, and has only the functions
5 needed by JavaScriptCore and the rest of WebKit.
7 Originally written by Philip Hazel
8 Copyright (c) 1997-2006 University of Cambridge
9 Copyright (C) 2002, 2004, 2006, 2007 Apple Inc. All rights reserved.
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
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.
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.
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 -----------------------------------------------------------------------------
40 /* This header contains definitions that are shared between the different
41 modules, but which are not relevant to the exported API. This includes some
42 functions whose names all begin with "_pcre_". */
44 #ifndef PCRE_INTERNAL_H
45 #define PCRE_INTERNAL_H
47 /* Bit definitions for entries in the pcre_ctypes table. */
49 #define ctype_space 0x01
50 #define ctype_xdigit 0x08
51 #define ctype_word 0x10 /* alphameric or '_' */
53 /* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
54 of bits for a class map. Some classes are built by combining these tables. */
56 #define cbit_space 0 /* \s */
57 #define cbit_digit 32 /* \d */
58 #define cbit_word 64 /* \w */
59 #define cbit_length 96 /* Length of the cbits table */
61 /* Offsets of the various tables from the base tables pointer, and
65 #define fcc_offset 128
66 #define cbits_offset 256
67 #define ctypes_offset (cbits_offset + cbit_length)
68 #define tables_length (ctypes_offset + 128)
72 #include "Assertions.h"
75 #pragma warning(disable: 4232)
76 #pragma warning(disable: 4244)
81 /* The value of LINK_SIZE determines the number of bytes used to store links as
82 offsets within the compiled regex. The default is 2, which allows for compiled
83 patterns up to 64K long. */
87 /* Define DEBUG to get debugging output on stdout. */
93 /* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
94 inline, and there are *still* stupid compilers about that don't like indented
95 pre-processor statements, or at least there were when I first wrote this. After
96 all, it had only been about 10 years then... */
99 #define DPRINTF(p) printf p
101 #define DPRINTF(p) /*nothing*/
104 /* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
105 in big-endian order) by default. These are used, for example, to link from the
106 start of a subpattern to its alternatives and its end. The use of 2 bytes per
107 offset limits the size of the compiled regex to around 64K, which is big enough
108 for almost everybody. However, I received a request for an even bigger limit.
109 For this reason, and also to make the code easier to maintain, the storing and
110 loading of offsets from the byte string is now handled by the functions that are
113 /* PCRE uses some other 2-byte quantities that do not change when the size of
114 offsets changes. There are used for repeat counts and for other things such as
115 capturing parenthesis numbers in back references. */
117 static inline void put2ByteValue(unsigned char* opcodePtr
, int value
)
119 ASSERT(value
>= 0 && value
<= 0xFFFF);
120 opcodePtr
[0] = value
>> 8;
121 opcodePtr
[1] = value
;
124 static inline int get2ByteValue(const unsigned char* opcodePtr
)
126 return (opcodePtr
[0] << 8) | opcodePtr
[1];
129 static inline void put2ByteValueAndAdvance(unsigned char*& opcodePtr
, int value
)
131 put2ByteValue(opcodePtr
, value
);
135 static inline void putLinkValueAllowZero(unsigned char* opcodePtr
, int value
)
137 put2ByteValue(opcodePtr
, value
);
140 static inline int getLinkValueAllowZero(const unsigned char* opcodePtr
)
142 return get2ByteValue(opcodePtr
);
145 #define MAX_PATTERN_SIZE (1 << 16)
147 static inline void putLinkValue(unsigned char* opcodePtr
, int value
)
150 putLinkValueAllowZero(opcodePtr
, value
);
153 static inline int getLinkValue(const unsigned char* opcodePtr
)
155 int value
= getLinkValueAllowZero(opcodePtr
);
160 static inline void putLinkValueAndAdvance(unsigned char*& opcodePtr
, int value
)
162 putLinkValue(opcodePtr
, value
);
163 opcodePtr
+= LINK_SIZE
;
166 static inline void putLinkValueAllowZeroAndAdvance(unsigned char*& opcodePtr
, int value
)
168 putLinkValueAllowZero(opcodePtr
, value
);
169 opcodePtr
+= LINK_SIZE
;
172 // FIXME: These are really more of a "compiled regexp state" than "regexp options"
174 UseFirstByteOptimizationOption
= 0x40000000, /* first_byte is set */
175 UseRequiredByteOptimizationOption
= 0x20000000, /* req_byte is set */
176 UseMultiLineFirstByteOptimizationOption
= 0x10000000, /* start after \n for multiline */
177 IsAnchoredOption
= 0x02000000, /* can't use partial with this regex */
178 IgnoreCaseOption
= 0x00000001,
179 MatchAcrossMultipleLinesOption
= 0x00000002
182 /* Flags added to firstbyte or reqbyte; a "non-literal" item is either a
183 variable-length repeat, or a anything other than literal characters. */
185 #define REQ_IGNORE_CASE 0x0100 /* indicates should ignore case */
186 #define REQ_VARY 0x0200 /* reqbyte followed non-literal item */
188 /* Miscellaneous definitions */
190 /* Flag bits and data types for the extended class (OP_XCLASS) for classes that
191 contain UTF-8 characters with values greater than 255. */
193 #define XCL_NOT 0x01 /* Flag: this is a negative class */
194 #define XCL_MAP 0x02 /* Flag: a 32-byte map is present */
196 #define XCL_END 0 /* Marks end of individual items */
197 #define XCL_SINGLE 1 /* Single item (one multibyte char) follows */
198 #define XCL_RANGE 2 /* A range (two multibyte chars) follows */
200 /* These are escaped items that aren't just an encoding of a particular data
201 value such as \n. They must have non-zero values, as check_escape() returns
202 their negation. Also, they must appear in the same order as in the opcode
203 definitions below, up to ESC_w. The final one must be
204 ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
205 tests in the code for an escape > ESC_b and <= ESC_w to
206 detect the types that may be repeated. These are the types that consume
207 characters. If any new escapes are put in between that don't consume a
208 character, that code will have to change. */
210 enum { ESC_B
= 1, ESC_b
, ESC_D
, ESC_d
, ESC_S
, ESC_s
, ESC_W
, ESC_w
, ESC_REF
};
212 /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
213 that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
214 OP_EOD must correspond in order to the list of escapes immediately above.
215 Note that whenever this list is updated, the two macro definitions that follow
216 must also be updated to match. */
218 #define FOR_EACH_OPCODE(macro) \
221 macro(NOT_WORD_BOUNDARY) \
222 macro(WORD_BOUNDARY) \
225 macro(NOT_WHITESPACE) \
227 macro(NOT_WORDCHAR) \
235 macro(CHAR_IGNORING_CASE) \
237 macro(ASCII_LETTER_IGNORING_CASE) \
265 macro(TYPEMINQUERY) \
298 #define OPCODE_ENUM_VALUE(opcode) OP_##opcode,
299 enum { FOR_EACH_OPCODE(OPCODE_ENUM_VALUE
) };
301 /* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and
302 study.c that all opcodes are less than 128 in value. This makes handling UTF-8
303 character sequences easier. */
305 /* The highest extraction number before we have to start using additional
306 bytes. (Originally PCRE didn't have support for extraction counts higher than
307 this number.) The value is limited by the number of opcodes left after OP_BRA,
308 i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
311 /* FIXME: Note that OP_BRA + 100 is > 128, so the two comments above
314 #define EXTRACT_BASIC_MAX 100
316 /* The index of names and the
317 code vector run on as long as necessary after the end. We store an explicit
318 offset to the name table so that if a regex is compiled on one host, saved, and
319 then run on another where the size of pointers is different, all might still
320 be well. For the case of compiled-on-4 and run-on-8, we include an extra
321 pointer that is always NULL.
327 unsigned short top_bracket
;
328 unsigned short top_backref
;
330 unsigned short first_byte
;
331 unsigned short req_byte
;
334 /* Internal shared data tables. These are tables that are used by more than one
335 of the exported public functions. They have to be "external" in the C sense,
336 but are not part of the PCRE public API. The data for these tables is in the
337 pcre_tables.c module. */
339 #define kjs_pcre_utf8_table1_size 6
341 extern const int kjs_pcre_utf8_table1
[6];
342 extern const int kjs_pcre_utf8_table2
[6];
343 extern const int kjs_pcre_utf8_table3
[6];
344 extern const unsigned char kjs_pcre_utf8_table4
[0x40];
346 extern const unsigned char kjs_pcre_default_tables
[tables_length
];
348 static inline unsigned char toLowerCase(unsigned char c
)
350 static const unsigned char* lowerCaseChars
= kjs_pcre_default_tables
+ lcc_offset
;
351 return lowerCaseChars
[c
];
354 static inline unsigned char flipCase(unsigned char c
)
356 static const unsigned char* flippedCaseChars
= kjs_pcre_default_tables
+ fcc_offset
;
357 return flippedCaseChars
[c
];
360 static inline unsigned char classBitmapForChar(unsigned char c
)
362 static const unsigned char* charClassBitmaps
= kjs_pcre_default_tables
+ cbits_offset
;
363 return charClassBitmaps
[c
];
366 static inline unsigned char charTypeForChar(unsigned char c
)
368 const unsigned char* charTypeMap
= kjs_pcre_default_tables
+ ctypes_offset
;
369 return charTypeMap
[c
];
372 static inline bool isWordChar(UChar c
)
374 return c
< 128 && (charTypeForChar(c
) & ctype_word
);
377 static inline bool isSpaceChar(UChar c
)
379 return c
< 128 && (charTypeForChar(c
) & ctype_space
);
382 static inline bool isNewline(UChar nl
)
384 return (nl
== 0xA || nl
== 0xD || nl
== 0x2028 || nl
== 0x2029);
387 static inline bool isBracketStartOpcode(unsigned char opcode
)
389 if (opcode
>= OP_BRA
)
400 static inline void advanceToEndOfBracket(const unsigned char*& opcodePtr
)
402 ASSERT(isBracketStartOpcode(*opcodePtr
) || *opcodePtr
== OP_ALT
);
404 opcodePtr
+= getLinkValue(opcodePtr
+ 1);
405 while (*opcodePtr
== OP_ALT
);
408 /* Internal shared functions. These are functions that are used in more
409 that one of the source files. They have to have external linkage, but
410 but are not part of the public API and so not exported from the library. */
412 extern int kjs_pcre_ucp_othercase(unsigned);
413 extern bool kjs_pcre_xclass(int, const unsigned char*);
419 /* End of pcre_internal.h */