• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/pcre/pcre_newline.c

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 #ifdef HAVE_CONFIG_H
00051 #include "config.h"
00052 #else if defined(_WINDOWS)
00053 #include <spl/configwin32.h>
00054 #endif
00055 
00056 
00057 #include "pcre_internal.h"
00058 
00059 
00060 
00061 /*************************************************
00062 *      Check for newline at given position       *
00063 *************************************************/
00064 
00065 /* It is guaranteed that the initial value of ptr is less than the end of the
00066 string that is being processed.
00067 
00068 Arguments:
00069   ptr          pointer to possible newline
00070   type         the newline type
00071   endptr       pointer to the end of the string
00072   lenptr       where to return the length
00073   utf8         TRUE if in utf8 mode
00074 
00075 Returns:       TRUE or FALSE
00076 */
00077 
00078 BOOL
00079 _pcre_is_newline(USPTR ptr, int type, USPTR endptr, int *lenptr, BOOL utf8)
00080 {
00081 int c;
00082 if (utf8) { GETCHAR(c, ptr); } else c = *ptr;
00083 
00084 if (type == NLTYPE_ANYCRLF) switch(c)
00085   {
00086   case 0x000a: *lenptr = 1; return TRUE;             /* LF */
00087   case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
00088                return TRUE;                          /* CR */
00089   default: return FALSE;
00090   }
00091 
00092 /* NLTYPE_ANY */
00093 
00094 else switch(c)
00095   {
00096   case 0x000a:                                       /* LF */
00097   case 0x000b:                                       /* VT */
00098   case 0x000c: *lenptr = 1; return TRUE;             /* FF */
00099   case 0x000d: *lenptr = (ptr < endptr - 1 && ptr[1] == 0x0a)? 2 : 1;
00100                return TRUE;                          /* CR */
00101   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;   /* NEL */
00102   case 0x2028:                                       /* LS */
00103   case 0x2029: *lenptr = 3; return TRUE;             /* PS */
00104   default: return FALSE;
00105   }
00106 }
00107 
00108 
00109 
00110 /*************************************************
00111 *     Check for newline at previous position     *
00112 *************************************************/
00113 
00114 /* It is guaranteed that the initial value of ptr is greater than the start of
00115 the string that is being processed.
00116 
00117 Arguments:
00118   ptr          pointer to possible newline
00119   type         the newline type
00120   startptr     pointer to the start of the string
00121   lenptr       where to return the length
00122   utf8         TRUE if in utf8 mode
00123 
00124 Returns:       TRUE or FALSE
00125 */
00126 
00127 BOOL
00128 _pcre_was_newline(USPTR ptr, int type, USPTR startptr, int *lenptr, BOOL utf8)
00129 {
00130 int c;
00131 ptr--;
00132 #ifdef SUPPORT_UTF8
00133 if (utf8)
00134   {
00135   BACKCHAR(ptr);
00136   GETCHAR(c, ptr);
00137   }
00138 else c = *ptr;
00139 #else   /* no UTF-8 support */
00140 c = *ptr;
00141 #endif  /* SUPPORT_UTF8 */
00142 
00143 if (type == NLTYPE_ANYCRLF) switch(c)
00144   {
00145   case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
00146                return TRUE;                         /* LF */
00147   case 0x000d: *lenptr = 1; return TRUE;            /* CR */
00148   default: return FALSE;
00149   }
00150 
00151 else switch(c)
00152   {
00153   case 0x000a: *lenptr = (ptr > startptr && ptr[-1] == 0x0d)? 2 : 1;
00154                return TRUE;                         /* LF */
00155   case 0x000b:                                      /* VT */
00156   case 0x000c:                                      /* FF */
00157   case 0x000d: *lenptr = 1; return TRUE;            /* CR */
00158   case 0x0085: *lenptr = utf8? 2 : 1; return TRUE;  /* NEL */
00159   case 0x2028:                                      /* LS */
00160   case 0x2029: *lenptr = 3; return TRUE;            /* PS */
00161   default: return FALSE;
00162   }
00163 }
00164 
00165 /* End of pcre_newline.c */