Path: blob/master/thirdparty/pcre2/src/pcre2_string_utils.c
9903 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) 2018-2021 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 internal functions for comparing and finding the length41of strings. These are used instead of strcmp() etc because the standard42functions work only on 8-bit data. */434445#ifdef HAVE_CONFIG_H46#include "config.h"47#endif4849#include "pcre2_internal.h"505152/*************************************************53* Emulated memmove() for systems without it *54*************************************************/5556/* This function can make use of bcopy() if it is available. Otherwise do it by57steam, as there some non-Unix environments that lack both memmove() and58bcopy(). */5960#if !defined(VPCOMPAT) && !defined(HAVE_MEMMOVE)61void *62PRIV(memmove)(void *d, const void *s, size_t n)63{64#ifdef HAVE_BCOPY65bcopy(s, d, n);66return d;67#else68size_t i;69unsigned char *dest = (unsigned char *)d;70const unsigned char *src = (const unsigned char *)s;71if (dest > src)72{73dest += n;74src += n;75for (i = 0; i < n; ++i) *(--dest) = *(--src);76return (void *)dest;77}78else79{80for (i = 0; i < n; ++i) *dest++ = *src++;81return (void *)(dest - n);82}83#endif /* not HAVE_BCOPY */84}85#endif /* not VPCOMPAT && not HAVE_MEMMOVE */868788/*************************************************89* Compare two zero-terminated PCRE2 strings *90*************************************************/9192/*93Arguments:94str1 first string95str2 second string9697Returns: 0, 1, or -198*/99100int101PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2)102{103PCRE2_UCHAR c1, c2;104while (*str1 != '\0' || *str2 != '\0')105{106c1 = *str1++;107c2 = *str2++;108if (c1 != c2) return ((c1 > c2) << 1) - 1;109}110return 0;111}112113114/*************************************************115* Compare zero-terminated PCRE2 & 8-bit strings *116*************************************************/117118/* As the 8-bit string is almost always a literal, its type is specified as119const char *.120121Arguments:122str1 first string123str2 second string124125Returns: 0, 1, or -1126*/127128int129PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2)130{131PCRE2_UCHAR c1, c2;132while (*str1 != '\0' || *str2 != '\0')133{134c1 = *str1++;135c2 = *str2++;136if (c1 != c2) return ((c1 > c2) << 1) - 1;137}138return 0;139}140141142/*************************************************143* Compare two PCRE2 strings, given a length *144*************************************************/145146/*147Arguments:148str1 first string149str2 second string150len the length151152Returns: 0, 1, or -1153*/154155int156PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)157{158PCRE2_UCHAR c1, c2;159for (; len > 0; len--)160{161c1 = *str1++;162c2 = *str2++;163if (c1 != c2) return ((c1 > c2) << 1) - 1;164}165return 0;166}167168169/*************************************************170* Compare PCRE2 string to 8-bit string by length *171*************************************************/172173/* As the 8-bit string is almost always a literal, its type is specified as174const char *.175176Arguments:177str1 first string178str2 second string179len the length180181Returns: 0, 1, or -1182*/183184int185PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len)186{187PCRE2_UCHAR c1, c2;188for (; len > 0; len--)189{190c1 = *str1++;191c2 = *str2++;192if (c1 != c2) return ((c1 > c2) << 1) - 1;193}194return 0;195}196197198/*************************************************199* Find the length of a PCRE2 string *200*************************************************/201202/*203Argument: the string204Returns: the length205*/206207PCRE2_SIZE208PRIV(strlen)(PCRE2_SPTR str)209{210PCRE2_SIZE c = 0;211while (*str++ != 0) c++;212return c;213}214215216/*************************************************217* Copy 8-bit 0-terminated string to PCRE2 string *218*************************************************/219220/* Arguments:221str1 buffer to receive the string222str2 8-bit string to be copied223224Returns: the number of code units used (excluding trailing zero)225*/226227PCRE2_SIZE228PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2)229{230PCRE2_UCHAR *t = str1;231while (*str2 != 0) *t++ = *str2++;232*t = 0;233return t - str1;234}235236/* End of pcre2_string_utils.c */237238239