applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[platform/upstream/glib.git] / glib / gunicode.h
1 /* gunicode.h - Unicode manipulation functions
2  *
3  *  Copyright (C) 1999, 2000 Tom Tromey
4  *  Copyright 2000 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 #ifndef __GUNICODE_H__
23 #define __GUNICODE_H__
24
25 #include <stddef.h>      /* For size_t */
26
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31
32 typedef guint32 gunichar;
33 typedef guint16 gunichar2;
34
35 /* These are the possible character classifications.  */
36 typedef enum {
37   G_UNICODE_CONTROL,
38   G_UNICODE_FORMAT,
39   G_UNICODE_UNASSIGNED,
40   G_UNICODE_PRIVATE_USE,
41   G_UNICODE_SURROGATE,
42   G_UNICODE_LOWERCASE_LETTER,
43   G_UNICODE_MODIFIER_LETTER,
44   G_UNICODE_OTHER_LETTER,
45   G_UNICODE_TITLECASE_LETTER,
46   G_UNICODE_UPPERCASE_LETTER,
47   G_UNICODE_COMBINING_MARK,
48   G_UNICODE_ENCLOSING_MARK,
49   G_UNICODE_NON_SPACING_MARK,
50   G_UNICODE_DECIMAL_NUMBER,
51   G_UNICODE_LETTER_NUMBER,
52   G_UNICODE_OTHER_NUMBER,
53   G_UNICODE_CONNECT_PUNCTUATION,
54   G_UNICODE_DASH_PUNCTUATION,
55   G_UNICODE_CLOSE_PUNCTUATION,
56   G_UNICODE_FINAL_PUNCTUATION,
57   G_UNICODE_INITIAL_PUNCTUATION,
58   G_UNICODE_OTHER_PUNCTUATION,
59   G_UNICODE_OPEN_PUNCTUATION,
60   G_UNICODE_CURRENCY_SYMBOL,
61   G_UNICODE_MODIFIER_SYMBOL,
62   G_UNICODE_MATH_SYMBOL,
63   G_UNICODE_OTHER_SYMBOL,
64   G_UNICODE_LINE_SEPARATOR,
65   G_UNICODE_PARAGRAPH_SEPARATOR,
66   G_UNICODE_SPACE_SEPARATOR
67 } GUnicodeType;
68
69 /* Returns TRUE if current locale uses UTF-8 charset.  If CHARSET is
70  * not null, sets *CHARSET to the name of the current locale's
71  * charset.  This value is statically allocated.
72  */
73 gboolean g_get_charset (char **charset);
74
75 /* These are all analogs of the <ctype.h> functions.
76  */
77 gboolean g_unichar_isalnum   (gunichar c);
78 gboolean g_unichar_isalpha   (gunichar c);
79 gboolean g_unichar_iscntrl   (gunichar c);
80 gboolean g_unicphar_isdigit   (gunichar c);
81 gboolean g_unichar_isgraph   (gunichar c);
82 gboolean g_unichar_islower   (gunichar c);
83 gboolean g_unichar_isprint   (gunichar c);
84 gboolean g_unichar_ispunct   (gunichar c);
85 gboolean g_unichar_isspace   (gunichar c);
86 gboolean g_unichar_isupper   (gunichar c);
87 gboolean g_unichar_isxdigit  (gunichar c);
88 gboolean g_unichar_istitle   (gunichar c);
89 gboolean g_unichar_isdefined (gunichar c);
90 gboolean g_unichar_iswide    (gunichar c);
91
92 /* More <ctype.h> functions.  These convert between the three cases.
93  * See the Unicode book to understand title case.  */
94 gunichar g_unichar_toupper (gunichar c);
95 gunichar g_unichar_tolower (gunichar c);
96 gunichar g_unichar_totitle (gunichar c);
97
98 /* If C is a digit (according to `g_unichar_isdigit'), then return its
99    numeric value.  Otherwise return -1.  */
100 gint g_unichar_digit_value (gunichar c);
101
102 gint g_unichar_xdigit_value (gunichar c);
103
104 /* Return the Unicode character type of a given character.  */
105 GUnicodeType g_unichar_type (gunichar c);
106
107
108
109 /* Compute canonical ordering of a string in-place.  This rearranges
110    decomposed characters in the string according to their combining
111    classes.  See the Unicode manual for more information.  */
112 void g_unicode_canonical_ordering (gunichar *string,
113                                    size_t   len);
114
115 /* Compute canonical decomposition of a character.  Returns g_malloc()d
116    string of Unicode characters.  RESULT_LEN is set to the resulting
117    length of the string.  */
118 gunichar *g_unicode_canonical_decomposition (gunichar  ch,
119                                              size_t   *result_len);
120
121 /* Array of skip-bytes-per-initial character.
122  * We prefix variable declarations so they can
123  * properly get exported in windows dlls.
124  */
125 #ifndef GLIB_VAR
126 #  ifdef G_OS_WIN32
127 #    ifdef GLIB_COMPILATION
128 #      define GLIB_VAR __declspec(dllexport)
129 #    else /* !GLIB_COMPILATION */
130 #      define GLIB_VAR extern __declspec(dllimport)
131 #    endif /* !GLIB_COMPILATION */
132 #  else /* !G_OS_WIN32 */
133 #    define GLIB_VAR extern
134 #  endif /* !G_OS_WIN32 */
135 #endif /* !GLIB_VAR */
136
137 GLIB_VAR char g_utf8_skip[256];
138
139 #define g_utf8_next_char(p) (char *)((p) + g_utf8_skip[*(guchar *)(p)])
140
141 gunichar g_utf8_get_char          (const gchar *p);
142 gchar *  g_utf8_offset_to_pointer  (const gchar *str,
143                                     gint         offset);
144 gint     g_utf8_pointer_to_offset (const gchar *str,
145                                    const gchar *pos);
146 gchar *  g_utf8_prev_char         (const gchar *p);
147 gchar *  g_utf8_find_next_char    (const gchar *p,
148                                    const gchar *bound);
149 gchar *  g_utf8_find_prev_char    (const gchar *str,
150                                    const gchar *p);
151
152 gint g_utf8_strlen (const gchar *p,
153                     gint         max);
154
155 /* Copies n characters from src to dest */
156 gchar *g_utf8_strncpy (gchar       *dest,
157                        const gchar *src,
158                        size_t       n);
159
160 /* Find the UTF-8 character corresponding to ch, in string p. These
161    functions are equivalants to strchr and strrchr */
162
163 gchar *g_utf8_strchr  (const gchar *p,
164                        gunichar     ch);
165 gchar *g_utf8_strrchr (const gchar *p,
166                        gunichar     ch);
167
168 gunichar2 *g_utf8_to_utf16 (const gchar     *str,
169                             gint             len);
170 gunichar * g_utf8_to_ucs4  (const gchar     *str,
171                             gint             len);
172 gunichar * g_utf16_to_ucs4 (const gunichar2 *str,
173                             gint             len);
174 gchar *    g_utf16_to_utf8 (const gunichar2 *str,
175                             gint             len);
176 gunichar * g_ucs4_to_utf16 (const gunichar  *str,
177                             gint             len);
178 gchar *    g_ucs4_to_utf8  (const gunichar  *str,
179                             gint             len);
180
181 /* Convert a single character into UTF-8. outbuf must have at
182  * least 6 bytes of space. Returns the number of bytes in the
183  * result.
184  */
185 gint      g_unichar_to_utf8 (gunichar    c,
186                              char       *outbuf);
187
188 #ifdef __cplusplus
189 }
190 #endif
191
192 #endif /* GUNICODE_H */