1
/*************************************************
2
* Perl-Compatible Regular Expressions *
3
*************************************************/
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.
8
Written by Philip Hazel
9
Copyright (c) 1997-2007 University of Cambridge
11
-----------------------------------------------------------------------------
12
Redistribution and use in source and binary forms, with or without
13
modification, are permitted provided that the following conditions are met:
15
* Redistributions of source code must retain the above copyright notice,
16
this list of conditions and the following disclaimer.
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.
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.
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
-----------------------------------------------------------------------------
41
/* This module contains code for searching the table of Unicode character
48
#include "pcre_internal.h"
50
#include "ucp.h" /* Category definitions */
51
#include "ucpinternal.h" /* Internal table details */
52
#include "ucptable.h" /* The table itself */
55
/* Table to translate from particular type value to the general value. */
57
static const int ucp_gentype[] = {
58
ucp_C, ucp_C, ucp_C, ucp_C, ucp_C, /* Cc, Cf, Cn, Co, Cs */
59
ucp_L, ucp_L, ucp_L, ucp_L, ucp_L, /* Ll, Lu, Lm, Lo, Lt */
60
ucp_M, ucp_M, ucp_M, /* Mc, Me, Mn */
61
ucp_N, ucp_N, ucp_N, /* Nd, Nl, No */
62
ucp_P, ucp_P, ucp_P, ucp_P, ucp_P, /* Pc, Pd, Pe, Pf, Pi */
63
ucp_P, ucp_P, /* Ps, Po */
64
ucp_S, ucp_S, ucp_S, ucp_S, /* Sc, Sk, Sm, So */
65
ucp_Z, ucp_Z, ucp_Z /* Zl, Zp, Zs */
70
/*************************************************
71
* Search table and return type *
72
*************************************************/
74
/* Three values are returned: the category is ucp_C, ucp_L, etc. The detailed
75
character type is ucp_Lu, ucp_Nd, etc. The script is ucp_Latin, etc.
79
type_ptr the detailed character type is returned here
80
script_ptr the script is returned here
82
Returns: the character type category
86
_pcre_ucp_findprop(const unsigned int c, int *type_ptr, int *script_ptr)
89
int top = sizeof(ucp_table)/sizeof(cnode);
92
/* The table is searched using a binary chop. You might think that using
93
intermediate variables to hold some of the common expressions would speed
94
things up, but tests with gcc 3.4.4 on Linux showed that, on the contrary, it
95
makes things a lot slower. */
102
*script_ptr = ucp_Common;
105
mid = (bot + top) >> 1;
106
if (c == (ucp_table[mid].f0 & f0_charmask)) break;
107
if (c < (ucp_table[mid].f0 & f0_charmask)) top = mid;
110
if ((ucp_table[mid].f0 & f0_rangeflag) != 0 &&
111
c <= (ucp_table[mid].f0 & f0_charmask) +
112
(ucp_table[mid].f1 & f1_rangemask)) break;
117
/* Found an entry in the table. Set the script and detailed type values, and
118
return the general type. */
120
*script_ptr = (ucp_table[mid].f0 & f0_scriptmask) >> f0_scriptshift;
121
*type_ptr = (ucp_table[mid].f1 & f1_typemask) >> f1_typeshift;
123
return ucp_gentype[*type_ptr];
128
/*************************************************
129
* Search table and return other case *
130
*************************************************/
132
/* If the given character is a letter, and there is another case for the
133
letter, return the other case. Otherwise, return -1.
136
c the character value
138
Returns: the other case or NOTACHAR if none
142
_pcre_ucp_othercase(const unsigned int c)
145
int top = sizeof(ucp_table)/sizeof(cnode);
148
/* The table is searched using a binary chop. You might think that using
149
intermediate variables to hold some of the common expressions would speed
150
things up, but tests with gcc 3.4.4 on Linux showed that, on the contrary, it
151
makes things a lot slower. */
155
if (top <= bot) return -1;
156
mid = (bot + top) >> 1;
157
if (c == (ucp_table[mid].f0 & f0_charmask)) break;
158
if (c < (ucp_table[mid].f0 & f0_charmask)) top = mid;
161
if ((ucp_table[mid].f0 & f0_rangeflag) != 0 &&
162
c <= (ucp_table[mid].f0 & f0_charmask) +
163
(ucp_table[mid].f1 & f1_rangemask)) break;
168
/* Found an entry in the table. Return NOTACHAR for a range entry. Otherwise
169
return the other case if there is one, else NOTACHAR. */
171
if ((ucp_table[mid].f0 & f0_rangeflag) != 0) return NOTACHAR;
173
offset = ucp_table[mid].f1 & f1_casemask;
174
if ((offset & f1_caseneg) != 0) offset |= f1_caseneg;
175
return (offset == 0)? NOTACHAR : c + offset;
179
/* End of pcre_ucp_searchfuncs.c */