Fixed the WINE_NO_STRICT #ifdefs to do the right thing if STRICT is
[wine] / include / wine / unicode.h
1 /*
2  * Wine internal Unicode definitions
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This 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 this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifndef __WINE_UNICODE_H
22 #define __WINE_UNICODE_H
23
24 #include "windef.h"
25 #include "winnls.h"
26
27 /* code page info common to SBCS and DBCS */
28 struct cp_info
29 {
30     unsigned int          codepage;          /* codepage id */
31     unsigned int          char_size;         /* char size (1 or 2 bytes) */
32     WCHAR                 def_char;          /* default char value (can be double-byte) */
33     WCHAR                 def_unicode_char;  /* default Unicode char value */
34     const char           *name;              /* code page name */
35 };
36
37 struct sbcs_table
38 {
39     struct cp_info        info;
40     const WCHAR          *cp2uni;            /* code page -> Unicode map */
41     const unsigned char  *uni2cp_low;        /* Unicode -> code page map */
42     const unsigned short *uni2cp_high;
43 };
44
45 struct dbcs_table
46 {
47     struct cp_info        info;
48     const WCHAR          *cp2uni;            /* code page -> Unicode map */
49     const unsigned char  *cp2uni_leadbytes;
50     const unsigned short *uni2cp_low;        /* Unicode -> code page map */
51     const unsigned short *uni2cp_high;
52     unsigned char         lead_bytes[12];    /* lead bytes ranges */
53 };
54
55 union cptable
56 {
57     struct cp_info    info;
58     struct sbcs_table sbcs;
59     struct dbcs_table dbcs;
60 };
61
62 extern const union cptable *cp_get_table( unsigned int codepage );
63 extern const union cptable *cp_enum_table( unsigned int index );
64
65 extern int cp_mbstowcs( const union cptable *table, int flags,
66                         const char *src, int srclen,
67                         WCHAR *dst, int dstlen );
68 extern int cp_wcstombs( const union cptable *table, int flags,
69                         const WCHAR *src, int srclen,
70                         char *dst, int dstlen, const char *defchar, int *used );
71 extern int utf8_wcstombs( const WCHAR *src, int srclen, char *dst, int dstlen );
72 extern int utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen );
73
74 extern int strcmpiW( const WCHAR *str1, const WCHAR *str2 );
75 extern int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n );
76 extern WCHAR *strstrW( const WCHAR *str, const WCHAR *sub );
77 extern long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base );
78 extern unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base );
79
80 static inline int is_dbcs_leadbyte( const union cptable *table, unsigned char ch )
81 {
82     return (table->info.char_size == 2) && (table->dbcs.cp2uni_leadbytes[ch]);
83 }
84
85 static inline WCHAR tolowerW( WCHAR ch )
86 {
87     extern const WCHAR casemap_lower[];
88     return ch + casemap_lower[casemap_lower[ch >> 8] + (ch & 0xff)];
89 }
90
91 static inline WCHAR toupperW( WCHAR ch )
92 {
93     extern const WCHAR casemap_upper[];
94     return ch + casemap_upper[casemap_upper[ch >> 8] + (ch & 0xff)];
95 }
96
97 /* the character type contains the C1_* flags in the low 12 bits */
98 /* and the C2_* type in the high 4 bits */
99 static inline unsigned short get_char_typeW( WCHAR ch )
100 {
101     extern const unsigned short wctype_table[];
102     return wctype_table[wctype_table[ch >> 8] + (ch & 0xff)];
103 }
104
105 inline static int iscntrlW( WCHAR wc )
106 {
107     return get_char_typeW(wc) & C1_CNTRL;
108 }
109
110 inline static int ispunctW( WCHAR wc )
111 {
112     return get_char_typeW(wc) & C1_PUNCT;
113 }
114
115 inline static int isspaceW( WCHAR wc )
116 {
117     return get_char_typeW(wc) & C1_SPACE;
118 }
119
120 inline static int isdigitW( WCHAR wc )
121 {
122     return get_char_typeW(wc) & C1_DIGIT;
123 }
124
125 inline static int isxdigitW( WCHAR wc )
126 {
127     return get_char_typeW(wc) & C1_XDIGIT;
128 }
129
130 inline static int islowerW( WCHAR wc )
131 {
132     return get_char_typeW(wc) & C1_LOWER;
133 }
134
135 inline static int isupperW( WCHAR wc )
136 {
137     return get_char_typeW(wc) & C1_UPPER;
138 }
139
140 inline static int isalnumW( WCHAR wc )
141 {
142     return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
143 }
144
145 inline static int isalphaW( WCHAR wc )
146 {
147     return get_char_typeW(wc) & (C1_ALPHA|C1_LOWER|C1_UPPER);
148 }
149
150 inline static int isgraphW( WCHAR wc )
151 {
152     return get_char_typeW(wc) & (C1_ALPHA|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER);
153 }
154
155 inline static int isprintW( WCHAR wc )
156 {
157     return get_char_typeW(wc) & (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER);
158 }
159
160 /* some useful string manipulation routines */
161
162 static inline unsigned int strlenW( const WCHAR *str )
163 {
164 #if defined(__i386__) && defined(__GNUC__)
165     int dummy, res;
166     __asm__ __volatile__( "cld\n\t"
167                           "repnz\n\t"
168                           "scasw\n\t"
169                           "notl %0"
170                           : "=c" (res), "=&D" (dummy)
171                           : "0" (0xffffffff), "1" (str), "a" (0) );
172     return res - 1;
173 #else
174     const WCHAR *s = str;
175     while (*s) s++;
176     return s - str;
177 #endif
178 }
179
180 static inline WCHAR *strcpyW( WCHAR *dst, const WCHAR *src )
181 {
182 #if defined(__i386__) && defined(__GNUC__)
183     int dummy1, dummy2, dummy3;
184     __asm__ __volatile__( "cld\n"
185                           "1:\tlodsw\n\t"
186                           "stosw\n\t"
187                           "testw %%ax,%%ax\n\t"
188                           "jne 1b"
189                           : "=&S" (dummy1), "=&D" (dummy2), "=&a" (dummy3)
190                           : "0" (src), "1" (dst)
191                           : "memory" );
192 #else
193     WCHAR *p = dst;
194     while ((*p++ = *src++));
195 #endif
196     return dst;
197 }
198
199 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
200 {
201     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
202     return *str1 - *str2;
203 }
204
205 static inline int strncmpW( const WCHAR *str1, const WCHAR *str2, int n )
206 {
207     if (n <= 0) return 0;
208     while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
209     return *str1 - *str2;
210 }
211
212 static inline WCHAR *strncpyW( WCHAR *str1, const WCHAR *str2, int n )
213 {
214     WCHAR *ret = str1;
215     while (n-- > 0) if (!(*str1++ = *str2++)) break;
216     while (n-- > 0) *str1++ = 0;
217     return ret;
218 }
219
220 static inline WCHAR *strcatW( WCHAR *dst, const WCHAR *src )
221 {
222     strcpyW( dst + strlenW(dst), src );
223     return dst;
224 }
225
226 static inline WCHAR *strchrW( const WCHAR *str, WCHAR ch )
227 {
228     for ( ; *str; str++) if (*str == ch) return (WCHAR *)str;
229     return NULL;
230 }
231
232 static inline WCHAR *strrchrW( const WCHAR *str, WCHAR ch )
233 {
234     WCHAR *ret = NULL;
235     for ( ; *str; str++) if (*str == ch) ret = (WCHAR *)str;
236     return ret;
237 }
238
239 static inline WCHAR *strlwrW( WCHAR *str )
240 {
241     WCHAR *ret = str;
242     while ((*str = tolowerW(*str))) str++;
243     return ret;
244 }
245
246 static inline WCHAR *struprW( WCHAR *str )
247 {
248     WCHAR *ret = str;
249     while ((*str = toupperW(*str))) str++;
250     return ret;
251 }
252
253 static inline long int atolW( const WCHAR *str )
254 {
255     return strtolW( str, (WCHAR **)0, 10 );
256 }
257
258 static inline int atoiW( const WCHAR *str )
259 {
260     return (int)atolW( str );
261 }
262
263 #endif  /* __WINE_UNICODE_H */