Back to index

php5  5.3.10
pcre_newline.c
Go to the documentation of this file.
00001 /*************************************************
00002 *      Perl-Compatible Regular Expressions       *
00003 *************************************************/
00004 
00005 /* PCRE is a library of functions to support regular expressions whose syntax
00006 and semantics are as close as possible to those of the Perl 5 language.
00007 
00008                        Written by Philip Hazel
00009            Copyright (c) 1997-2009 University of Cambridge
00010 
00011 -----------------------------------------------------------------------------
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014 
00015     * Redistributions of source code must retain the above copyright notice,
00016       this list of conditions and the following disclaimer.
00017 
00018     * Redistributions in binary form must reproduce the above copyright
00019       notice, this list of conditions and the following disclaimer in the
00020       documentation and/or other materials provided with the distribution.
00021 
00022     * Neither the name of the University of Cambridge nor the names of its
00023       contributors may be used to endorse or promote products derived from
00024       this software without specific prior written permission.
00025 
00026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036 POSSIBILITY OF SUCH DAMAGE.
00037 -----------------------------------------------------------------------------
00038 */
00039 
00040 
00041 /* This module contains internal functions for testing newlines when more than
00042 one kind of newline is to be recognized. When a newline is found, its length is
00043 returned. In principle, we could implement several newline "types", each
00044 referring to a different set of newline characters. At present, PCRE supports
00045 only NLTYPE_FIXED, which gets handled without these functions, NLTYPE_ANYCRLF,
00046 and NLTYPE_ANY. The full list of Unicode newline characters is taken from
00047 http://unicode.org/unicode/reports/tr18/. */
00048 
00049 
00050 #include "config.h"
00051 
00052 #include "pcre_internal.h"
00053 
00054 
00055 
00056 /*************************************************
00057 *      Check for newline at given position       *
00058 *************************************************/
00059 
00060 /* It is guaranteed that the initial value of ptr is less than the end of the
00061 string that is being processed.
00062 
00063 Arguments:
00064   ptr          pointer to possible newline
00065   type         the newline type
00066   endptr       pointer to the end of the string
00067   lenptr       where to return the length
00068   utf8         TRUE if in utf8 mode
00069 
00070 Returns:       TRUE or FALSE
00071 */
00072 
00073 BOOL
00074 _pcre_is_newline(USPTR ptr, int type, USPTR endptr, int *lenptr, BOOL utf8)
00075 {
00076 int c;
00077 if (utf8) { GETCHAR(c, ptr); } else c = *ptr;
00078 
00079 if (type == NLTYPE_ANYCRLF) switch(c)
00080   {
00081   case 0x000a: *lenptr = 1; return TRUE;             /* LF */
00082   case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
00083                return TRUE;                          /* CR */
00084   default: return FALSE;
00085   }
00086 
00087 /* NLTYPE_ANY */
00088 
00089 else switch(c)
00090   {
00091   case 0x000a:                                       /* LF */
00092   case 0x000b:                                       /* VT */
00093   case 0x000c: *lenptr = 1; return TRUE;             /* FF */
00094   case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
00095                return TRUE;                          /* CR */
00096   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;   /* NEL */
00097   case 0x2028:                                       /* LS */
00098   case 0x2029: *lenptr = 3; return TRUE;             /* PS */
00099   default: return FALSE;
00100   }
00101 }
00102 
00103 
00104 
00105 /*************************************************
00106 *     Check for newline at previous position     *
00107 *************************************************/
00108 
00109 /* It is guaranteed that the initial value of ptr is greater than the start of
00110 the string that is being processed.
00111 
00112 Arguments:
00113   ptr          pointer to possible newline
00114   type         the newline type
00115   startptr     pointer to the start of the string
00116   lenptr       where to return the length
00117   utf8         TRUE if in utf8 mode
00118 
00119 Returns:       TRUE or FALSE
00120 */
00121 
00122 BOOL
00123 _pcre_was_newline(USPTR ptr, int type, USPTR startptr, int *lenptr, BOOL utf8)
00124 {
00125 int c;
00126 ptr--;
00127 #ifdef SUPPORT_UTF8
00128 if (utf8)
00129   {
00130   BACKCHAR(ptr);
00131   GETCHAR(c, ptr);
00132   }
00133 else c = *ptr;
00134 #else   /* no UTF-8 support */
00135 c = *ptr;
00136 #endif  /* SUPPORT_UTF8 */
00137 
00138 if (type == NLTYPE_ANYCRLF) switch(c)
00139   {
00140   case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
00141                return TRUE;                         /* LF */
00142   case 0x000d: *lenptr = 1; return TRUE;            /* CR */
00143   default: return FALSE;
00144   }
00145 
00146 else switch(c)
00147   {
00148   case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
00149                return TRUE;                         /* LF */
00150   case 0x000b:                                      /* VT */
00151   case 0x000c:                                      /* FF */
00152   case 0x000d: *lenptr = 1; return TRUE;            /* CR */
00153   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;  /* NEL */
00154   case 0x2028:                                      /* LS */
00155   case 0x2029: *lenptr = 3; return TRUE;            /* PS */
00156   default: return FALSE;
00157   }
00158 }
00159 
00160 /* End of pcre_newline.c */