Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / yarr / pcre / pcre_ucp_searchfuncs.cpp
1 /* This is JavaScriptCore's variant of the PCRE library. While this library
2 started out as a copy of PCRE, many of the features of PCRE have been
3 removed. This library now supports only the regular expression features
4 required by the JavaScript language specification, and has only the functions
5 needed by JavaScriptCore and the rest of WebKit.
6
7                  Originally written by Philip Hazel
8            Copyright (c) 1997-2006 University of Cambridge
9     Copyright (C) 2002, 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
10
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15     * Redistributions of source code must retain the above copyright notice,
16       this list of conditions and the following disclaimer.
17
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.
21
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.
25
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 -----------------------------------------------------------------------------
38 */
39
40
41 /* This module contains code for searching the table of Unicode character
42 properties. */
43
44 #include "pcre_internal.h"
45
46 #include "ucpinternal.h"       /* Internal table details */
47 #include "ucptable.cpp"        /* The table itself */
48
49 /*************************************************
50 *       Search table and return other case       *
51 *************************************************/
52
53 /* If the given character is a letter, and there is another case for the
54 letter, return the other case. Otherwise, return -1.
55
56 Arguments:
57   c           the character value
58
59 Returns:      the other case or -1 if none
60 */
61
62 int jsc_pcre_ucp_othercase(unsigned c)
63 {
64     int bot = 0;
65     int top = sizeof(ucp_table) / sizeof(cnode);
66     int mid;
67     
68     /* The table is searched using a binary chop. You might think that using
69      intermediate variables to hold some of the common expressions would speed
70      things up, but tests with gcc 3.4.4 on Linux showed that, on the contrary, it
71      makes things a lot slower. */
72     
73     for (;;) {
74         if (top <= bot)
75             return -1;
76         mid = (bot + top) >> 1;
77         if (c == (ucp_table[mid].f0 & f0_charmask))
78             break;
79         if (c < (ucp_table[mid].f0 & f0_charmask))
80             top = mid;
81         else {
82             if ((ucp_table[mid].f0 & f0_rangeflag) && (c <= (ucp_table[mid].f0 & f0_charmask) + (ucp_table[mid].f1 & f1_rangemask)))
83                 break;
84             bot = mid + 1;
85         }
86     }
87     
88     /* Found an entry in the table. Return -1 for a range entry. Otherwise return
89      the other case if there is one, else -1. */
90     
91     if (ucp_table[mid].f0 & f0_rangeflag)
92         return -1;
93     
94     int offset = ucp_table[mid].f1 & f1_casemask;
95     if (offset & f1_caseneg)
96         offset |= f1_caseneg;
97     return !offset ? -1 : c + offset;
98 }