Path: blob/master/thirdparty/pcre2/src/pcre2_string_utils.c
21418 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*/394041/* This module contains internal functions for comparing and finding the length42of strings. These are used instead of strcmp() etc because the standard43functions work only on 8-bit data. */444546#include "pcre2_internal.h"47484950/*************************************************51* Compare two zero-terminated PCRE2 strings *52*************************************************/5354/*55Arguments:56str1 first string57str2 second string5859Returns: 0, 1, or -160*/6162int63PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2)64{65PCRE2_UCHAR c1, c2;66while (*str1 != '\0' || *str2 != '\0')67{68c1 = *str1++;69c2 = *str2++;70if (c1 != c2) return ((c1 > c2) << 1) - 1;71}72return 0;73}747576/*************************************************77* Compare zero-terminated PCRE2 & 8-bit strings *78*************************************************/7980/* As the 8-bit string is almost always a literal, its type is specified as81const char *.8283Arguments:84str1 first string85str2 second string8687Returns: 0, 1, or -188*/8990int91PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2)92{93PCRE2_UCHAR c1, c2;94while (*str1 != '\0' || *str2 != '\0')95{96c1 = *str1++;97c2 = *str2++;98if (c1 != c2) return ((c1 > c2) << 1) - 1;99}100return 0;101}102103104/*************************************************105* Compare two PCRE2 strings, given a length *106*************************************************/107108/*109Arguments:110str1 first string111str2 second string112len the length113114Returns: 0, 1, or -1115*/116117int118PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)119{120PCRE2_UCHAR c1, c2;121for (; len > 0; len--)122{123c1 = *str1++;124c2 = *str2++;125if (c1 != c2) return ((c1 > c2) << 1) - 1;126}127return 0;128}129130131/*************************************************132* Compare PCRE2 string to 8-bit string by length *133*************************************************/134135/* As the 8-bit string is almost always a literal, its type is specified as136const char *.137138Arguments:139str1 first string140str2 second string141len the length142143Returns: 0, 1, or -1144*/145146int147PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len)148{149PCRE2_UCHAR c1, c2;150for (; len > 0; len--)151{152c1 = *str1++;153c2 = *str2++;154if (c1 != c2) return ((c1 > c2) << 1) - 1;155}156return 0;157}158159160/*************************************************161* Find the length of a PCRE2 string *162*************************************************/163164/*165Argument: the string166Returns: the length167*/168169PCRE2_SIZE170PRIV(strlen)(PCRE2_SPTR str)171{172PCRE2_SIZE c = 0;173while (*str++ != 0) c++;174return c;175}176177178/*************************************************179* Copy 8-bit 0-terminated string to PCRE2 string *180*************************************************/181182/* Arguments:183str1 buffer to receive the string184str2 8-bit string to be copied185186Returns: the number of code units used (excluding trailing zero)187*/188189PCRE2_SIZE190PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2)191{192PCRE2_UCHAR *t = str1;193while (*str2 != 0) *t++ = *str2++;194*t = 0;195return t - str1;196}197198/* End of pcre2_string_utils.c */199200201