Back to index

php5  5.3.10
pcre_xclass.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-2010 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 an internal function that is used to match an extended
00042 class. It is used by both pcre_exec() and pcre_def_exec(). */
00043 
00044 
00045 #include "config.h"
00046 
00047 #include "pcre_internal.h"
00048 
00049 
00050 /*************************************************
00051 *       Match character against an XCLASS        *
00052 *************************************************/
00053 
00054 /* This function is called to match a character against an extended class that
00055 might contain values > 255 and/or Unicode properties.
00056 
00057 Arguments:
00058   c           the character
00059   data        points to the flag byte of the XCLASS data
00060 
00061 Returns:      TRUE if character matches, else FALSE
00062 */
00063 
00064 BOOL
00065 _pcre_xclass(int c, const uschar *data)
00066 {
00067 int t;
00068 BOOL negated = (*data & XCL_NOT) != 0;
00069 
00070 /* Character values < 256 are matched against a bitmap, if one is present. If
00071 not, we still carry on, because there may be ranges that start below 256 in the
00072 additional data. */
00073 
00074 if (c < 256)
00075   {
00076   if ((*data & XCL_MAP) != 0 && (data[1 + c/8] & (1 << (c&7))) != 0)
00077     return !negated;   /* char found */
00078   }
00079 
00080 /* First skip the bit map if present. Then match against the list of Unicode
00081 properties or large chars or ranges that end with a large char. We won't ever
00082 encounter XCL_PROP or XCL_NOTPROP when UCP support is not compiled. */
00083 
00084 if ((*data++ & XCL_MAP) != 0) data += 32;
00085 
00086 while ((t = *data++) != XCL_END)
00087   {
00088   int x, y;
00089   if (t == XCL_SINGLE)
00090     {
00091     GETCHARINC(x, data);
00092     if (c == x) return !negated;
00093     }
00094   else if (t == XCL_RANGE)
00095     {
00096     GETCHARINC(x, data);
00097     GETCHARINC(y, data);
00098     if (c >= x && c <= y) return !negated;
00099     }
00100 
00101 #ifdef SUPPORT_UCP
00102   else  /* XCL_PROP & XCL_NOTPROP */
00103     {
00104     const ucd_record *prop = GET_UCD(c);
00105 
00106     switch(*data)
00107       {
00108       case PT_ANY:
00109       if (t == XCL_PROP) return !negated;
00110       break;
00111 
00112       case PT_LAMP:
00113       if ((prop->chartype == ucp_Lu || prop->chartype == ucp_Ll ||
00114            prop->chartype == ucp_Lt) == (t == XCL_PROP)) return !negated;
00115       break;
00116 
00117       case PT_GC:
00118       if ((data[1] == _pcre_ucp_gentype[prop->chartype]) == (t == XCL_PROP))
00119         return !negated;
00120       break;
00121 
00122       case PT_PC:
00123       if ((data[1] == prop->chartype) == (t == XCL_PROP)) return !negated;
00124       break;
00125 
00126       case PT_SC:
00127       if ((data[1] == prop->script) == (t == XCL_PROP)) return !negated;
00128       break;
00129 
00130       case PT_ALNUM:
00131       if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
00132            _pcre_ucp_gentype[prop->chartype] == ucp_N) == (t == XCL_PROP))
00133         return !negated;
00134       break;
00135 
00136       case PT_SPACE:    /* Perl space */
00137       if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
00138            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
00139              == (t == XCL_PROP))
00140         return !negated;
00141       break;
00142 
00143       case PT_PXSPACE:  /* POSIX space */
00144       if ((_pcre_ucp_gentype[prop->chartype] == ucp_Z ||
00145            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
00146            c == CHAR_FF || c == CHAR_CR) == (t == XCL_PROP))
00147         return !negated;
00148       break;
00149 
00150       case PT_WORD:
00151       if ((_pcre_ucp_gentype[prop->chartype] == ucp_L ||
00152            _pcre_ucp_gentype[prop->chartype] == ucp_N || c == CHAR_UNDERSCORE)
00153              == (t == XCL_PROP))
00154         return !negated;
00155       break;
00156 
00157       /* This should never occur, but compilers may mutter if there is no
00158       default. */
00159 
00160       default:
00161       return FALSE;
00162       }
00163 
00164     data += 2;
00165     }
00166 #endif  /* SUPPORT_UCP */
00167   }
00168 
00169 return negated;   /* char did not match */
00170 }
00171 
00172 /* End of pcre_xclass.c */