1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
8 Written by Philip Hazel
9 Copyright (c) 1997-2013 University of Cambridge
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 -----------------------------------------------------------------------------
41 /* This module contains an internal function that is used to match an extended
42 class. It is used by both pcre_exec() and pcre_def_exec(). */
49 #include "pcre_internal.h"
52 /*************************************************
53 * Match character against an XCLASS *
54 *************************************************/
56 /* This function is called to match a character against an extended class that
57 might contain values > 255 and/or Unicode properties.
61 data points to the flag byte of the XCLASS data
63 Returns: TRUE if character matches, else FALSE
67 PRIV(xclass
)(pcre_uint32 c
, const pcre_uchar
*data
, BOOL utf
)
70 BOOL negated
= (*data
& XCL_NOT
) != 0;
74 /* In 8 bit mode, this must always be TRUE. Help the compiler to know that. */
78 /* Character values < 256 are matched against a bitmap, if one is present. If
79 not, we still carry on, because there may be ranges that start below 256 in the
84 if ((*data
& XCL_HASPROP
) == 0)
86 if ((*data
& XCL_MAP
) == 0) return negated
;
87 return (((pcre_uint8
*)(data
+ 1))[c
/8] & (1 << (c
&7))) != 0;
89 if ((*data
& XCL_MAP
) != 0 &&
90 (((pcre_uint8
*)(data
+ 1))[c
/8] & (1 << (c
&7))) != 0)
91 return !negated
; /* char found */
94 /* First skip the bit map if present. Then match against the list of Unicode
95 properties or large chars or ranges that end with a large char. We won't ever
96 encounter XCL_PROP or XCL_NOTPROP when UCP support is not compiled. */
98 if ((*data
++ & XCL_MAP
) != 0) data
+= 32 / sizeof(pcre_uchar
);
100 while ((t
= *data
++) != XCL_END
)
108 GETCHARINC(x
, data
); /* macro generates multiple statements */
113 if (c
== x
) return !negated
;
115 else if (t
== XCL_RANGE
)
120 GETCHARINC(x
, data
); /* macro generates multiple statements */
121 GETCHARINC(y
, data
); /* macro generates multiple statements */
129 if (c
>= x
&& c
<= y
) return !negated
;
133 else /* XCL_PROP & XCL_NOTPROP */
135 const ucd_record
*prop
= GET_UCD(c
);
136 BOOL isprop
= t
== XCL_PROP
;
141 if (isprop
) return !negated
;
145 if ((prop
->chartype
== ucp_Lu
|| prop
->chartype
== ucp_Ll
||
146 prop
->chartype
== ucp_Lt
) == isprop
) return !negated
;
150 if ((data
[1] == PRIV(ucp_gentype
)[prop
->chartype
]) == isprop
)
155 if ((data
[1] == prop
->chartype
) == isprop
) return !negated
;
159 if ((data
[1] == prop
->script
) == isprop
) return !negated
;
163 if ((PRIV(ucp_gentype
)[prop
->chartype
] == ucp_L
||
164 PRIV(ucp_gentype
)[prop
->chartype
] == ucp_N
) == isprop
)
168 /* Perl space used to exclude VT, but from Perl 5.18 it is included,
169 which means that Perl space and POSIX space are now identical. PCRE
170 was changed at release 8.34. */
172 case PT_SPACE
: /* Perl space */
173 case PT_PXSPACE
: /* POSIX space */
178 if (isprop
) return !negated
;
182 if ((PRIV(ucp_gentype
)[prop
->chartype
] == ucp_Z
) == isprop
)
189 if ((PRIV(ucp_gentype
)[prop
->chartype
] == ucp_L
||
190 PRIV(ucp_gentype
)[prop
->chartype
] == ucp_N
|| c
== CHAR_UNDERSCORE
)
198 if ((c
== CHAR_DOLLAR_SIGN
|| c
== CHAR_COMMERCIAL_AT
||
199 c
== CHAR_GRAVE_ACCENT
) == isprop
)
204 if ((c
< 0xd800 || c
> 0xdfff) == isprop
)
209 /* The following three properties can occur only in an XCLASS, as there
210 is no \p or \P coding for them. */
212 /* Graphic character. Implement this as not Z (space or separator) and
213 not C (other), except for Cf (format) with a few exceptions. This seems
214 to be what Perl does. The exceptional characters are:
216 U+061C Arabic Letter Mark
217 U+180E Mongolian Vowel Separator
218 U+2066 - U+2069 Various "isolate"s
222 if ((PRIV(ucp_gentype
)[prop
->chartype
] != ucp_Z
&&
223 (PRIV(ucp_gentype
)[prop
->chartype
] != ucp_C
||
224 (prop
->chartype
== ucp_Cf
&&
225 c
!= 0x061c && c
!= 0x180e && (c
< 0x2066 || c
> 0x2069))
230 /* Printable character: same as graphic, with the addition of Zs, i.e.
231 not Zl and not Zp, and U+180E. */
234 if ((prop
->chartype
!= ucp_Zl
&&
235 prop
->chartype
!= ucp_Zp
&&
236 (PRIV(ucp_gentype
)[prop
->chartype
] != ucp_C
||
237 (prop
->chartype
== ucp_Cf
&&
238 c
!= 0x061c && (c
< 0x2066 || c
> 0x2069))
243 /* Punctuation: all Unicode punctuation, plus ASCII characters that
244 Unicode treats as symbols rather than punctuation, for Perl
245 compatibility (these are $+<=>^`|~). */
248 if ((PRIV(ucp_gentype
)[prop
->chartype
] == ucp_P
||
249 (c
< 256 && PRIV(ucp_gentype
)[prop
->chartype
] == ucp_S
)) == isprop
)
253 /* This should never occur, but compilers may mutter if there is no
262 #endif /* SUPPORT_UCP */
265 return negated
; /* char did not match */
268 /* End of pcre_xclass.c */