Path: blob/master/thirdparty/pcre2/src/pcre2_extuni.c
21731 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016-2024 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/394041/* This module contains an internal function that is used to match a Unicode42extended grapheme sequence. It is used by both pcre2_match() and43pcre2_dfa_match(). However, it is called only when Unicode support is being44compiled. Nevertheless, we provide a dummy function when there is no Unicode45support, because some compilers do not like functionless source files. */464748#include "pcre2_internal.h"49505152/* Dummy function */5354#ifndef SUPPORT_UNICODE55PCRE2_SPTR56PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,57PCRE2_SPTR end_subject, BOOL utf, int *xcount)58{59(void)c;60(void)eptr;61(void)start_subject;62(void)end_subject;63(void)utf;64(void)xcount;65return NULL;66}67#else686970/*************************************************71* Match an extended grapheme sequence *72*************************************************/7374/* NOTE: The logic contained in this function is replicated in three special-75purpose functions in the pcre2_jit_compile.c module. If the logic below is76changed, they must be kept in step so that the interpreter and the JIT have the77same behaviour.7879Arguments:80c the first character81eptr pointer to next character82start_subject pointer to start of subject83end_subject pointer to end of subject84utf TRUE if in UTF mode85xcount pointer to count of additional characters,86or NULL if count not needed8788Returns: pointer after the end of the sequence89*/9091PCRE2_SPTR92PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,93PCRE2_SPTR end_subject, BOOL utf, int *xcount)94{95BOOL was_ep_ZWJ = FALSE;96int lgb = UCD_GRAPHBREAK(c);9798while (eptr < end_subject)99{100int rgb;101int len = 1;102if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }103rgb = UCD_GRAPHBREAK(c);104if ((PRIV(ucp_gbtable)[lgb] & (1u << rgb)) == 0) break;105106/* ZWJ followed by Extended Pictographic is allowed only if the ZWJ was107preceded by Extended Pictographic. */108109if (lgb == ucp_gbZWJ && rgb == ucp_gbExtended_Pictographic && !was_ep_ZWJ)110break;111112/* Not breaking between Regional Indicators is allowed only if there113are an even number of preceding RIs. */114115if (lgb == ucp_gbRegional_Indicator && rgb == ucp_gbRegional_Indicator)116{117int ricount = 0;118PCRE2_SPTR bptr = eptr - 1;119if (utf) BACKCHAR(bptr);120121/* bptr is pointing to the left-hand character */122123while (bptr > start_subject)124{125bptr--;126if (utf)127{128BACKCHAR(bptr);129GETCHAR(c, bptr);130}131else132c = *bptr;133if (UCD_GRAPHBREAK(c) != ucp_gbRegional_Indicator) break;134ricount++;135}136if ((ricount & 1) != 0) break; /* Grapheme break required */137}138139/* Set a flag when ZWJ follows Extended Pictographic (with optional Extend in140between; see next statement). */141142was_ep_ZWJ = (lgb == ucp_gbExtended_Pictographic && rgb == ucp_gbZWJ);143144/* If Extend follows Extended_Pictographic, do not update lgb; this allows145any number of them before a following ZWJ. */146147if (rgb != ucp_gbExtend || lgb != ucp_gbExtended_Pictographic) lgb = rgb;148149eptr += len;150if (xcount != NULL) *xcount += 1;151}152153return eptr;154}155156#endif /* SUPPORT_UNICODE */157158/* End of pcre2_extuni.c */159160161