Add GRegex for regular expression matching. (#50075)
[platform/upstream/glib.git] / glib / pcre / pcre_newline.c
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            Copyright (c) 1997-2006 University of Cambridge
10
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15     * Redistributions of source code must retain the above copyright notice,
16       this list of conditions and the following disclaimer.
17
18     * Redistributions in binary form must reproduce the above copyright
19       notice, this list of conditions and the following disclaimer in the
20       documentation and/or other materials provided with the distribution.
21
22     * Neither the name of the University of Cambridge nor the names of its
23       contributors may be used to endorse or promote products derived from
24       this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
38 */
39
40
41 /* This module contains internal functions for testing newlines when more than
42 one kind of newline is to be recognized. When a newline is found, its length is
43 returned. In principle, we could implement several newline "types", each
44 referring to a different set of newline characters. At present, PCRE supports
45 only NLTYPE_FIXED, which gets handled without these functions, and NLTYPE_ALL,
46 so for now the type isn't passed into the functions. It can easily be added
47 later if required. The full list of Unicode newline characters is taken from
48 http://unicode.org/unicode/reports/tr18/. */
49
50
51 #include "pcre_internal.h"
52
53
54
55 /*************************************************
56 *      Check for newline at given position       *
57 *************************************************/
58
59 /* It is guaranteed that the initial value of ptr is less than the end of the
60 string that is being processed.
61
62 Arguments:
63   ptr          pointer to possible newline
64   endptr       pointer to the end of the string
65   lenptr       where to return the length
66   utf8         TRUE if in utf8 mode
67
68 Returns:       TRUE or FALSE
69 */
70
71 BOOL
72 _pcre_is_newline(const uschar *ptr, const uschar *endptr, int *lenptr,
73   BOOL utf8)
74 {
75 int c;
76 if (utf8) { GETCHAR(c, ptr); } else c = *ptr;
77 switch(c)
78   {
79   case 0x000a:                                       /* LF */
80   case 0x000b:                                       /* VT */
81   case 0x000c: *lenptr = 1; return TRUE;             /* FF */
82   case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
83                return TRUE;                          /* CR */
84   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;   /* NEL */
85   case 0x2028:                                       /* LS */
86   case 0x2029: *lenptr = 3; return TRUE;             /* PS */
87   default: return FALSE;
88   }
89 }
90
91
92
93 /*************************************************
94 *     Check for newline at previous position     *
95 *************************************************/
96
97 /* It is guaranteed that the initial value of ptr is greater than the start of
98 the string that is being processed.
99
100 Arguments:
101   ptr          pointer to possible newline
102   startptr     pointer to the start of the string
103   lenptr       where to return the length
104   utf8         TRUE if in utf8 mode
105
106 Returns:       TRUE or FALSE
107 */
108
109 BOOL
110 _pcre_was_newline(const uschar *ptr, const uschar *startptr, int *lenptr,
111   BOOL utf8)
112 {
113 int c;
114 ptr--;
115 if (utf8)
116   {
117   BACKCHAR(ptr);
118   GETCHAR(c, ptr);
119   }
120 else c = *ptr;
121 switch(c)
122   {
123   case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
124                return TRUE;                         /* LF */
125   case 0x000b:                                      /* VT */
126   case 0x000c:                                      /* FF */
127   case 0x000d: *lenptr = 1; return TRUE;            /* CR */
128   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;  /* NEL */
129   case 0x2028:                                      /* LS */
130   case 0x2029: *lenptr = 3; return TRUE;            /* PS */
131   default: return FALSE;
132   }
133 }
134
135 /* End of pcre_newline.c */