Home | History | Annotate | Download | only in glib
      1 /* gunicode.h - Unicode manipulation functions
      2  *
      3  *  Copyright (C) 1999, 2000 Tom Tromey
      4  *  Copyright 2000, 2005 Red Hat, Inc.
      5  *
      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.
     10  *
     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.
     15  *
     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.
     20  */
     21 
     22 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
     23 #error "Only <glib.h> can be included directly."
     24 #endif
     25 
     26 #ifndef __G_UNICODE_H__
     27 #define __G_UNICODE_H__
     28 
     29 #include <glib/gerror.h>
     30 #include <glib/gtypes.h>
     31 
     32 G_BEGIN_DECLS
     33 
     34 typedef guint32 gunichar;
     35 typedef guint16 gunichar2;
     36 
     37 /* These are the possible character classifications.
     38  * See http://www.unicode.org/Public/UNIDATA/UCD.html#General_Category_Values
     39  */
     40 typedef enum
     41 {
     42   G_UNICODE_CONTROL,
     43   G_UNICODE_FORMAT,
     44   G_UNICODE_UNASSIGNED,
     45   G_UNICODE_PRIVATE_USE,
     46   G_UNICODE_SURROGATE,
     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
     72 } GUnicodeType;
     73 
     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/
     78  */
     79 typedef enum
     80 {
     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 } GUnicodeBreakType;
    118 
    119 typedef enum
    120 {                         /* ISO 15924 code */
    121   G_UNICODE_SCRIPT_INVALID_CODE = -1,
    122   G_UNICODE_SCRIPT_COMMON       = 0,   /* Zyyy */
    123   G_UNICODE_SCRIPT_INHERITED,          /* Qaai */
    124   G_UNICODE_SCRIPT_ARABIC,             /* Arab */
    125   G_UNICODE_SCRIPT_ARMENIAN,           /* Armn */
    126   G_UNICODE_SCRIPT_BENGALI,            /* Beng */
    127   G_UNICODE_SCRIPT_BOPOMOFO,           /* Bopo */
    128   G_UNICODE_SCRIPT_CHEROKEE,           /* Cher */
    129   G_UNICODE_SCRIPT_COPTIC,             /* Qaac */
    130   G_UNICODE_SCRIPT_CYRILLIC,           /* Cyrl (Cyrs) */
    131   G_UNICODE_SCRIPT_DESERET,            /* Dsrt */
    132   G_UNICODE_SCRIPT_DEVANAGARI,         /* Deva */
    133   G_UNICODE_SCRIPT_ETHIOPIC,           /* Ethi */
    134   G_UNICODE_SCRIPT_GEORGIAN,           /* Geor (Geon, Geoa) */
    135   G_UNICODE_SCRIPT_GOTHIC,             /* Goth */
    136   G_UNICODE_SCRIPT_GREEK,              /* Grek */
    137   G_UNICODE_SCRIPT_GUJARATI,           /* Gujr */
    138   G_UNICODE_SCRIPT_GURMUKHI,           /* Guru */
    139   G_UNICODE_SCRIPT_HAN,                /* Hani */
    140   G_UNICODE_SCRIPT_HANGUL,             /* Hang */
    141   G_UNICODE_SCRIPT_HEBREW,             /* Hebr */
    142   G_UNICODE_SCRIPT_HIRAGANA,           /* Hira */
    143   G_UNICODE_SCRIPT_KANNADA,            /* Knda */
    144   G_UNICODE_SCRIPT_KATAKANA,           /* Kana */
    145   G_UNICODE_SCRIPT_KHMER,              /* Khmr */
    146   G_UNICODE_SCRIPT_LAO,                /* Laoo */
    147   G_UNICODE_SCRIPT_LATIN,              /* Latn (Latf, Latg) */
    148   G_UNICODE_SCRIPT_MALAYALAM,          /* Mlym */
    149   G_UNICODE_SCRIPT_MONGOLIAN,          /* Mong */
    150   G_UNICODE_SCRIPT_MYANMAR,            /* Mymr */
    151   G_UNICODE_SCRIPT_OGHAM,              /* Ogam */
    152   G_UNICODE_SCRIPT_OLD_ITALIC,         /* Ital */
    153   G_UNICODE_SCRIPT_ORIYA,              /* Orya */
    154   G_UNICODE_SCRIPT_RUNIC,              /* Runr */
    155   G_UNICODE_SCRIPT_SINHALA,            /* Sinh */
    156   G_UNICODE_SCRIPT_SYRIAC,             /* Syrc (Syrj, Syrn, Syre) */
    157   G_UNICODE_SCRIPT_TAMIL,              /* Taml */
    158   G_UNICODE_SCRIPT_TELUGU,             /* Telu */
    159   G_UNICODE_SCRIPT_THAANA,             /* Thaa */
    160   G_UNICODE_SCRIPT_THAI,               /* Thai */
    161   G_UNICODE_SCRIPT_TIBETAN,            /* Tibt */
    162   G_UNICODE_SCRIPT_CANADIAN_ABORIGINAL, /* Cans */
    163   G_UNICODE_SCRIPT_YI,                 /* Yiii */
    164   G_UNICODE_SCRIPT_TAGALOG,            /* Tglg */
    165   G_UNICODE_SCRIPT_HANUNOO,            /* Hano */
    166   G_UNICODE_SCRIPT_BUHID,              /* Buhd */
    167   G_UNICODE_SCRIPT_TAGBANWA,           /* Tagb */
    168 
    169   /* Unicode-4.0 additions */
    170   G_UNICODE_SCRIPT_BRAILLE,            /* Brai */
    171   G_UNICODE_SCRIPT_CYPRIOT,            /* Cprt */
    172   G_UNICODE_SCRIPT_LIMBU,              /* Limb */
    173   G_UNICODE_SCRIPT_OSMANYA,            /* Osma */
    174   G_UNICODE_SCRIPT_SHAVIAN,            /* Shaw */
    175   G_UNICODE_SCRIPT_LINEAR_B,           /* Linb */
    176   G_UNICODE_SCRIPT_TAI_LE,             /* Tale */
    177   G_UNICODE_SCRIPT_UGARITIC,           /* Ugar */
    178 
    179   /* Unicode-4.1 additions */
    180   G_UNICODE_SCRIPT_NEW_TAI_LUE,        /* Talu */
    181   G_UNICODE_SCRIPT_BUGINESE,           /* Bugi */
    182   G_UNICODE_SCRIPT_GLAGOLITIC,         /* Glag */
    183   G_UNICODE_SCRIPT_TIFINAGH,           /* Tfng */
    184   G_UNICODE_SCRIPT_SYLOTI_NAGRI,       /* Sylo */
    185   G_UNICODE_SCRIPT_OLD_PERSIAN,        /* Xpeo */
    186   G_UNICODE_SCRIPT_KHAROSHTHI,         /* Khar */
    187 
    188   /* Unicode-5.0 additions */
    189   G_UNICODE_SCRIPT_UNKNOWN,            /* Zzzz */
    190   G_UNICODE_SCRIPT_BALINESE,           /* Bali */
    191   G_UNICODE_SCRIPT_CUNEIFORM,          /* Xsux */
    192   G_UNICODE_SCRIPT_PHOENICIAN,         /* Phnx */
    193   G_UNICODE_SCRIPT_PHAGS_PA,           /* Phag */
    194   G_UNICODE_SCRIPT_NKO,                /* Nkoo */
    195 
    196   /* Unicode-5.1 additions */
    197   G_UNICODE_SCRIPT_KAYAH_LI,           /* Kali */
    198   G_UNICODE_SCRIPT_LEPCHA,             /* Lepc */
    199   G_UNICODE_SCRIPT_REJANG,             /* Rjng */
    200   G_UNICODE_SCRIPT_SUNDANESE,          /* Sund */
    201   G_UNICODE_SCRIPT_SAURASHTRA,         /* Saur */
    202   G_UNICODE_SCRIPT_CHAM,               /* Cham */
    203   G_UNICODE_SCRIPT_OL_CHIKI,           /* Olck */
    204   G_UNICODE_SCRIPT_VAI,                /* Vaii */
    205   G_UNICODE_SCRIPT_CARIAN,             /* Cari */
    206   G_UNICODE_SCRIPT_LYCIAN,             /* Lyci */
    207   G_UNICODE_SCRIPT_LYDIAN              /* Lydi */
    208 } GUnicodeScript;
    209 
    210 /* Returns TRUE if current locale uses UTF-8 charset.  If CHARSET is
    211  * not null, sets *CHARSET to the name of the current locale's
    212  * charset.  This value is statically allocated, and should be copied
    213  * in case the locale's charset will be changed later using setlocale()
    214  * or in some other way.
    215  */
    216 gboolean g_get_charset (G_CONST_RETURN char **charset);
    217 
    218 /* These are all analogs of the <ctype.h> functions.
    219  */
    220 gboolean g_unichar_isalnum   (gunichar c) G_GNUC_CONST;
    221 gboolean g_unichar_isalpha   (gunichar c) G_GNUC_CONST;
    222 gboolean g_unichar_iscntrl   (gunichar c) G_GNUC_CONST;
    223 gboolean g_unichar_isdigit   (gunichar c) G_GNUC_CONST;
    224 gboolean g_unichar_isgraph   (gunichar c) G_GNUC_CONST;
    225 gboolean g_unichar_islower   (gunichar c) G_GNUC_CONST;
    226 gboolean g_unichar_isprint   (gunichar c) G_GNUC_CONST;
    227 gboolean g_unichar_ispunct   (gunichar c) G_GNUC_CONST;
    228 gboolean g_unichar_isspace   (gunichar c) G_GNUC_CONST;
    229 gboolean g_unichar_isupper   (gunichar c) G_GNUC_CONST;
    230 gboolean g_unichar_isxdigit  (gunichar c) G_GNUC_CONST;
    231 gboolean g_unichar_istitle   (gunichar c) G_GNUC_CONST;
    232 gboolean g_unichar_isdefined (gunichar c) G_GNUC_CONST;
    233 gboolean g_unichar_iswide    (gunichar c) G_GNUC_CONST;
    234 gboolean g_unichar_iswide_cjk(gunichar c) G_GNUC_CONST;
    235 gboolean g_unichar_iszerowidth(gunichar c) G_GNUC_CONST;
    236 gboolean g_unichar_ismark    (gunichar c) G_GNUC_CONST;
    237 
    238 /* More <ctype.h> functions.  These convert between the three cases.
    239  * See the Unicode book to understand title case.  */
    240 gunichar g_unichar_toupper (gunichar c) G_GNUC_CONST;
    241 gunichar g_unichar_tolower (gunichar c) G_GNUC_CONST;
    242 gunichar g_unichar_totitle (gunichar c) G_GNUC_CONST;
    243 
    244 /* If C is a digit (according to `g_unichar_isdigit'), then return its
    245    numeric value.  Otherwise return -1.  */
    246 gint g_unichar_digit_value (gunichar c) G_GNUC_CONST;
    247 
    248 gint g_unichar_xdigit_value (gunichar c) G_GNUC_CONST;
    249 
    250 /* Return the Unicode character type of a given character.  */
    251 GUnicodeType g_unichar_type (gunichar c) G_GNUC_CONST;
    252 
    253 /* Return the line break property for a given character */
    254 GUnicodeBreakType g_unichar_break_type (gunichar c) G_GNUC_CONST;
    255 
    256 /* Returns the combining class for a given character */
    257 gint g_unichar_combining_class (gunichar uc) G_GNUC_CONST;
    258 
    259 
    260 /* Compute canonical ordering of a string in-place.  This rearranges
    261    decomposed characters in the string according to their combining
    262    classes.  See the Unicode manual for more information.  */
    263 void g_unicode_canonical_ordering (gunichar *string,
    264 				   gsize     len);
    265 
    266 /* Compute canonical decomposition of a character.  Returns g_malloc()d
    267    string of Unicode characters.  RESULT_LEN is set to the resulting
    268    length of the string.  */
    269 gunichar *g_unicode_canonical_decomposition (gunichar  ch,
    270 					     gsize    *result_len) G_GNUC_MALLOC;
    271 
    272 /* Array of skip-bytes-per-initial character.
    273  */
    274 GLIB_VAR const gchar * const g_utf8_skip;
    275 
    276 #define g_utf8_next_char(p) (char *)((p) + g_utf8_skip[*(const guchar *)(p)])
    277 
    278 gunichar g_utf8_get_char           (const gchar  *p) G_GNUC_PURE;
    279 gunichar g_utf8_get_char_validated (const  gchar *p,
    280 				    gssize        max_len) G_GNUC_PURE;
    281 
    282 gchar*   g_utf8_offset_to_pointer (const gchar *str,
    283                                    glong        offset) G_GNUC_PURE;
    284 glong    g_utf8_pointer_to_offset (const gchar *str,
    285 				   const gchar *pos) G_GNUC_PURE;
    286 gchar*   g_utf8_prev_char         (const gchar *p) G_GNUC_PURE;
    287 gchar*   g_utf8_find_next_char    (const gchar *p,
    288 				   const gchar *end) G_GNUC_PURE;
    289 gchar*   g_utf8_find_prev_char    (const gchar *str,
    290 				   const gchar *p) G_GNUC_PURE;
    291 
    292 glong g_utf8_strlen (const gchar *p,
    293 		     gssize       max) G_GNUC_PURE;
    294 
    295 /* Copies n characters from src to dest */
    296 gchar* g_utf8_strncpy (gchar       *dest,
    297 		       const gchar *src,
    298 		       gsize        n);
    299 
    300 /* Find the UTF-8 character corresponding to ch, in string p. These
    301    functions are equivalants to strchr and strrchr */
    302 gchar* g_utf8_strchr  (const gchar *p,
    303 		       gssize       len,
    304 		       gunichar     c);
    305 gchar* g_utf8_strrchr (const gchar *p,
    306 		       gssize       len,
    307 		       gunichar     c);
    308 gchar* g_utf8_strreverse (const gchar *str,
    309 			  gssize len);
    310 
    311 gunichar2 *g_utf8_to_utf16     (const gchar      *str,
    312 				glong             len,
    313 				glong            *items_read,
    314 				glong            *items_written,
    315 				GError          **error) G_GNUC_MALLOC;
    316 gunichar * g_utf8_to_ucs4      (const gchar      *str,
    317 				glong             len,
    318 				glong            *items_read,
    319 				glong            *items_written,
    320 				GError          **error) G_GNUC_MALLOC;
    321 gunichar * g_utf8_to_ucs4_fast (const gchar      *str,
    322 				glong             len,
    323 				glong            *items_written) G_GNUC_MALLOC;
    324 gunichar * g_utf16_to_ucs4     (const gunichar2  *str,
    325 				glong             len,
    326 				glong            *items_read,
    327 				glong            *items_written,
    328 				GError          **error) G_GNUC_MALLOC;
    329 gchar*     g_utf16_to_utf8     (const gunichar2  *str,
    330 				glong             len,
    331 				glong            *items_read,
    332 				glong            *items_written,
    333 				GError          **error) G_GNUC_MALLOC;
    334 gunichar2 *g_ucs4_to_utf16     (const gunichar   *str,
    335 				glong             len,
    336 				glong            *items_read,
    337 				glong            *items_written,
    338 				GError          **error) G_GNUC_MALLOC;
    339 gchar*     g_ucs4_to_utf8      (const gunichar   *str,
    340 				glong             len,
    341 				glong            *items_read,
    342 				glong            *items_written,
    343 				GError          **error) G_GNUC_MALLOC;
    344 
    345 /* Convert a single character into UTF-8. outbuf must have at
    346  * least 6 bytes of space. Returns the number of bytes in the
    347  * result.
    348  */
    349 gint      g_unichar_to_utf8 (gunichar    c,
    350 			     gchar      *outbuf);
    351 
    352 /* Validate a UTF8 string, return TRUE if valid, put pointer to
    353  * first invalid char in **end
    354  */
    355 
    356 gboolean g_utf8_validate (const gchar  *str,
    357                           gssize        max_len,
    358                           const gchar **end);
    359 
    360 /* Validate a Unicode character */
    361 gboolean g_unichar_validate (gunichar ch) G_GNUC_CONST;
    362 
    363 gchar *g_utf8_strup   (const gchar *str,
    364 		       gssize       len) G_GNUC_MALLOC;
    365 gchar *g_utf8_strdown (const gchar *str,
    366 		       gssize       len) G_GNUC_MALLOC;
    367 gchar *g_utf8_casefold (const gchar *str,
    368 			gssize       len) G_GNUC_MALLOC;
    369 
    370 typedef enum {
    371   G_NORMALIZE_DEFAULT,
    372   G_NORMALIZE_NFD = G_NORMALIZE_DEFAULT,
    373   G_NORMALIZE_DEFAULT_COMPOSE,
    374   G_NORMALIZE_NFC = G_NORMALIZE_DEFAULT_COMPOSE,
    375   G_NORMALIZE_ALL,
    376   G_NORMALIZE_NFKD = G_NORMALIZE_ALL,
    377   G_NORMALIZE_ALL_COMPOSE,
    378   G_NORMALIZE_NFKC = G_NORMALIZE_ALL_COMPOSE
    379 } GNormalizeMode;
    380 
    381 gchar *g_utf8_normalize (const gchar   *str,
    382 			 gssize         len,
    383 			 GNormalizeMode mode) G_GNUC_MALLOC;
    384 
    385 gint   g_utf8_collate     (const gchar *str1,
    386 			   const gchar *str2) G_GNUC_PURE;
    387 gchar *g_utf8_collate_key (const gchar *str,
    388 			   gssize       len) G_GNUC_MALLOC;
    389 gchar *g_utf8_collate_key_for_filename (const gchar *str,
    390 			                gssize       len) G_GNUC_MALLOC;
    391 
    392 gboolean g_unichar_get_mirror_char (gunichar ch,
    393                                     gunichar *mirrored_ch);
    394 
    395 GUnicodeScript g_unichar_get_script (gunichar ch) G_GNUC_CONST;
    396 
    397 
    398 /* private */
    399 
    400 gchar *_g_utf8_make_valid (const gchar *name);
    401 
    402 G_END_DECLS
    403 
    404 #endif /* __G_UNICODE_H__ */
    405