Navit SVN

/work/compile/navit/src/navit/support/glib/gunicode.h

00001 /* gunicode.h - Unicode manipulation functions
00002  *
00003  *  Copyright (C) 1999, 2000 Tom Tromey
00004  *  Copyright 2000, 2005 Red Hat, Inc.
00005  *
00006  * The Gnome Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * The Gnome Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with the Gnome Library; see the file COPYING.LIB.  If not,
00018  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  *   Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
00023 #error "Only <glib.h> can be included directly."
00024 #endif
00025 
00026 #ifndef __G_UNICODE_H__
00027 #define __G_UNICODE_H__
00028 
00029 #include <glib/gerror.h>
00030 #include <glib/gtypes.h>
00031 
00032 G_BEGIN_DECLS
00033 
00034 typedef guint32 gunichar;
00035 typedef guint16 gunichar2;
00036 
00037 /* These are the possible character classifications.
00038  * See http://www.unicode.org/Public/UNIDATA/UCD.html#General_Category_Values
00039  */
00040 typedef enum
00041 {
00042   G_UNICODE_CONTROL,
00043   G_UNICODE_FORMAT,
00044   G_UNICODE_UNASSIGNED,
00045   G_UNICODE_PRIVATE_USE,
00046   G_UNICODE_SURROGATE,
00047   G_UNICODE_LOWERCASE_LETTER,
00048   G_UNICODE_MODIFIER_LETTER,
00049   G_UNICODE_OTHER_LETTER,
00050   G_UNICODE_TITLECASE_LETTER,
00051   G_UNICODE_UPPERCASE_LETTER,
00052   G_UNICODE_COMBINING_MARK,
00053   G_UNICODE_ENCLOSING_MARK,
00054   G_UNICODE_NON_SPACING_MARK,
00055   G_UNICODE_DECIMAL_NUMBER,
00056   G_UNICODE_LETTER_NUMBER,
00057   G_UNICODE_OTHER_NUMBER,
00058   G_UNICODE_CONNECT_PUNCTUATION,
00059   G_UNICODE_DASH_PUNCTUATION,
00060   G_UNICODE_CLOSE_PUNCTUATION,
00061   G_UNICODE_FINAL_PUNCTUATION,
00062   G_UNICODE_INITIAL_PUNCTUATION,
00063   G_UNICODE_OTHER_PUNCTUATION,
00064   G_UNICODE_OPEN_PUNCTUATION,
00065   G_UNICODE_CURRENCY_SYMBOL,
00066   G_UNICODE_MODIFIER_SYMBOL,
00067   G_UNICODE_MATH_SYMBOL,
00068   G_UNICODE_OTHER_SYMBOL,
00069   G_UNICODE_LINE_SEPARATOR,
00070   G_UNICODE_PARAGRAPH_SEPARATOR,
00071   G_UNICODE_SPACE_SEPARATOR
00072 } GUnicodeType;
00073 
00074 /* These are the possible line break classifications.
00075  * Note that new types may be added in the future.
00076  * Implementations may regard unknown values like G_UNICODE_BREAK_UNKNOWN
00077  * See http://www.unicode.org/unicode/reports/tr14/
00078  */
00079 typedef enum
00080 {
00081   G_UNICODE_BREAK_MANDATORY,
00082   G_UNICODE_BREAK_CARRIAGE_RETURN,
00083   G_UNICODE_BREAK_LINE_FEED,
00084   G_UNICODE_BREAK_COMBINING_MARK,
00085   G_UNICODE_BREAK_SURROGATE,
00086   G_UNICODE_BREAK_ZERO_WIDTH_SPACE,
00087   G_UNICODE_BREAK_INSEPARABLE,
00088   G_UNICODE_BREAK_NON_BREAKING_GLUE,
00089   G_UNICODE_BREAK_CONTINGENT,
00090   G_UNICODE_BREAK_SPACE,
00091   G_UNICODE_BREAK_AFTER,
00092   G_UNICODE_BREAK_BEFORE,
00093   G_UNICODE_BREAK_BEFORE_AND_AFTER,
00094   G_UNICODE_BREAK_HYPHEN,
00095   G_UNICODE_BREAK_NON_STARTER,
00096   G_UNICODE_BREAK_OPEN_PUNCTUATION,
00097   G_UNICODE_BREAK_CLOSE_PUNCTUATION,
00098   G_UNICODE_BREAK_QUOTATION,
00099   G_UNICODE_BREAK_EXCLAMATION,
00100   G_UNICODE_BREAK_IDEOGRAPHIC,
00101   G_UNICODE_BREAK_NUMERIC,
00102   G_UNICODE_BREAK_INFIX_SEPARATOR,
00103   G_UNICODE_BREAK_SYMBOL,
00104   G_UNICODE_BREAK_ALPHABETIC,
00105   G_UNICODE_BREAK_PREFIX,
00106   G_UNICODE_BREAK_POSTFIX,
00107   G_UNICODE_BREAK_COMPLEX_CONTEXT,
00108   G_UNICODE_BREAK_AMBIGUOUS,
00109   G_UNICODE_BREAK_UNKNOWN,
00110   G_UNICODE_BREAK_NEXT_LINE,
00111   G_UNICODE_BREAK_WORD_JOINER,
00112   G_UNICODE_BREAK_HANGUL_L_JAMO,
00113   G_UNICODE_BREAK_HANGUL_V_JAMO,
00114   G_UNICODE_BREAK_HANGUL_T_JAMO,
00115   G_UNICODE_BREAK_HANGUL_LV_SYLLABLE,
00116   G_UNICODE_BREAK_HANGUL_LVT_SYLLABLE
00117 } GUnicodeBreakType;
00118 
00119 typedef enum 
00120 {                         /* ISO 15924 code */
00121   G_UNICODE_SCRIPT_INVALID_CODE = -1,
00122   G_UNICODE_SCRIPT_COMMON       = 0,   /* Zyyy */
00123   G_UNICODE_SCRIPT_INHERITED,          /* Qaai */
00124   G_UNICODE_SCRIPT_ARABIC,             /* Arab */
00125   G_UNICODE_SCRIPT_ARMENIAN,           /* Armn */
00126   G_UNICODE_SCRIPT_BENGALI,            /* Beng */
00127   G_UNICODE_SCRIPT_BOPOMOFO,           /* Bopo */
00128   G_UNICODE_SCRIPT_CHEROKEE,           /* Cher */
00129   G_UNICODE_SCRIPT_COPTIC,             /* Qaac */
00130   G_UNICODE_SCRIPT_CYRILLIC,           /* Cyrl (Cyrs) */
00131   G_UNICODE_SCRIPT_DESERET,            /* Dsrt */
00132   G_UNICODE_SCRIPT_DEVANAGARI,         /* Deva */
00133   G_UNICODE_SCRIPT_ETHIOPIC,           /* Ethi */
00134   G_UNICODE_SCRIPT_GEORGIAN,           /* Geor (Geon, Geoa) */
00135   G_UNICODE_SCRIPT_GOTHIC,             /* Goth */
00136   G_UNICODE_SCRIPT_GREEK,              /* Grek */
00137   G_UNICODE_SCRIPT_GUJARATI,           /* Gujr */
00138   G_UNICODE_SCRIPT_GURMUKHI,           /* Guru */
00139   G_UNICODE_SCRIPT_HAN,                /* Hani */
00140   G_UNICODE_SCRIPT_HANGUL,             /* Hang */
00141   G_UNICODE_SCRIPT_HEBREW,             /* Hebr */
00142   G_UNICODE_SCRIPT_HIRAGANA,           /* Hira */
00143   G_UNICODE_SCRIPT_KANNADA,            /* Knda */
00144   G_UNICODE_SCRIPT_KATAKANA,           /* Kana */
00145   G_UNICODE_SCRIPT_KHMER,              /* Khmr */
00146   G_UNICODE_SCRIPT_LAO,                /* Laoo */
00147   G_UNICODE_SCRIPT_LATIN,              /* Latn (Latf, Latg) */
00148   G_UNICODE_SCRIPT_MALAYALAM,          /* Mlym */
00149   G_UNICODE_SCRIPT_MONGOLIAN,          /* Mong */
00150   G_UNICODE_SCRIPT_MYANMAR,            /* Mymr */
00151   G_UNICODE_SCRIPT_OGHAM,              /* Ogam */
00152   G_UNICODE_SCRIPT_OLD_ITALIC,         /* Ital */
00153   G_UNICODE_SCRIPT_ORIYA,              /* Orya */
00154   G_UNICODE_SCRIPT_RUNIC,              /* Runr */
00155   G_UNICODE_SCRIPT_SINHALA,            /* Sinh */
00156   G_UNICODE_SCRIPT_SYRIAC,             /* Syrc (Syrj, Syrn, Syre) */
00157   G_UNICODE_SCRIPT_TAMIL,              /* Taml */
00158   G_UNICODE_SCRIPT_TELUGU,             /* Telu */
00159   G_UNICODE_SCRIPT_THAANA,             /* Thaa */
00160   G_UNICODE_SCRIPT_THAI,               /* Thai */
00161   G_UNICODE_SCRIPT_TIBETAN,            /* Tibt */
00162   G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL, /* Cans */
00163   G_UNICODE_SCRIPT_YI,                 /* Yiii */
00164   G_UNICODE_SCRIPT_TAGALOG,            /* Tglg */
00165   G_UNICODE_SCRIPT_HANUNOO,            /* Hano */
00166   G_UNICODE_SCRIPT_BUHID,              /* Buhd */
00167   G_UNICODE_SCRIPT_TAGBANWA,           /* Tagb */
00168 
00169   /* Unicode-4.0 additions */
00170   G_UNICODE_SCRIPT_BRAILLE,            /* Brai */
00171   G_UNICODE_SCRIPT_CYPRIOT,            /* Cprt */
00172   G_UNICODE_SCRIPT_LIMBU,              /* Limb */
00173   G_UNICODE_SCRIPT_OSMANYA,            /* Osma */
00174   G_UNICODE_SCRIPT_SHAVIAN,            /* Shaw */
00175   G_UNICODE_SCRIPT_LINEAR_B,           /* Linb */
00176   G_UNICODE_SCRIPT_TAI_LE,             /* Tale */
00177   G_UNICODE_SCRIPT_UGARITIC,           /* Ugar */
00178       
00179   /* Unicode-4.1 additions */
00180   G_UNICODE_SCRIPT_NEW_TAI_LUE,        /* Talu */
00181   G_UNICODE_SCRIPT_BUGINESE,           /* Bugi */
00182   G_UNICODE_SCRIPT_GLAGOLITIC,         /* Glag */
00183   G_UNICODE_SCRIPT_TIFINAGH,           /* Tfng */
00184   G_UNICODE_SCRIPT_SYLOTI_NAGRI,       /* Sylo */
00185   G_UNICODE_SCRIPT_OLD_PERSIAN,        /* Xpeo */
00186   G_UNICODE_SCRIPT_KHAROSHTHI,         /* Khar */
00187 
00188   /* Unicode-5.0 additions */
00189   G_UNICODE_SCRIPT_UNKNOWN,            /* Zzzz */
00190   G_UNICODE_SCRIPT_BALINESE,           /* Bali */
00191   G_UNICODE_SCRIPT_CUNEIFORM,          /* Xsux */
00192   G_UNICODE_SCRIPT_PHOENICIAN,         /* Phnx */
00193   G_UNICODE_SCRIPT_PHAGS_PA,           /* Phag */
00194   G_UNICODE_SCRIPT_NKO,                /* Nkoo */
00195 
00196   /* Unicode-5.1 additions */
00197   G_UNICODE_SCRIPT_KAYAH_LI,           /* Kali */
00198   G_UNICODE_SCRIPT_LEPCHA,             /* Lepc */
00199   G_UNICODE_SCRIPT_REJANG,             /* Rjng */
00200   G_UNICODE_SCRIPT_SUNDANESE,          /* Sund */
00201   G_UNICODE_SCRIPT_SAURASHTRA,         /* Saur */
00202   G_UNICODE_SCRIPT_CHAM,               /* Cham */
00203   G_UNICODE_SCRIPT_OL_CHIKI,           /* Olck */
00204   G_UNICODE_SCRIPT_VAI,                /* Vaii */
00205   G_UNICODE_SCRIPT_CARIAN,             /* Cari */
00206   G_UNICODE_SCRIPT_LYCIAN,             /* Lyci */
00207   G_UNICODE_SCRIPT_LYDIAN              /* Lydi */
00208 } GUnicodeScript;
00209 
00210 /* Returns TRUE if current locale uses UTF-8 charset.  If CHARSET is
00211  * not null, sets *CHARSET to the name of the current locale's
00212  * charset.  This value is statically allocated, and should be copied
00213  * in case the locale's charset will be changed later using setlocale()
00214  * or in some other way.
00215  */
00216 gboolean g_get_charset (G_CONST_RETURN char **charset);
00217 
00218 /* These are all analogs of the <ctype.h> functions.
00219  */
00220 gboolean g_unichar_isalnum   (gunichar c) G_GNUC_CONST;
00221 gboolean g_unichar_isalpha   (gunichar c) G_GNUC_CONST;
00222 gboolean g_unichar_iscntrl   (gunichar c) G_GNUC_CONST;
00223 gboolean g_unichar_isdigit   (gunichar c) G_GNUC_CONST;
00224 gboolean g_unichar_isgraph   (gunichar c) G_GNUC_CONST;
00225 gboolean g_unichar_islower   (gunichar c) G_GNUC_CONST;
00226 gboolean g_unichar_isprint   (gunichar c) G_GNUC_CONST;
00227 gboolean g_unichar_ispunct   (gunichar c) G_GNUC_CONST;
00228 gboolean g_unichar_isspace   (gunichar c) G_GNUC_CONST;
00229 gboolean g_unichar_isupper   (gunichar c) G_GNUC_CONST;
00230 gboolean g_unichar_isxdigit  (gunichar c) G_GNUC_CONST;
00231 gboolean g_unichar_istitle   (gunichar c) G_GNUC_CONST;
00232 gboolean g_unichar_isdefined (gunichar c) G_GNUC_CONST;
00233 gboolean g_unichar_iswide    (gunichar c) G_GNUC_CONST;
00234 gboolean g_unichar_iswide_cjk(gunichar c) G_GNUC_CONST;
00235 gboolean g_unichar_iszerowidth(gunichar c) G_GNUC_CONST;
00236 gboolean g_unichar_ismark    (gunichar c) G_GNUC_CONST;
00237 
00238 /* More <ctype.h> functions.  These convert between the three cases.
00239  * See the Unicode book to understand title case.  */
00240 gunichar g_unichar_toupper (gunichar c) G_GNUC_CONST;
00241 gunichar g_unichar_tolower (gunichar c) G_GNUC_CONST;
00242 gunichar g_unichar_totitle (gunichar c) G_GNUC_CONST;
00243 
00244 /* If C is a digit (according to `g_unichar_isdigit'), then return its
00245    numeric value.  Otherwise return -1.  */
00246 gint g_unichar_digit_value (gunichar c) G_GNUC_CONST;
00247 
00248 gint g_unichar_xdigit_value (gunichar c) G_GNUC_CONST;
00249 
00250 /* Return the Unicode character type of a given character.  */
00251 GUnicodeType g_unichar_type (gunichar c) G_GNUC_CONST;
00252 
00253 /* Return the line break property for a given character */
00254 GUnicodeBreakType g_unichar_break_type (gunichar c) G_GNUC_CONST;
00255 
00256 /* Returns the combining class for a given character */
00257 gint g_unichar_combining_class (gunichar uc) G_GNUC_CONST;
00258 
00259 
00260 /* Compute canonical ordering of a string in-place.  This rearranges
00261    decomposed characters in the string according to their combining
00262    classes.  See the Unicode manual for more information.  */
00263 void g_unicode_canonical_ordering (gunichar *string,
00264                                    gsize     len);
00265 
00266 /* Compute canonical decomposition of a character.  Returns g_malloc()d
00267    string of Unicode characters.  RESULT_LEN is set to the resulting
00268    length of the string.  */
00269 gunichar *g_unicode_canonical_decomposition (gunichar  ch,
00270                                              gsize    *result_len) G_GNUC_MALLOC;
00271 
00272 /* Array of skip-bytes-per-initial character.
00273  */
00274 GLIB_VAR const gchar * const g_utf8_skip;
00275 
00276 #define g_utf8_next_char(p) (char *)((p) + g_utf8_skip[*(const guchar *)(p)])
00277 
00278 gunichar g_utf8_get_char           (const gchar  *p) G_GNUC_PURE;
00279 gunichar g_utf8_get_char_validated (const  gchar *p,
00280                                     gssize        max_len) G_GNUC_PURE;
00281 
00282 gchar*   g_utf8_offset_to_pointer (const gchar *str,
00283                                    glong        offset) G_GNUC_PURE;
00284 glong    g_utf8_pointer_to_offset (const gchar *str,      
00285                                    const gchar *pos) G_GNUC_PURE;
00286 gchar*   g_utf8_prev_char         (const gchar *p) G_GNUC_PURE;
00287 gchar*   g_utf8_find_next_char    (const gchar *p,
00288                                    const gchar *end) G_GNUC_PURE;
00289 gchar*   g_utf8_find_prev_char    (const gchar *str,
00290                                    const gchar *p) G_GNUC_PURE;
00291 
00292 glong g_utf8_strlen (const gchar *p,  
00293                      gssize       max) G_GNUC_PURE;
00294 
00295 /* Copies n characters from src to dest */
00296 gchar* g_utf8_strncpy (gchar       *dest,
00297                        const gchar *src,
00298                        gsize        n);
00299 
00300 /* Find the UTF-8 character corresponding to ch, in string p. These
00301    functions are equivalants to strchr and strrchr */
00302 gchar* g_utf8_strchr  (const gchar *p,
00303                        gssize       len,
00304                        gunichar     c);
00305 gchar* g_utf8_strrchr (const gchar *p,
00306                        gssize       len,
00307                        gunichar     c);
00308 gchar* g_utf8_strreverse (const gchar *str,
00309                           gssize len);
00310 
00311 gunichar2 *g_utf8_to_utf16     (const gchar      *str,
00312                                 glong             len,            
00313                                 glong            *items_read,     
00314                                 glong            *items_written,  
00315                                 GError          **error) G_GNUC_MALLOC;
00316 gunichar * g_utf8_to_ucs4      (const gchar      *str,
00317                                 glong             len,            
00318                                 glong            *items_read,     
00319                                 glong            *items_written,  
00320                                 GError          **error) G_GNUC_MALLOC;
00321 gunichar * g_utf8_to_ucs4_fast (const gchar      *str,
00322                                 glong             len,            
00323                                 glong            *items_written) G_GNUC_MALLOC; 
00324 gunichar * g_utf16_to_ucs4     (const gunichar2  *str,
00325                                 glong             len,            
00326                                 glong            *items_read,     
00327                                 glong            *items_written,  
00328                                 GError          **error) G_GNUC_MALLOC;
00329 gchar*     g_utf16_to_utf8     (const gunichar2  *str,
00330                                 glong             len,            
00331                                 glong            *items_read,     
00332                                 glong            *items_written,  
00333                                 GError          **error) G_GNUC_MALLOC;
00334 gunichar2 *g_ucs4_to_utf16     (const gunichar   *str,
00335                                 glong             len,            
00336                                 glong            *items_read,     
00337                                 glong            *items_written,  
00338                                 GError          **error) G_GNUC_MALLOC;
00339 gchar*     g_ucs4_to_utf8      (const gunichar   *str,
00340                                 glong             len,            
00341                                 glong            *items_read,     
00342                                 glong            *items_written,  
00343                                 GError          **error) G_GNUC_MALLOC;
00344 
00345 /* Convert a single character into UTF-8. outbuf must have at
00346  * least 6 bytes of space. Returns the number of bytes in the
00347  * result.
00348  */
00349 gint      g_unichar_to_utf8 (gunichar    c,
00350                              gchar      *outbuf);
00351 
00352 /* Validate a UTF8 string, return TRUE if valid, put pointer to
00353  * first invalid char in **end
00354  */
00355 
00356 gboolean g_utf8_validate (const gchar  *str,
00357                           gssize        max_len,  
00358                           const gchar **end);
00359 
00360 /* Validate a Unicode character */
00361 gboolean g_unichar_validate (gunichar ch) G_GNUC_CONST;
00362 
00363 gchar *g_utf8_strup   (const gchar *str,
00364                        gssize       len) G_GNUC_MALLOC;
00365 gchar *g_utf8_strdown (const gchar *str,
00366                        gssize       len) G_GNUC_MALLOC;
00367 gchar *g_utf8_casefold (const gchar *str,
00368                         gssize       len) G_GNUC_MALLOC;
00369 
00370 typedef enum {
00371   G_NORMALIZE_DEFAULT,
00372   G_NORMALIZE_NFD = G_NORMALIZE_DEFAULT,
00373   G_NORMALIZE_DEFAULT_COMPOSE,
00374   G_NORMALIZE_NFC = G_NORMALIZE_DEFAULT_COMPOSE,
00375   G_NORMALIZE_ALL,
00376   G_NORMALIZE_NFKD = G_NORMALIZE_ALL,
00377   G_NORMALIZE_ALL_COMPOSE,
00378   G_NORMALIZE_NFKC = G_NORMALIZE_ALL_COMPOSE
00379 } GNormalizeMode;
00380 
00381 gchar *g_utf8_normalize (const gchar   *str,
00382                          gssize         len,
00383                          GNormalizeMode mode) G_GNUC_MALLOC;
00384 
00385 gint   g_utf8_collate     (const gchar *str1,
00386                            const gchar *str2) G_GNUC_PURE;
00387 gchar *g_utf8_collate_key (const gchar *str,
00388                            gssize       len) G_GNUC_MALLOC;
00389 gchar *g_utf8_collate_key_for_filename (const gchar *str,
00390                                         gssize       len) G_GNUC_MALLOC;
00391 
00392 gboolean g_unichar_get_mirror_char (gunichar ch,
00393                                     gunichar *mirrored_ch);
00394 
00395 GUnicodeScript g_unichar_get_script (gunichar ch) G_GNUC_CONST;
00396 
00397 
00398 /* private */
00399 
00400 gchar *_g_utf8_make_valid (const gchar *name);
00401 
00402 G_END_DECLS
00403 
00404 #endif /* __G_UNICODE_H__ */