2 * gunicode.c: Some Unicode routines
5 * Miguel de Icaza (miguel@novell.com)
7 * (C) 2006 Novell, Inc.
9 * utf8 validation code came from:
10 * libxml2-2.6.26 licensed under the MIT X11 license
12 * Authors credit in libxml's string.c:
13 * William Brack <wbrack@mmm.com.hk>
16 * Permission is hereby granted, free of charge, to any person obtaining
17 * a copy of this software and associated documentation files (the
18 * "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so, subject to
22 * the following conditions:
24 * The above copyright notice and this permission notice shall be
25 * included in all copies or substantial portions of the Software.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 #include <unicode-data.h>
43 # ifdef HAVE_LOCALCHARSET_H
44 # include <localcharset.h>
48 const char *my_charset;
51 * Character set conversion
55 g_unichar_type (gunichar c)
59 guint16 cp = (guint16) c;
60 for (i = 0; i < unicode_category_ranges_count; i++) {
61 if (cp < unicode_category_ranges [i].start)
63 if (unicode_category_ranges [i].end <= cp)
65 return unicode_category [i] [cp - unicode_category_ranges [i].start];
69 // 3400-4DB5: OtherLetter
70 // 4E00-9FC3: OtherLetter
71 // AC00-D7A3: OtherLetter
72 // D800-DFFF: OtherSurrogate
73 // E000-F8FF: OtherPrivateUse
74 // 20000-2A6D6 OtherLetter
75 // F0000-FFFFD OtherPrivateUse
76 // 100000-10FFFD OtherPrivateUse
78 if (0x3400 <= cp && cp < 0x4DB5)
79 return G_UNICODE_OTHER_LETTER;
80 if (0x4E00 <= cp && cp < 0x9FC3)
81 return G_UNICODE_OTHER_LETTER;
82 if (0xAC00<= cp && cp < 0xD7A3)
83 return G_UNICODE_OTHER_LETTER;
84 if (0xD800 <= cp && cp < 0xDFFF)
85 return G_UNICODE_SURROGATE;
86 if (0xE000 <= cp && cp < 0xF8FF)
87 return G_UNICODE_PRIVATE_USE;
88 /* since the argument is UTF-16, we cannot check beyond FFFF */
90 /* It should match any of above */
95 g_unichar_break_type (gunichar c)
98 return G_UNICODE_BREAK_UNKNOWN;
102 g_unichar_case (gunichar c, gboolean upper)
105 guint32 cp = (guint32) c, v;
107 for (i = 0; i < simple_case_map_ranges_count; i++) {
108 if (cp < simple_case_map_ranges [i].start)
110 if (simple_case_map_ranges [i].end <= cp)
113 const guint16 *tab = upper ? simple_upper_case_mapping_lowarea [i] : simple_lower_case_mapping_lowarea [i];
114 v = tab [cp - simple_case_map_ranges [i].start];
117 i2 = (gint8)(i - (upper ? simple_upper_case_mapping_lowarea_table_count : simple_lower_case_mapping_lowarea_table_count));
118 tab = upper ? simple_upper_case_mapping_higharea [i2] : simple_lower_case_mapping_higharea [i2];
119 v = tab [cp - simple_case_map_ranges [i].start];
121 return v != 0 ? (gunichar) v : c;
127 g_unichar_toupper (gunichar c)
129 return g_unichar_case (c, TRUE);
133 g_unichar_tolower (gunichar c)
135 return g_unichar_case (c, FALSE);
139 g_unichar_totitle (gunichar c)
145 for (i = 0; i < simple_titlecase_mapping_count; i++) {
146 if (simple_titlecase_mapping [i].codepoint == cp)
147 return simple_titlecase_mapping [i].title;
148 if (simple_titlecase_mapping [i].codepoint > cp)
149 /* it is ordered, hence no more match */
152 return g_unichar_toupper (c);
156 g_unichar_isxdigit (gunichar c)
158 return (g_unichar_xdigit_value (c) != -1);
163 g_unichar_xdigit_value (gunichar c)
165 if (c >= 0x30 && c <= 0x39) /*0-9*/
167 if (c >= 0x41 && c <= 0x46) /*A-F*/
169 if (c >= 0x61 && c <= 0x66) /*a-f*/
175 g_unichar_isspace (gunichar c)
177 GUnicodeType type = g_unichar_type (c);
178 if (type == G_UNICODE_LINE_SEPARATOR ||
179 type == G_UNICODE_PARAGRAPH_SEPARATOR ||
180 type == G_UNICODE_SPACE_SEPARATOR)
188 * This is broken, and assumes an UTF8 system, but will do for eglib's first user
191 g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
196 len = strlen (utf8string);
198 res = g_malloc (len + 1);
199 g_strlcpy (res, utf8string, len + 1);
204 static gboolean is_utf8;
207 g_get_charset (G_CONST_RETURN char **charset)
209 if (my_charset == NULL) {
210 /* These shouldn't be heap allocated */
211 #if defined(HAVE_LOCALCHARSET_H)
212 my_charset = locale_charset ();
214 my_charset = "UTF-8";
216 is_utf8 = strcmp (my_charset, "UTF-8") == 0;
220 *charset = my_charset;
224 #endif /* G_OS_WIN32 */
227 g_locale_to_utf8 (const gchar *opsysstring, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
229 g_get_charset (NULL);
231 return g_convert (opsysstring, len, "UTF-8", my_charset, bytes_read, bytes_written, error);
235 g_locale_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read, gsize *bytes_written, GError **error)
237 g_get_charset (NULL);
239 return g_convert (utf8string, len, my_charset, "UTF-8", bytes_read, bytes_written, error);