1 /* gunicode.h - Unicode manipulation functions
3 * Copyright (C) 1999, 2000 Tom Tromey
4 * Copyright 2000, 2005 Red Hat, Inc.
6 * The Gnome Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * The Gnome 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
26 #ifndef __G_UNICODE_H__
27 #define __G_UNICODE_H__
29 #include <glib/gerror.h>
30 #include <glib/gtypes.h>
34 typedef guint32 gunichar;
35 typedef guint16 gunichar2;
37 /* These are the possible character classifications.
38 * See http://www.unicode.org/Public/UNIDATA/UCD.html#General_Category_Values
45 G_UNICODE_PRIVATE_USE,
47 G_UNICODE_LOWERCASE_LETTER,
48 G_UNICODE_MODIFIER_LETTER,
49 G_UNICODE_OTHER_LETTER,
50 G_UNICODE_TITLECASE_LETTER,
51 G_UNICODE_UPPERCASE_LETTER,
52 G_UNICODE_COMBINING_MARK,
53 G_UNICODE_ENCLOSING_MARK,
54 G_UNICODE_NON_SPACING_MARK,
55 G_UNICODE_DECIMAL_NUMBER,
56 G_UNICODE_LETTER_NUMBER,
57 G_UNICODE_OTHER_NUMBER,
58 G_UNICODE_CONNECT_PUNCTUATION,
59 G_UNICODE_DASH_PUNCTUATION,
60 G_UNICODE_CLOSE_PUNCTUATION,
61 G_UNICODE_FINAL_PUNCTUATION,
62 G_UNICODE_INITIAL_PUNCTUATION,
63 G_UNICODE_OTHER_PUNCTUATION,
64 G_UNICODE_OPEN_PUNCTUATION,
65 G_UNICODE_CURRENCY_SYMBOL,
66 G_UNICODE_MODIFIER_SYMBOL,
67 G_UNICODE_MATH_SYMBOL,
68 G_UNICODE_OTHER_SYMBOL,
69 G_UNICODE_LINE_SEPARATOR,
70 G_UNICODE_PARAGRAPH_SEPARATOR,
71 G_UNICODE_SPACE_SEPARATOR
74 /* These are the possible line break classifications.
75 * Note that new types may be added in the future.
76 * Implementations may regard unknown values like G_UNICODE_BREAK_UNKNOWN
77 * See http://www.unicode.org/unicode/reports/tr14/
81 G_UNICODE_BREAK_MANDATORY,
82 G_UNICODE_BREAK_CARRIAGE_RETURN,
83 G_UNICODE_BREAK_LINE_FEED,
84 G_UNICODE_BREAK_COMBINING_MARK,
85 G_UNICODE_BREAK_SURROGATE,
86 G_UNICODE_BREAK_ZERO_WIDTH_SPACE,
87 G_UNICODE_BREAK_INSEPARABLE,
88 G_UNICODE_BREAK_NON_BREAKING_GLUE,
89 G_UNICODE_BREAK_CONTINGENT,
90 G_UNICODE_BREAK_SPACE,
91 G_UNICODE_BREAK_AFTER,
92 G_UNICODE_BREAK_BEFORE,
93 G_UNICODE_BREAK_BEFORE_AND_AFTER,
94 G_UNICODE_BREAK_HYPHEN,
95 G_UNICODE_BREAK_NON_STARTER,
96 G_UNICODE_BREAK_OPEN_PUNCTUATION,
97 G_UNICODE_BREAK_CLOSE_PUNCTUATION,
98 G_UNICODE_BREAK_QUOTATION,
99 G_UNICODE_BREAK_EXCLAMATION,
100 G_UNICODE_BREAK_IDEOGRAPHIC,
101 G_UNICODE_BREAK_NUMERIC,
102 G_UNICODE_BREAK_INFIX_SEPARATOR,
103 G_UNICODE_BREAK_SYMBOL,
104 G_UNICODE_BREAK_ALPHABETIC,
105 G_UNICODE_BREAK_PREFIX,
106 G_UNICODE_BREAK_POSTFIX,
107 G_UNICODE_BREAK_COMPLEX_CONTEXT,
108 G_UNICODE_BREAK_AMBIGUOUS,
109 G_UNICODE_BREAK_UNKNOWN,
110 G_UNICODE_BREAK_NEXT_LINE,
111 G_UNICODE_BREAK_WORD_JOINER,
112 G_UNICODE_BREAK_HANGUL_L_JAMO,
113 G_UNICODE_BREAK_HANGUL_V_JAMO,
114 G_UNICODE_BREAK_HANGUL_T_JAMO,
115 G_UNICODE_BREAK_HANGUL_LV_SYLLABLE,
116 G_UNICODE_BREAK_HANGUL_LVT_SYLLABLE,
117 G_UNICODE_BREAK_CLOSE_PARANTHESIS
121 { /* ISO 15924 code */
122 G_UNICODE_SCRIPT_INVALID_CODE = -1,
123 G_UNICODE_SCRIPT_COMMON = 0, /* Zyyy */
124 G_UNICODE_SCRIPT_INHERITED, /* Qaai */
125 G_UNICODE_SCRIPT_ARABIC, /* Arab */
126 G_UNICODE_SCRIPT_ARMENIAN, /* Armn */
127 G_UNICODE_SCRIPT_BENGALI, /* Beng */
128 G_UNICODE_SCRIPT_BOPOMOFO, /* Bopo */
129 G_UNICODE_SCRIPT_CHEROKEE, /* Cher */
130 G_UNICODE_SCRIPT_COPTIC, /* Qaac */
131 G_UNICODE_SCRIPT_CYRILLIC, /* Cyrl (Cyrs) */
132 G_UNICODE_SCRIPT_DESERET, /* Dsrt */
133 G_UNICODE_SCRIPT_DEVANAGARI, /* Deva */
134 G_UNICODE_SCRIPT_ETHIOPIC, /* Ethi */
135 G_UNICODE_SCRIPT_GEORGIAN, /* Geor (Geon, Geoa) */
136 G_UNICODE_SCRIPT_GOTHIC, /* Goth */
137 G_UNICODE_SCRIPT_GREEK, /* Grek */
138 G_UNICODE_SCRIPT_GUJARATI, /* Gujr */
139 G_UNICODE_SCRIPT_GURMUKHI, /* Guru */
140 G_UNICODE_SCRIPT_HAN, /* Hani */
141 G_UNICODE_SCRIPT_HANGUL, /* Hang */
142 G_UNICODE_SCRIPT_HEBREW, /* Hebr */
143 G_UNICODE_SCRIPT_HIRAGANA, /* Hira */
144 G_UNICODE_SCRIPT_KANNADA, /* Knda */
145 G_UNICODE_SCRIPT_KATAKANA, /* Kana */
146 G_UNICODE_SCRIPT_KHMER, /* Khmr */
147 G_UNICODE_SCRIPT_LAO, /* Laoo */
148 G_UNICODE_SCRIPT_LATIN, /* Latn (Latf, Latg) */
149 G_UNICODE_SCRIPT_MALAYALAM, /* Mlym */
150 G_UNICODE_SCRIPT_MONGOLIAN, /* Mong */
151 G_UNICODE_SCRIPT_MYANMAR, /* Mymr */
152 G_UNICODE_SCRIPT_OGHAM, /* Ogam */
153 G_UNICODE_SCRIPT_OLD_ITALIC, /* Ital */
154 G_UNICODE_SCRIPT_ORIYA, /* Orya */
155 G_UNICODE_SCRIPT_RUNIC, /* Runr */
156 G_UNICODE_SCRIPT_SINHALA, /* Sinh */
157 G_UNICODE_SCRIPT_SYRIAC, /* Syrc (Syrj, Syrn, Syre) */
158 G_UNICODE_SCRIPT_TAMIL, /* Taml */
159 G_UNICODE_SCRIPT_TELUGU, /* Telu */
160 G_UNICODE_SCRIPT_THAANA, /* Thaa */
161 G_UNICODE_SCRIPT_THAI, /* Thai */
162 G_UNICODE_SCRIPT_TIBETAN, /* Tibt */
163 G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL, /* Cans */
164 G_UNICODE_SCRIPT_YI, /* Yiii */
165 G_UNICODE_SCRIPT_TAGALOG, /* Tglg */
166 G_UNICODE_SCRIPT_HANUNOO, /* Hano */
167 G_UNICODE_SCRIPT_BUHID, /* Buhd */
168 G_UNICODE_SCRIPT_TAGBANWA, /* Tagb */
170 /* Unicode-4.0 additions */
171 G_UNICODE_SCRIPT_BRAILLE, /* Brai */
172 G_UNICODE_SCRIPT_CYPRIOT, /* Cprt */
173 G_UNICODE_SCRIPT_LIMBU, /* Limb */
174 G_UNICODE_SCRIPT_OSMANYA, /* Osma */
175 G_UNICODE_SCRIPT_SHAVIAN, /* Shaw */
176 G_UNICODE_SCRIPT_LINEAR_B, /* Linb */
177 G_UNICODE_SCRIPT_TAI_LE, /* Tale */
178 G_UNICODE_SCRIPT_UGARITIC, /* Ugar */
180 /* Unicode-4.1 additions */
181 G_UNICODE_SCRIPT_NEW_TAI_LUE, /* Talu */
182 G_UNICODE_SCRIPT_BUGINESE, /* Bugi */
183 G_UNICODE_SCRIPT_GLAGOLITIC, /* Glag */
184 G_UNICODE_SCRIPT_TIFINAGH, /* Tfng */
185 G_UNICODE_SCRIPT_SYLOTI_NAGRI, /* Sylo */
186 G_UNICODE_SCRIPT_OLD_PERSIAN, /* Xpeo */
187 G_UNICODE_SCRIPT_KHAROSHTHI, /* Khar */
189 /* Unicode-5.0 additions */
190 G_UNICODE_SCRIPT_UNKNOWN, /* Zzzz */
191 G_UNICODE_SCRIPT_BALINESE, /* Bali */
192 G_UNICODE_SCRIPT_CUNEIFORM, /* Xsux */
193 G_UNICODE_SCRIPT_PHOENICIAN, /* Phnx */
194 G_UNICODE_SCRIPT_PHAGS_PA, /* Phag */
195 G_UNICODE_SCRIPT_NKO, /* Nkoo */
197 /* Unicode-5.1 additions */
198 G_UNICODE_SCRIPT_KAYAH_LI, /* Kali */
199 G_UNICODE_SCRIPT_LEPCHA, /* Lepc */
200 G_UNICODE_SCRIPT_REJANG, /* Rjng */
201 G_UNICODE_SCRIPT_SUNDANESE, /* Sund */
202 G_UNICODE_SCRIPT_SAURASHTRA, /* Saur */
203 G_UNICODE_SCRIPT_CHAM, /* Cham */
204 G_UNICODE_SCRIPT_OL_CHIKI, /* Olck */
205 G_UNICODE_SCRIPT_VAI, /* Vaii */
206 G_UNICODE_SCRIPT_CARIAN, /* Cari */
207 G_UNICODE_SCRIPT_LYCIAN, /* Lyci */
208 G_UNICODE_SCRIPT_LYDIAN, /* Lydi */
210 /* Unicode-5.2 additions */
211 G_UNICODE_SCRIPT_AVESTAN, /* Avst */
212 G_UNICODE_SCRIPT_BAMUM, /* Bamu */
213 G_UNICODE_SCRIPT_EGYPTIAN_HIEROGLYPHS, /* Egyp */
214 G_UNICODE_SCRIPT_IMPERIAL_ARAMAIC, /* Armi */
215 G_UNICODE_SCRIPT_INSCRIPTIONAL_PAHLAVI, /* Phli */
216 G_UNICODE_SCRIPT_INSCRIPTIONAL_PARTHIAN, /* Prti */
217 G_UNICODE_SCRIPT_JAVANESE, /* Java */
218 G_UNICODE_SCRIPT_KAITHI, /* Kthi */
219 G_UNICODE_SCRIPT_LISU, /* Lisu */
220 G_UNICODE_SCRIPT_MEETEI_MAYEK, /* Mtei */
221 G_UNICODE_SCRIPT_OLD_SOUTH_ARABIAN, /* Sarb */
222 G_UNICODE_SCRIPT_OLD_TURKIC, /* Orkh */
223 G_UNICODE_SCRIPT_SAMARITAN, /* Samr */
224 G_UNICODE_SCRIPT_TAI_THAM, /* Lana */
225 G_UNICODE_SCRIPT_TAI_VIET, /* Tavt */
227 /* Unicode-6.0 additions */
228 G_UNICODE_SCRIPT_BATAK, /* Batk */
229 G_UNICODE_SCRIPT_BRAHMI, /* Brah */
230 G_UNICODE_SCRIPT_MANDAIC /* Mand */
233 /* Returns TRUE if current locale uses UTF-8 charset. If CHARSET is
234 * not null, sets *CHARSET to the name of the current locale's
235 * charset. This value is statically allocated, and should be copied
236 * in case the locale's charset will be changed later using setlocale()
237 * or in some other way.
239 gboolean g_get_charset (const char **charset);
241 /* These are all analogs of the <ctype.h> functions.
243 gboolean g_unichar_isalnum (gunichar c) G_GNUC_CONST;
244 gboolean g_unichar_isalpha (gunichar c) G_GNUC_CONST;
245 gboolean g_unichar_iscntrl (gunichar c) G_GNUC_CONST;
246 gboolean g_unichar_isdigit (gunichar c) G_GNUC_CONST;
247 gboolean g_unichar_isgraph (gunichar c) G_GNUC_CONST;
248 gboolean g_unichar_islower (gunichar c) G_GNUC_CONST;
249 gboolean g_unichar_isprint (gunichar c) G_GNUC_CONST;
250 gboolean g_unichar_ispunct (gunichar c) G_GNUC_CONST;
251 gboolean g_unichar_isspace (gunichar c) G_GNUC_CONST;
252 gboolean g_unichar_isupper (gunichar c) G_GNUC_CONST;
253 gboolean g_unichar_isxdigit (gunichar c) G_GNUC_CONST;
254 gboolean g_unichar_istitle (gunichar c) G_GNUC_CONST;
255 gboolean g_unichar_isdefined (gunichar c) G_GNUC_CONST;
256 gboolean g_unichar_iswide (gunichar c) G_GNUC_CONST;
257 gboolean g_unichar_iswide_cjk(gunichar c) G_GNUC_CONST;
258 gboolean g_unichar_iszerowidth(gunichar c) G_GNUC_CONST;
259 gboolean g_unichar_ismark (gunichar c) G_GNUC_CONST;
261 /* More <ctype.h> functions. These convert between the three cases.
262 * See the Unicode book to understand title case. */
263 gunichar g_unichar_toupper (gunichar c) G_GNUC_CONST;
264 gunichar g_unichar_tolower (gunichar c) G_GNUC_CONST;
265 gunichar g_unichar_totitle (gunichar c) G_GNUC_CONST;
267 /* If C is a digit (according to `g_unichar_isdigit'), then return its
268 numeric value. Otherwise return -1. */
269 gint g_unichar_digit_value (gunichar c) G_GNUC_CONST;
271 gint g_unichar_xdigit_value (gunichar c) G_GNUC_CONST;
273 /* Return the Unicode character type of a given character. */
274 GUnicodeType g_unichar_type (gunichar c) G_GNUC_CONST;
276 /* Return the line break property for a given character */
277 GUnicodeBreakType g_unichar_break_type (gunichar c) G_GNUC_CONST;
279 /* Returns the combining class for a given character */
280 gint g_unichar_combining_class (gunichar uc) G_GNUC_CONST;
283 /* Pairwise canonical compose/decompose */
284 gboolean g_unichar_compose (gunichar a,
287 gboolean g_unichar_decompose (gunichar ch,
291 /* Compute canonical ordering of a string in-place. This rearranges
292 decomposed characters in the string according to their combining
293 classes. See the Unicode manual for more information. */
294 void g_unicode_canonical_ordering (gunichar *string,
298 /* Compute canonical decomposition of a character. Returns g_malloc()d
299 string of Unicode characters. RESULT_LEN is set to the resulting
300 length of the string. */
301 gunichar *g_unicode_canonical_decomposition (gunichar ch,
302 gsize *result_len) G_GNUC_MALLOC;
304 /* Array of skip-bytes-per-initial character.
306 GLIB_VAR const gchar * const g_utf8_skip;
308 #define g_utf8_next_char(p) (char *)((p) + g_utf8_skip[*(const guchar *)(p)])
310 gunichar g_utf8_get_char (const gchar *p) G_GNUC_PURE;
311 gunichar g_utf8_get_char_validated (const gchar *p,
312 gssize max_len) G_GNUC_PURE;
314 gchar* g_utf8_offset_to_pointer (const gchar *str,
315 glong offset) G_GNUC_PURE;
316 glong g_utf8_pointer_to_offset (const gchar *str,
317 const gchar *pos) G_GNUC_PURE;
318 gchar* g_utf8_prev_char (const gchar *p) G_GNUC_PURE;
319 gchar* g_utf8_find_next_char (const gchar *p,
320 const gchar *end) G_GNUC_PURE;
321 gchar* g_utf8_find_prev_char (const gchar *str,
322 const gchar *p) G_GNUC_PURE;
324 glong g_utf8_strlen (const gchar *p,
325 gssize max) G_GNUC_PURE;
327 gchar *g_utf8_substring (const gchar *p,
329 glong end_pos) G_GNUC_MALLOC;
331 /* Copies n characters from src to dest */
332 gchar* g_utf8_strncpy (gchar *dest,
336 /* Find the UTF-8 character corresponding to ch, in string p. These
337 functions are equivalants to strchr and strrchr */
338 gchar* g_utf8_strchr (const gchar *p,
341 gchar* g_utf8_strrchr (const gchar *p,
344 gchar* g_utf8_strreverse (const gchar *str,
347 gunichar2 *g_utf8_to_utf16 (const gchar *str,
350 glong *items_written,
351 GError **error) G_GNUC_MALLOC;
352 gunichar * g_utf8_to_ucs4 (const gchar *str,
355 glong *items_written,
356 GError **error) G_GNUC_MALLOC;
357 gunichar * g_utf8_to_ucs4_fast (const gchar *str,
359 glong *items_written) G_GNUC_MALLOC;
360 gunichar * g_utf16_to_ucs4 (const gunichar2 *str,
363 glong *items_written,
364 GError **error) G_GNUC_MALLOC;
365 gchar* g_utf16_to_utf8 (const gunichar2 *str,
368 glong *items_written,
369 GError **error) G_GNUC_MALLOC;
370 gunichar2 *g_ucs4_to_utf16 (const gunichar *str,
373 glong *items_written,
374 GError **error) G_GNUC_MALLOC;
375 gchar* g_ucs4_to_utf8 (const gunichar *str,
378 glong *items_written,
379 GError **error) G_GNUC_MALLOC;
381 /* Convert a single character into UTF-8. outbuf must have at
382 * least 6 bytes of space. Returns the number of bytes in the
385 gint g_unichar_to_utf8 (gunichar c,
388 /* Validate a UTF8 string, return TRUE if valid, put pointer to
389 * first invalid char in **end
392 gboolean g_utf8_validate (const gchar *str,
396 /* Validate a Unicode character */
397 gboolean g_unichar_validate (gunichar ch) G_GNUC_CONST;
399 gchar *g_utf8_strup (const gchar *str,
400 gssize len) G_GNUC_MALLOC;
401 gchar *g_utf8_strdown (const gchar *str,
402 gssize len) G_GNUC_MALLOC;
403 gchar *g_utf8_casefold (const gchar *str,
404 gssize len) G_GNUC_MALLOC;
408 G_NORMALIZE_NFD = G_NORMALIZE_DEFAULT,
409 G_NORMALIZE_DEFAULT_COMPOSE,
410 G_NORMALIZE_NFC = G_NORMALIZE_DEFAULT_COMPOSE,
412 G_NORMALIZE_NFKD = G_NORMALIZE_ALL,
413 G_NORMALIZE_ALL_COMPOSE,
414 G_NORMALIZE_NFKC = G_NORMALIZE_ALL_COMPOSE
417 gchar *g_utf8_normalize (const gchar *str,
419 GNormalizeMode mode) G_GNUC_MALLOC;
421 gint g_utf8_collate (const gchar *str1,
422 const gchar *str2) G_GNUC_PURE;
423 gchar *g_utf8_collate_key (const gchar *str,
424 gssize len) G_GNUC_MALLOC;
425 gchar *g_utf8_collate_key_for_filename (const gchar *str,
426 gssize len) G_GNUC_MALLOC;
428 gboolean g_unichar_get_mirror_char (gunichar ch,
429 gunichar *mirrored_ch);
431 GUnicodeScript g_unichar_get_script (gunichar ch) G_GNUC_CONST;
436 gchar *_g_utf8_make_valid (const gchar *name);
440 #endif /* __G_UNICODE_H__ */