Add GRegex for regular expression matching. (#50075)
[platform/upstream/glib.git] / glib / pcre / pcre_ucp_searchfuncs.c
1 /*************************************************
2 *      Perl-Compatible Regular Expressions       *
3 *************************************************/
4
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.
7
8                        Written by Philip Hazel
9            Copyright (c) 1997-2006 University of Cambridge
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 /* This file has been modified to use glib instead of the internal table
41  * in ucptable.c -- Marco Barisione */
42
43 /* This module contains code for searching the table of Unicode character
44 properties. */
45
46 #include "pcre_internal.h"
47
48 #include "ucp.h"               /* Category definitions */
49 #include "ucpinternal.h"       /* Internal table details */
50
51
52 /* Table to translate from particular type value to the general value. */
53
54 static int ucp_gentype[] = {
55   ucp_C, ucp_C, ucp_C, ucp_C, ucp_C,  /* Cc, Cf, Cn, Co, Cs */
56   ucp_L, ucp_L, ucp_L, ucp_L, ucp_L,  /* Ll, Lu, Lm, Lo, Lt */
57   ucp_M, ucp_M, ucp_M,                /* Mc, Me, Mn */
58   ucp_N, ucp_N, ucp_N,                /* Nd, Nl, No */
59   ucp_P, ucp_P, ucp_P, ucp_P, ucp_P,  /* Pc, Pd, Pe, Pf, Pi */
60   ucp_P, ucp_P,                       /* Ps, Po */
61   ucp_S, ucp_S, ucp_S, ucp_S,         /* Sc, Sk, Sm, So */
62   ucp_Z, ucp_Z, ucp_Z                 /* Zl, Zp, Zs */
63 };
64
65
66
67 /*************************************************
68 *         Search table and return type           *
69 *************************************************/
70
71 /* Three values are returned: the category is ucp_C, ucp_L, etc. The detailed
72 character type is ucp_Lu, ucp_Nd, etc. The script is ucp_Latin, etc.
73
74 Arguments:
75   c           the character value
76   type_ptr    the detailed character type is returned here
77   script_ptr  the script is returned here
78
79 Returns:      the character type category
80 */
81
82 int
83 _pcre_ucp_findprop(const unsigned int c, int *type_ptr, int *script_ptr)
84 {
85 /* Note that the Unicode types have the same values in glib and in
86  * PCRE, so ucp_Ll == G_UNICODE_LOWERCASE_LETTER,
87  * ucp_Zs == G_UNICODE_SPACE_SEPARATOR, and so on. */
88 *type_ptr = g_unichar_type(c);
89 *script_ptr = g_unichar_get_script(c);
90 return ucp_gentype[*type_ptr];
91 }
92
93
94
95
96 /*************************************************
97 *       Search table and return other case       *
98 *************************************************/
99
100 /* If the given character is a letter, and there is another case for the
101 letter, return the other case. Otherwise, return -1.
102
103 Arguments:
104   c           the character value
105
106 Returns:      the other case or NOTACHAR if none
107 */
108
109 unsigned int
110 _pcre_ucp_othercase(const unsigned int c)
111 {
112 int other_case = NOTACHAR;
113
114 if (g_unichar_islower(c))
115   other_case = g_unichar_toupper(c);
116 else if (g_unichar_isupper(c))
117   other_case = g_unichar_tolower(c);
118
119 if (other_case == c)
120   other_case = NOTACHAR;
121
122 return other_case;
123 }
124
125
126 /* End of pcre_ucp_searchfuncs.c */