php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
pcre2_extuni.c
Go to the documentation of this file.
1/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016-2024 University of Cambridge
11
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, are permitted provided that the following conditions are met:
15
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY OF SUCH DAMAGE.
38-----------------------------------------------------------------------------
39*/
40
41/* This module contains an internal function that is used to match a Unicode
42extended grapheme sequence. It is used by both pcre2_match() and
43pcre2_def_match(). However, it is called only when Unicode support is being
44compiled. Nevertheless, we provide a dummy function when there is no Unicode
45support, because some compilers do not like functionless source files. */
46
47
48#ifdef HAVE_CONFIG_H
49#include "config.h"
50#endif
51
52
53#include "pcre2_internal.h"
54
55
56/* Dummy function */
57
58#ifndef SUPPORT_UNICODE
60PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,
61 PCRE2_SPTR end_subject, BOOL utf, int *xcount)
62{
63(void)c;
64(void)eptr;
65(void)start_subject;
66(void)end_subject;
67(void)utf;
68(void)xcount;
69return NULL;
70}
71#else
72
73
74/*************************************************
75* Match an extended grapheme sequence *
76*************************************************/
77
78/* NOTE: The logic contained in this function is replicated in three special-
79purpose functions in the pcre2_jit_compile.c module. If the logic below is
80changed, they must be kept in step so that the interpreter and the JIT have the
81same behaviour.
82
83Arguments:
84 c the first character
85 eptr pointer to next character
86 start_subject pointer to start of subject
87 end_subject pointer to end of subject
88 utf TRUE if in UTF mode
89 xcount pointer to count of additional characters,
90 or NULL if count not needed
91
92Returns: pointer after the end of the sequence
93*/
94
96PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,
97 PCRE2_SPTR end_subject, BOOL utf, int *xcount)
98{
99BOOL was_ep_ZWJ = FALSE;
100int lgb = UCD_GRAPHBREAK(c);
101
102while (eptr < end_subject)
103 {
104 int rgb;
105 int len = 1;
106 if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }
107 rgb = UCD_GRAPHBREAK(c);
108 if ((PRIV(ucp_gbtable)[lgb] & (1u << rgb)) == 0) break;
109
110 /* ZWJ followed by Extended Pictographic is allowed only if the ZWJ was
111 preceded by Extended Pictographic. */
112
113 if (lgb == ucp_gbZWJ && rgb == ucp_gbExtended_Pictographic && !was_ep_ZWJ)
114 break;
115
116 /* Not breaking between Regional Indicators is allowed only if there
117 are an even number of preceding RIs. */
118
120 {
121 int ricount = 0;
122 PCRE2_SPTR bptr = eptr - 1;
123 if (utf) BACKCHAR(bptr);
124
125 /* bptr is pointing to the left-hand character */
126
127 while (bptr > start_subject)
128 {
129 bptr--;
130 if (utf)
131 {
132 BACKCHAR(bptr);
133 GETCHAR(c, bptr);
134 }
135 else
136 c = *bptr;
138 ricount++;
139 }
140 if ((ricount & 1) != 0) break; /* Grapheme break required */
141 }
142
143 /* Set a flag when ZWJ follows Extended Pictographic (with optional Extend in
144 between; see next statement). */
145
146 was_ep_ZWJ = (lgb == ucp_gbExtended_Pictographic && rgb == ucp_gbZWJ);
147
148 /* If Extend follows Extended_Pictographic, do not update lgb; this allows
149 any number of them before a following ZWJ. */
150
151 if (rgb != ucp_gbExtend || lgb != ucp_gbExtended_Pictographic) lgb = rgb;
152
153 eptr += len;
154 if (xcount != NULL) *xcount += 1;
155 }
156
157return eptr;
158}
159
160#endif /* SUPPORT_UNICODE */
161
162/* End of pcre2_extuni.c */
size_t len
Definition apprentice.c:174
#define FALSE
Definition gd_gd.c:8
#define NULL
Definition gdcache.h:45
#define PCRE2_SPTR
Definition pcre2.h:820
PCRE2_SPTR PRIV extuni(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject, PCRE2_SPTR end_subject, BOOL utf, int *xcount)
int BOOL
#define PRIV(name)
#define UCD_GRAPHBREAK(ch)
#define GETCHAR(c, eptr)
#define GETCHARLEN(c, eptr, len)
@ ucp_gbExtend
Definition pcre2_ucp.h:201
@ ucp_gbRegional_Indicator
Definition pcre2_ucp.h:209
@ ucp_gbZWJ
Definition pcre2_ucp.h:211
@ ucp_gbExtended_Pictographic
Definition pcre2_ucp.h:212
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)