/src/pcre2/src/pcre2_newline.c
Line | Count | Source |
1 | | /************************************************* |
2 | | * Perl-Compatible Regular Expressions * |
3 | | *************************************************/ |
4 | | |
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. |
7 | | |
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 |
11 | | |
12 | | ----------------------------------------------------------------------------- |
13 | | Redistribution and use in source and binary forms, with or without |
14 | | modification, 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 | | |
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 | | ----------------------------------------------------------------------------- |
39 | | */ |
40 | | |
41 | | |
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/. */ |
49 | | |
50 | | |
51 | | #include "pcre2_internal.h" |
52 | | |
53 | | |
54 | | |
55 | | /************************************************* |
56 | | * Check for newline at given position * |
57 | | *************************************************/ |
58 | | |
59 | | /* This function is called only via the IS_NEWLINE macro, which does so only |
60 | | when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed |
61 | | newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the code unit |
62 | | pointed to by ptr is less than the end of the string. |
63 | | |
64 | | Arguments: |
65 | | ptr pointer to possible newline |
66 | | type the newline type |
67 | | endptr pointer to the end of the string |
68 | | lenptr where to return the length |
69 | | utf TRUE if in utf mode |
70 | | |
71 | | Returns: TRUE or FALSE |
72 | | */ |
73 | | |
74 | | BOOL |
75 | | PRIV(is_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR endptr, uint32_t *lenptr, BOOL utf) |
76 | 0 | { |
77 | 0 | uint32_t c; |
78 | 0 | #ifdef SUPPORT_UNICODE |
79 | 0 | if (utf) |
80 | 0 | { |
81 | 0 | GETCHAR(c, ptr); |
82 | 0 | } |
83 | 0 | else |
84 | 0 | { |
85 | 0 | c = *ptr; |
86 | 0 | } |
87 | | #else |
88 | | (void)utf; |
89 | | c = *ptr; |
90 | | #endif /* SUPPORT_UNICODE */ |
91 | |
|
92 | 0 | if (type == NLTYPE_ANYCRLF) |
93 | 0 | { |
94 | 0 | switch (c) |
95 | 0 | { |
96 | 0 | case CHAR_LF: |
97 | 0 | *lenptr = 1; |
98 | 0 | return TRUE; |
99 | | |
100 | 0 | case CHAR_CR: |
101 | 0 | *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF) ? 2 : 1; |
102 | 0 | return TRUE; |
103 | | |
104 | 0 | default: |
105 | 0 | return FALSE; |
106 | 0 | } |
107 | 0 | } |
108 | | |
109 | | /* NLTYPE_ANY */ |
110 | | |
111 | 0 | else |
112 | 0 | { |
113 | 0 | switch (c) |
114 | 0 | { |
115 | | #ifdef EBCDIC |
116 | | case CHAR_NEL: |
117 | | #endif |
118 | 0 | case CHAR_LF: |
119 | 0 | case CHAR_VT: |
120 | 0 | case CHAR_FF: |
121 | 0 | *lenptr = 1; |
122 | 0 | return TRUE; |
123 | | |
124 | 0 | case CHAR_CR: |
125 | 0 | *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF) ? 2 : 1; |
126 | 0 | return TRUE; |
127 | | |
128 | 0 | #ifndef EBCDIC |
129 | 0 | #if PCRE2_CODE_UNIT_WIDTH == 8 |
130 | 0 | case CHAR_NEL: |
131 | 0 | *lenptr = utf ? 2 : 1; |
132 | 0 | return TRUE; |
133 | | |
134 | 0 | case 0x2028: // LS |
135 | 0 | case 0x2029: // PS |
136 | 0 | *lenptr = 3; |
137 | 0 | return TRUE; |
138 | | |
139 | | #else /* 16-bit or 32-bit code units */ |
140 | | case CHAR_NEL: |
141 | | case 0x2028: // LS |
142 | | case 0x2029: // PS |
143 | | *lenptr = 1; |
144 | | return TRUE; |
145 | | #endif |
146 | 0 | #endif /* Not EBCDIC */ |
147 | | |
148 | 0 | default: |
149 | 0 | return FALSE; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | |
155 | | |
156 | | /************************************************* |
157 | | * Check for newline at previous position * |
158 | | *************************************************/ |
159 | | |
160 | | /* This function is called only via the WAS_NEWLINE macro, which does so only |
161 | | when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed |
162 | | newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the initial |
163 | | value of ptr is greater than the start of the string that is being processed. |
164 | | |
165 | | Arguments: |
166 | | ptr pointer to possible newline |
167 | | type the newline type |
168 | | startptr pointer to the start of the string |
169 | | lenptr where to return the length |
170 | | utf TRUE if in utf mode |
171 | | |
172 | | Returns: TRUE or FALSE |
173 | | */ |
174 | | |
175 | | BOOL |
176 | | PRIV(was_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR startptr, uint32_t *lenptr, BOOL utf) |
177 | 0 | { |
178 | 0 | uint32_t c; |
179 | 0 | ptr--; |
180 | |
|
181 | 0 | #ifdef SUPPORT_UNICODE |
182 | 0 | if (utf) |
183 | 0 | { |
184 | 0 | BACKCHAR(ptr); |
185 | 0 | GETCHAR(c, ptr); |
186 | 0 | } |
187 | 0 | else |
188 | 0 | { |
189 | 0 | c = *ptr; |
190 | 0 | } |
191 | | #else |
192 | | (void)utf; |
193 | | c = *ptr; |
194 | | #endif /* SUPPORT_UNICODE */ |
195 | |
|
196 | 0 | if (type == NLTYPE_ANYCRLF) |
197 | 0 | { |
198 | 0 | switch (c) |
199 | 0 | { |
200 | 0 | case CHAR_LF: |
201 | 0 | *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR) ? 2 : 1; |
202 | 0 | return TRUE; |
203 | | |
204 | 0 | case CHAR_CR: |
205 | 0 | *lenptr = 1; |
206 | 0 | return TRUE; |
207 | | |
208 | 0 | default: |
209 | 0 | return FALSE; |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | /* NLTYPE_ANY */ |
214 | | |
215 | 0 | else |
216 | 0 | { |
217 | 0 | switch (c) |
218 | 0 | { |
219 | 0 | case CHAR_LF: |
220 | 0 | *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR) ? 2 : 1; |
221 | 0 | return TRUE; |
222 | | |
223 | | #ifdef EBCDIC |
224 | | case CHAR_NEL: |
225 | | #endif |
226 | 0 | case CHAR_VT: |
227 | 0 | case CHAR_FF: |
228 | 0 | case CHAR_CR: |
229 | 0 | *lenptr = 1; |
230 | 0 | return TRUE; |
231 | | |
232 | 0 | #ifndef EBCDIC |
233 | 0 | #if PCRE2_CODE_UNIT_WIDTH == 8 |
234 | 0 | case CHAR_NEL: |
235 | 0 | *lenptr = utf ? 2 : 1; |
236 | 0 | return TRUE; |
237 | | |
238 | 0 | case 0x2028: // LS |
239 | 0 | case 0x2029: // PS |
240 | 0 | *lenptr = 3; |
241 | 0 | return TRUE; |
242 | | |
243 | | #else /* 16-bit or 32-bit code units */ |
244 | | case CHAR_NEL: |
245 | | case 0x2028: // LS |
246 | | case 0x2029: // PS |
247 | | *lenptr = 1; |
248 | | return TRUE; |
249 | | #endif |
250 | 0 | #endif /* Not EBCDIC */ |
251 | | |
252 | 0 | default: |
253 | 0 | return FALSE; |
254 | 0 | } |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | | /* End of pcre2_newline.c */ |