1 /* guniprop.c - Unicode character properties.
2  *
3  * Copyright (C) 1999 Tom Tromey
4  * Copyright (C) 2000 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 #include <glib.h>
23 #include "gunicode.h"
24 #include "gscripttable.h"
25 
26 #define G_SCRIPT_TABLE_MIDPOINT (G_N_ELEMENTS (g_script_table) / 2)
27 
28 static inline GUnicodeScript
g_unichar_get_script_bsearch(gunichar ch)29 g_unichar_get_script_bsearch (gunichar ch)
30 {
31   int lower = 0;
32   int upper = G_N_ELEMENTS (g_script_table) - 1;
33   static int saved_mid = G_SCRIPT_TABLE_MIDPOINT;
34   int mid = saved_mid;
35 
36 
37   do
38     {
39       if (ch < g_script_table[mid].start)
40 	upper = mid - 1;
41       else if (ch >= g_script_table[mid].start + g_script_table[mid].chars)
42 	lower = mid + 1;
43       else
44 	return g_script_table[saved_mid = mid].script;
45 
46       mid = (lower + upper) / 2;
47     }
48   while (lower <= upper);
49 
50   return G_UNICODE_SCRIPT_UNKNOWN;
51 }
52 
53 /**
54  * g_unichar_get_script:
55  * @ch: a Unicode character
56  *
57  * Looks up the #GUnicodeScript for a particular character (as defined
58  * by Unicode Standard Annex #24). No check is made for @ch being a
59  * valid Unicode character; if you pass in invalid character, the
60  * result is undefined.
61  *
62  * This function is equivalent to pango_script_for_unichar() and the
63  * two are interchangeable.
64  *
65  * Return value: the #GUnicodeScript for the character.
66  *
67  * Since: 2.14
68  */
69 GUnicodeScript
g_unichar_get_script(gunichar ch)70 g_unichar_get_script (gunichar ch)
71 {
72   if (ch < G_EASY_SCRIPTS_RANGE)
73     return g_script_easy_table[ch];
74   else
75     return g_unichar_get_script_bsearch (ch);
76 }
77