0.2.0-dev Download ZIP
239 lines ·6.2 KB Raw
| 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, |
| 76 | uint32_t *lenptr, BOOL utf) |
| 77 | { |
| 78 | uint32_t c; |
| 79 | |
| 80 | #ifdef SUPPORT_UNICODE |
| 81 | if (utf) { GETCHAR(c, ptr); } else c = *ptr; |
| 82 | #else |
| 83 | (void)utf; |
| 84 | c = *ptr; |
| 85 | #endif /* SUPPORT_UNICODE */ |
| 86 | |
| 87 | if (type == NLTYPE_ANYCRLF) switch(c) |
| 88 | { |
| 89 | case CHAR_LF: |
| 90 | *lenptr = 1; |
| 91 | return TRUE; |
| 92 | |
| 93 | case CHAR_CR: |
| 94 | *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1; |
| 95 | return TRUE; |
| 96 | |
| 97 | default: |
| 98 | return FALSE; |
| 99 | } |
| 100 | |
| 101 | /* NLTYPE_ANY */ |
| 102 | |
| 103 | else switch(c) |
| 104 | { |
| 105 | #ifdef EBCDIC |
| 106 | case CHAR_NEL: |
| 107 | #endif |
| 108 | case CHAR_LF: |
| 109 | case CHAR_VT: |
| 110 | case CHAR_FF: |
| 111 | *lenptr = 1; |
| 112 | return TRUE; |
| 113 | |
| 114 | case CHAR_CR: |
| 115 | *lenptr = (ptr < endptr - 1 && ptr[1] == CHAR_LF)? 2 : 1; |
| 116 | return TRUE; |
| 117 | |
| 118 | #ifndef EBCDIC |
| 119 | #if PCRE2_CODE_UNIT_WIDTH == 8 |
| 120 | case CHAR_NEL: |
| 121 | *lenptr = utf? 2 : 1; |
| 122 | return TRUE; |
| 123 | |
| 124 | case 0x2028: /* LS */ |
| 125 | case 0x2029: /* PS */ |
| 126 | *lenptr = 3; |
| 127 | return TRUE; |
| 128 | |
| 129 | #else /* 16-bit or 32-bit code units */ |
| 130 | case CHAR_NEL: |
| 131 | case 0x2028: /* LS */ |
| 132 | case 0x2029: /* PS */ |
| 133 | *lenptr = 1; |
| 134 | return TRUE; |
| 135 | #endif |
| 136 | #endif /* Not EBCDIC */ |
| 137 | |
| 138 | default: |
| 139 | return FALSE; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | /************************************************* |
| 146 | * Check for newline at previous position * |
| 147 | *************************************************/ |
| 148 | |
| 149 | /* This function is called only via the WAS_NEWLINE macro, which does so only |
| 150 | when the newline type is NLTYPE_ANY or NLTYPE_ANYCRLF. The case of a fixed |
| 151 | newline (NLTYPE_FIXED) is handled inline. It is guaranteed that the initial |
| 152 | value of ptr is greater than the start of the string that is being processed. |
| 153 | |
| 154 | Arguments: |
| 155 | ptr pointer to possible newline |
| 156 | type the newline type |
| 157 | startptr pointer to the start of the string |
| 158 | lenptr where to return the length |
| 159 | utf TRUE if in utf mode |
| 160 | |
| 161 | Returns: TRUE or FALSE |
| 162 | */ |
| 163 | |
| 164 | BOOL |
| 165 | PRIV(was_newline)(PCRE2_SPTR ptr, uint32_t type, PCRE2_SPTR startptr, |
| 166 | uint32_t *lenptr, BOOL utf) |
| 167 | { |
| 168 | uint32_t c; |
| 169 | ptr--; |
| 170 | |
| 171 | #ifdef SUPPORT_UNICODE |
| 172 | if (utf) |
| 173 | { |
| 174 | BACKCHAR(ptr); |
| 175 | GETCHAR(c, ptr); |
| 176 | } |
| 177 | else c = *ptr; |
| 178 | #else |
| 179 | (void)utf; |
| 180 | c = *ptr; |
| 181 | #endif /* SUPPORT_UNICODE */ |
| 182 | |
| 183 | if (type == NLTYPE_ANYCRLF) switch(c) |
| 184 | { |
| 185 | case CHAR_LF: |
| 186 | *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1; |
| 187 | return TRUE; |
| 188 | |
| 189 | case CHAR_CR: |
| 190 | *lenptr = 1; |
| 191 | return TRUE; |
| 192 | |
| 193 | default: |
| 194 | return FALSE; |
| 195 | } |
| 196 | |
| 197 | /* NLTYPE_ANY */ |
| 198 | |
| 199 | else switch(c) |
| 200 | { |
| 201 | case CHAR_LF: |
| 202 | *lenptr = (ptr > startptr && ptr[-1] == CHAR_CR)? 2 : 1; |
| 203 | return TRUE; |
| 204 | |
| 205 | #ifdef EBCDIC |
| 206 | case CHAR_NEL: |
| 207 | #endif |
| 208 | case CHAR_VT: |
| 209 | case CHAR_FF: |
| 210 | case CHAR_CR: |
| 211 | *lenptr = 1; |
| 212 | return TRUE; |
| 213 | |
| 214 | #ifndef EBCDIC |
| 215 | #if PCRE2_CODE_UNIT_WIDTH == 8 |
| 216 | case CHAR_NEL: |
| 217 | *lenptr = utf? 2 : 1; |
| 218 | return TRUE; |
| 219 | |
| 220 | case 0x2028: /* LS */ |
| 221 | case 0x2029: /* PS */ |
| 222 | *lenptr = 3; |
| 223 | return TRUE; |
| 224 | |
| 225 | #else /* 16-bit or 32-bit code units */ |
| 226 | case CHAR_NEL: |
| 227 | case 0x2028: /* LS */ |
| 228 | case 0x2029: /* PS */ |
| 229 | *lenptr = 1; |
| 230 | return TRUE; |
| 231 | #endif |
| 232 | #endif /* Not EBCDIC */ |
| 233 | |
| 234 | default: |
| 235 | return FALSE; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* End of pcre2_newline.c */ |