Path: blob/master/thirdparty/pcre2/src/pcre2_extuni.c
9898 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*/3940/* This module contains an internal function that is used to match a Unicode41extended grapheme sequence. It is used by both pcre2_match() and42pcre2_dfa_match(). However, it is called only when Unicode support is being43compiled. Nevertheless, we provide a dummy function when there is no Unicode44support, because some compilers do not like functionless source files. */454647#ifdef HAVE_CONFIG_H48#include "config.h"49#endif505152#include "pcre2_internal.h"535455/* Dummy function */5657#ifndef SUPPORT_UNICODE58PCRE2_SPTR59PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,60PCRE2_SPTR end_subject, BOOL utf, int *xcount)61{62(void)c;63(void)eptr;64(void)start_subject;65(void)end_subject;66(void)utf;67(void)xcount;68return NULL;69}70#else717273/*************************************************74* Match an extended grapheme sequence *75*************************************************/7677/* NOTE: The logic contained in this function is replicated in three special-78purpose functions in the pcre2_jit_compile.c module. If the logic below is79changed, they must be kept in step so that the interpreter and the JIT have the80same behaviour.8182Arguments:83c the first character84eptr pointer to next character85start_subject pointer to start of subject86end_subject pointer to end of subject87utf TRUE if in UTF mode88xcount pointer to count of additional characters,89or NULL if count not needed9091Returns: pointer after the end of the sequence92*/9394PCRE2_SPTR95PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject,96PCRE2_SPTR end_subject, BOOL utf, int *xcount)97{98BOOL was_ep_ZWJ = FALSE;99int lgb = UCD_GRAPHBREAK(c);100101while (eptr < end_subject)102{103int rgb;104int len = 1;105if (!utf) c = *eptr; else { GETCHARLEN(c, eptr, len); }106rgb = UCD_GRAPHBREAK(c);107if ((PRIV(ucp_gbtable)[lgb] & (1u << rgb)) == 0) break;108109/* ZWJ followed by Extended Pictographic is allowed only if the ZWJ was110preceded by Extended Pictographic. */111112if (lgb == ucp_gbZWJ && rgb == ucp_gbExtended_Pictographic && !was_ep_ZWJ)113break;114115/* Not breaking between Regional Indicators is allowed only if there116are an even number of preceding RIs. */117118if (lgb == ucp_gbRegional_Indicator && rgb == ucp_gbRegional_Indicator)119{120int ricount = 0;121PCRE2_SPTR bptr = eptr - 1;122if (utf) BACKCHAR(bptr);123124/* bptr is pointing to the left-hand character */125126while (bptr > start_subject)127{128bptr--;129if (utf)130{131BACKCHAR(bptr);132GETCHAR(c, bptr);133}134else135c = *bptr;136if (UCD_GRAPHBREAK(c) != ucp_gbRegional_Indicator) break;137ricount++;138}139if ((ricount & 1) != 0) break; /* Grapheme break required */140}141142/* Set a flag when ZWJ follows Extended Pictographic (with optional Extend in143between; see next statement). */144145was_ep_ZWJ = (lgb == ucp_gbExtended_Pictographic && rgb == ucp_gbZWJ);146147/* If Extend follows Extended_Pictographic, do not update lgb; this allows148any number of them before a following ZWJ. */149150if (rgb != ucp_gbExtend || lgb != ucp_gbExtended_Pictographic) lgb = rgb;151152eptr += len;153if (xcount != NULL) *xcount += 1;154}155156return eptr;157}158159#endif /* SUPPORT_UNICODE */160161/* End of pcre2_extuni.c */162163164