blob: 6e9366db93c6b8bbacf8dd123a55f67d97a7ec28 [file] [log] [blame]
Nick Kralevichf73ff172014-09-27 12:41:49 -07001/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
Janis Danisevskis112c9cc2016-03-31 13:35:25 +01009 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016 University of Cambridge
Nick Kralevichf73ff172014-09-27 12:41:49 -070011
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, are permitted provided that the following conditions are met:
15
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
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.
22
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.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY OF SUCH DAMAGE.
38-----------------------------------------------------------------------------
39*/
40
41
42/* This module contains internal functions for testing newlines when more than
43one kind of newline is to be recognized. When a newline is found, its length is
44returned. In principle, we could implement several newline "types", each
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010045referring to a different set of newline characters. At present, PCRE2 supports
Nick Kralevichf73ff172014-09-27 12:41:49 -070046only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
47and NLTYPE_ANY. The full list of Unicode newline characters is taken from
48http://unicode.org/unicode/reports/tr18/. */
49
50
51#ifdef HAVE_CONFIG_H
52#include "config.h"
53#endif
54
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010055#include "pcre2_internal.h"
Nick Kralevichf73ff172014-09-27 12:41:49 -070056
57
58
59/*************************************************
60* Check for newline at given position *
61*************************************************/
62
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010063/* This function is called only via the IS_NEWLINE macro, which does so only
64when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
65newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the code unit
66pointed to by ptr is less than the end of the string.
Nick Kralevichf73ff172014-09-27 12:41:49 -070067
68Arguments:
69 ptr pointer to possible newline
70 type the newline type
71 endptr pointer to the end of the string
72 lenptr where to return the length
73 utf TRUE if in utf mode
74
75Returns: TRUE or FALSE
76*/
77
78BOOL
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010079PRIV(is_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR endptr,
80 uint32_t *lenptr, BOOL utf)
Nick Kralevichf73ff172014-09-27 12:41:49 -070081{
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010082uint32_t c;
Nick Kralevichf73ff172014-09-27 12:41:49 -070083
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010084#ifdef SUPPORT_UNICODE
85if (utf) { GETCHAR(c, ptr); } else c = *ptr;
86#else
87(void)utf;
88c = *ptr;
89#endif /* SUPPORT_UNICODE */
Nick Kralevichf73ff172014-09-27 12:41:49 -070090
91if (type == NLTYPE_ANYCRLF) switch(c)
92 {
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010093 case CHAR_LF:
94 *lenptr = 1;
95 return TRUE;
96
97 case CHAR_CR:
98 *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
99 return TRUE;
100
101 default:
102 return FALSE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700103 }
104
105/* NLTYPE_ANY */
106
107else switch(c)
108 {
109#ifdef EBCDIC
110 case CHAR_NEL:
111#endif
112 case CHAR_LF:
113 case CHAR_VT:
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100114 case CHAR_FF:
115 *lenptr = 1;
116 return TRUE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700117
118 case CHAR_CR:
119 *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1;
120 return TRUE;
121
122#ifndef EBCDIC
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100123#if PCRE2_CODE_UNIT_WIDTH == 8
Nick Kralevichf73ff172014-09-27 12:41:49 -0700124 case CHAR_NEL:
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100125 *lenptr = utf? 2 : 1;
126 return TRUE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700127
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100128 case 0x2028: /* LS */
129 case 0x2029: /* PS */
130 *lenptr = 3;
131 return TRUE;
132
133#else /* 16-bit or 32-bit code units */
134 case CHAR_NEL:
135 case 0x2028: /* LS */
136 case 0x2029: /* PS */
137 *lenptr = 1;
138 return TRUE;
139#endif
140#endif /* Not EBCDIC */
141
142 default:
143 return FALSE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700144 }
145}
146
147
148
149/*************************************************
150* Check for newline at previous position *
151*************************************************/
152
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100153/* This function is called only via the WAS_NEWLINE macro, which does so only
154when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed
155newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the initial
156value of ptr is greater than the start of the string that is being processed.
Nick Kralevichf73ff172014-09-27 12:41:49 -0700157
158Arguments:
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
164
165Returns: TRUE or FALSE
166*/
167
168BOOL
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100169PRIV(was_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR startptr,
170 uint32_t *lenptr, BOOL utf)
Nick Kralevichf73ff172014-09-27 12:41:49 -0700171{
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100172uint32_t c;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700173ptr--;
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100174
175#ifdef SUPPORT_UNICODE
Nick Kralevichf73ff172014-09-27 12:41:49 -0700176if (utf)
177 {
178 BACKCHAR(ptr);
179 GETCHAR(c, ptr);
180 }
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100181else c = *ptr;
182#else
183(void)utf;
184c = *ptr;
185#endif /* SUPPORT_UNICODE */
Nick Kralevichf73ff172014-09-27 12:41:49 -0700186
187if (type == NLTYPE_ANYCRLF) switch(c)
188 {
189 case CHAR_LF:
190 *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
191 return TRUE;
192
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100193 case CHAR_CR:
194 *lenptr = 1;
195 return TRUE;
196
197 default:
198 return FALSE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700199 }
200
201/* NLTYPE_ANY */
202
203else switch(c)
204 {
205 case CHAR_LF:
206 *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1;
207 return TRUE;
208
209#ifdef EBCDIC
210 case CHAR_NEL:
211#endif
212 case CHAR_VT:
213 case CHAR_FF:
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100214 case CHAR_CR:
215 *lenptr = 1;
216 return TRUE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700217
218#ifndef EBCDIC
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100219#if PCRE2_CODE_UNIT_WIDTH == 8
Nick Kralevichf73ff172014-09-27 12:41:49 -0700220 case CHAR_NEL:
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100221 *lenptr = utf? 2 : 1;
222 return TRUE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700223
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100224 case 0x2028: /* LS */
225 case 0x2029: /* PS */
226 *lenptr = 3;
227 return TRUE;
228
229#else /* 16-bit or 32-bit code units */
230 case CHAR_NEL:
231 case 0x2028: /* LS */
232 case 0x2029: /* PS */
233 *lenptr = 1;
234 return TRUE;
235#endif
236#endif /* Not EBCDIC */
237
238 default:
239 return FALSE;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700240 }
241}
242
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100243/* End of pcre2_newline.c */