1 /* $Cambridge: exim/src/src/pcre/pcre_newline.c,v 1.2 2007/06/26 11:16:54 ph10 Exp $ */
3 /*************************************************
4 * Perl-Compatible Regular Expressions *
5 *************************************************/
7 /* PCRE is a library of functions to support regular expressions whose syntax
8 and semantics are as close as possible to those of the Perl 5 language.
10 Written by Philip Hazel
11 Copyright (c) 1997-2007 University of Cambridge
13 -----------------------------------------------------------------------------
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
17 * Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
20 * Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
24 * Neither the name of the University of Cambridge nor the names of its
25 contributors may be used to endorse or promote products derived from
26 this software without specific prior written permission.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 POSSIBILITY OF SUCH DAMAGE.
39 -----------------------------------------------------------------------------
43 /* This module contains internal functions for testing newlines when more than
44 one kind of newline is to be recognized. When a newline is found, its length is
45 returned. In principle, we could implement several newline "types", each
46 referring to a different set of newline characters. At present, PCRE supports
47 only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
48 and NLTYPE_ANY. The full list of Unicode newline characters is taken from
49 http://unicode.org/unicode/reports/tr18/. */
52 #include "pcre_internal.h"
56 /*************************************************
57 * Check for newline at given position *
58 *************************************************/
60 /* It is guaranteed that the initial value of ptr is less than the end of the
61 string that is being processed.
64 ptr pointer to possible newline
66 endptr pointer to the end of the string
67 lenptr where to return the length
68 utf8 TRUE if in utf8 mode
70 Returns: TRUE or FALSE
74 _pcre_is_newline(const uschar *ptr, int type, const uschar *endptr,
75 int *lenptr, BOOL utf8)
78 if (utf8) { GETCHAR(c, ptr); } else c = *ptr;
80 if (type == NLTYPE_ANYCRLF) switch(c)
82 case 0x000a: *lenptr = 1; return TRUE; /* LF */
83 case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
85 default: return FALSE;
94 case 0x000c: *lenptr = 1; return TRUE; /* FF */
95 case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
97 case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */
99 case 0x2029: *lenptr = 3; return TRUE; /* PS */
100 default: return FALSE;
106 /*************************************************
107 * Check for newline at previous position *
108 *************************************************/
110 /* It is guaranteed that the initial value of ptr is greater than the start of
111 the string that is being processed.
114 ptr pointer to possible newline
115 type the newline type
116 startptr pointer to the start of the string
117 lenptr where to return the length
118 utf8 TRUE if in utf8 mode
120 Returns: TRUE or FALSE
124 _pcre_was_newline(const uschar *ptr, int type, const uschar *startptr,
125 int *lenptr, BOOL utf8)
136 if (type == NLTYPE_ANYCRLF) switch(c)
138 case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
139 return TRUE; /* LF */
140 case 0x000d: *lenptr = 1; return TRUE; /* CR */
141 default: return FALSE;
146 case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
147 return TRUE; /* LF */
148 case 0x000b: /* VT */
149 case 0x000c: /* FF */
150 case 0x000d: *lenptr = 1; return TRUE; /* CR */
151 case 0x0085: *lenptr = utf8? 2 : 1; return TRUE; /* NEL */
152 case 0x2028: /* LS */
153 case 0x2029: *lenptr = 3; return TRUE; /* PS */
154 default: return FALSE;
158 /* End of pcre_newline.c */