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) 2016 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 -----------------------------------------------------------------------------
42 /* This module contains internal functions for testing newlines when more than
43 one kind of newline is to be recognized. When a newline is found, its length is
44 returned. In principle, we could implement several newline "types", each
45 referring to a different set of newline characters. At present, PCRE2 supports
46 only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
47 and NLTYPE_ANY. The full list of Unicode newline characters is taken from
48 http://unicode.org/unicode/reports/tr18/. */
55 #include "pcre2_internal.h"
59 /*************************************************
60 * Check for newline at given position *
61 *************************************************/
63 /* This function is called only via the IS_NEWLINE macro, which does so only
64 when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
65 newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the code unit
66 pointed to by ptr is less than the end of the string.
69 ptr pointer to possible newline
71 endptr pointer to the end of the string
72 lenptr where to return the length
73 utf TRUE if in utf mode
75 Returns: TRUE or FALSE
79 PRIV(is_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR endptr,
80 uint32_t *lenptr, BOOL utf)
84 #ifdef SUPPORT_UNICODE
85 if (utf) { GETCHAR(c, ptr); } else c = *ptr;
89 #endif /* SUPPORT_UNICODE */
91 if (type == NLTYPE_ANYCRLF) switch(c)
98 *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
119 *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
123 #if PCRE2_CODE_UNIT_WIDTH == 8
125 *lenptr = utf? 2 : 1;
128 case 0x2028: /* LS */
129 case 0x2029: /* PS */
133 #else /* 16-bit or 32-bit code units */
135 case 0x2028: /* LS */
136 case 0x2029: /* PS */
140 #endif /* Not EBCDIC */
149 /*************************************************
150 * Check for newline at previous position *
151 *************************************************/
153 /* This function is called only via the WAS_NEWLINE macro, which does so only
154 when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
155 newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the initial
156 value of ptr is greater than the start of the string that is being processed.
159 ptr pointer to possible newline
160 type the newline type
161 startptr pointer to the start of the string
162 lenptr where to return the length
163 utf TRUE if in utf mode
165 Returns: TRUE or FALSE
169 PRIV(was_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR startptr,
170 uint32_t *lenptr, BOOL utf)
175 #ifdef SUPPORT_UNICODE
185 #endif /* SUPPORT_UNICODE */
187 if (type == NLTYPE_ANYCRLF) switch(c)
190 *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
206 *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
219 #if PCRE2_CODE_UNIT_WIDTH == 8
221 *lenptr = utf? 2 : 1;
224 case 0x2028: /* LS */
225 case 0x2029: /* PS */
229 #else /* 16-bit or 32-bit code units */
231 case 0x2028: /* LS */
232 case 0x2029: /* PS */
236 #endif /* Not EBCDIC */
243 /* End of pcre2_newline.c */