1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2018 University of Cambridge
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
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.
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.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 -----------------------------------------------------------------------------
41 /* This module contains internal functions for comparing and finding the length
42 of strings. These are used instead of strcmp() etc because the standard
43 functions work only on 8-bit data. */
50 #include "pcre2_internal.h"
53 /*************************************************
54 * Emulated memmove() for systems without it *
55 *************************************************/
57 /* This function can make use of bcopy() if it is available. Otherwise do it by
58 steam, as there some non-Unix environments that lack both memmove() and
61 #if !defined(VPCOMPAT) && !defined(HAVE_MEMMOVE)
63 PRIV(memmove)(void *d, const void *s, size_t n)
70 unsigned char *dest = (unsigned char *)d;
71 const unsigned char *src = (const unsigned char *)s;
76 for (i = 0; i < n; ++i) *(--dest) = *(--src);
81 for (i = 0; i < n; ++i) *dest++ = *src++;
82 return (void *)(dest - n);
84 #endif /* not HAVE_BCOPY */
86 #endif /* not VPCOMPAT && not HAVE_MEMMOVE */
89 /*************************************************
90 * Compare two zero-terminated PCRE2 strings *
91 *************************************************/
102 PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2)
105 while (*str1 != '\0' || *str2 != '\0')
109 if (c1 != c2) return ((c1 > c2) << 1) - 1;
115 /*************************************************
116 * Compare zero-terminated PCRE2 & 8-bit strings *
117 *************************************************/
119 /* As the 8-bit string is almost always a literal, its type is specified as
130 PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2)
133 while (*str1 != '\0' || *str2 != '\0')
137 if (c1 != c2) return ((c1 > c2) << 1) - 1;
143 /*************************************************
144 * Compare two PCRE2 strings, given a length *
145 *************************************************/
157 PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)
160 for (; len > 0; len--)
164 if (c1 != c2) return ((c1 > c2) << 1) - 1;
170 /*************************************************
171 * Compare PCRE2 string to 8-bit string by length *
172 *************************************************/
174 /* As the 8-bit string is almost always a literal, its type is specified as
186 PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len)
189 for (; len > 0; len--)
193 if (c1 != c2) return ((c1 > c2) << 1) - 1;
199 /*************************************************
200 * Find the length of a PCRE2 string *
201 *************************************************/
209 PRIV(strlen)(PCRE2_SPTR str)
212 while (*str++ != 0) c++;
217 /*************************************************
218 * Copy 8-bit 0-terminated string to PCRE2 string *
219 *************************************************/
222 str1 buffer to receive the string
223 str2 8-bit string to be copied
225 Returns: the number of code units used (excluding trailing zero)
229 PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2)
231 PCRE2_UCHAR *t = str1;
232 while (*str2 != 0) *t++ = *str2++;
237 /* End of pcre2_string_utils.c */