Define fallbacks for missing isw* functions on FreeBSD 4.x.
[gnulib.git] / lib / mbchar.h
1 /* Multibyte character data type.
2    Copyright (C) 2001, 2005-2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>.  */
19
20 /* A multibyte character is a short subsequence of a char* string,
21    representing a single wide character.
22
23    We use multibyte characters instead of wide characters because of
24    the following goals:
25    1) correct multibyte handling, i.e. operate according to the LC_CTYPE
26       locale,
27    2) ease of maintenance, i.e. the maintainer needs not know all details
28       of the ISO C 99 standard,
29    3) don't fail grossly if the input is not in the encoding set by the
30       locale, because often different encodings are in use in the same
31       countries (ISO-8859-1/UTF-8, EUC-JP/Shift_JIS, ...),
32    4) fast in the case of ASCII characters,
33    5) portability, i.e. don't make unportable assumptions about wchar_t.
34
35    Multibyte characters are only accessed through the mb* macros.
36
37    mb_ptr (mbc)
38      return a pointer to the beginning of the multibyte sequence.
39
40    mb_len (mbc)
41      returns the number of bytes occupied by the multibyte sequence.
42      Always > 0.
43
44    mb_iseq (mbc, sc)
45      returns true if mbc is the standard ASCII character sc.
46
47    mb_isnul (mbc)
48      returns true if mbc is the nul character.
49
50    mb_cmp (mbc1, mbc2)
51      returns a positive, zero, or negative value depending on whether mbc1
52      sorts after, same or before mbc2.
53
54    mb_casecmp (mbc1, mbc2)
55      returns a positive, zero, or negative value depending on whether mbc1
56      sorts after, same or before mbc2, modulo upper/lowercase conversion.
57
58    mb_equal (mbc1, mbc2)
59      returns true if mbc1 and mbc2 are equal.
60
61    mb_caseequal (mbc1, mbc2)
62      returns true if mbc1 and mbc2 are equal modulo upper/lowercase conversion.
63
64    mb_isalnum (mbc)
65      returns true if mbc is alphanumeric.
66
67    mb_isalpha (mbc)
68      returns true if mbc is alphabetic.
69
70    mb_isascii(mbc)
71      returns true if mbc is plain ASCII.
72
73    mb_isblank (mbc)
74      returns true if mbc is a blank.
75
76    mb_iscntrl (mbc)
77      returns true if mbc is a control character.
78
79    mb_isdigit (mbc)
80      returns true if mbc is a decimal digit.
81
82    mb_isgraph (mbc)
83      returns true if mbc is a graphic character.
84
85    mb_islower (mbc)
86      returns true if mbc is lowercase.
87
88    mb_isprint (mbc)
89      returns true if mbc is a printable character.
90
91    mb_ispunct (mbc)
92      returns true if mbc is a punctuation character.
93
94    mb_isspace (mbc)
95      returns true if mbc is a space character.
96
97    mb_isupper (mbc)
98      returns true if mbc is uppercase.
99
100    mb_isxdigit (mbc)
101      returns true if mbc is a hexadecimal digit.
102
103    mb_width (mbc)
104      returns the number of columns on the output device occupied by mbc.
105      Always >= 0.
106
107    mb_putc (mbc, stream)
108      outputs mbc on stream, a byte oriented FILE stream opened for output.
109
110    mb_setascii (&mbc, sc)
111      assigns the standard ASCII character sc to mbc.
112
113    mb_copy (&destmbc, &srcmbc)
114      copies srcmbc to destmbc.
115
116    Here are the function prototypes of the macros.
117
118    extern const char *  mb_ptr (const mbchar_t mbc);
119    extern size_t        mb_len (const mbchar_t mbc);
120    extern bool          mb_iseq (const mbchar_t mbc, char sc);
121    extern bool          mb_isnul (const mbchar_t mbc);
122    extern int           mb_cmp (const mbchar_t mbc1, const mbchar_t mbc2);
123    extern int           mb_casecmp (const mbchar_t mbc1, const mbchar_t mbc2);
124    extern bool          mb_equal (const mbchar_t mbc1, const mbchar_t mbc2);
125    extern bool          mb_caseequal (const mbchar_t mbc1, const mbchar_t mbc2);
126    extern bool          mb_isalnum (const mbchar_t mbc);
127    extern bool          mb_isalpha (const mbchar_t mbc);
128    extern bool          mb_isascii (const mbchar_t mbc);
129    extern bool          mb_isblank (const mbchar_t mbc);
130    extern bool          mb_iscntrl (const mbchar_t mbc);
131    extern bool          mb_isdigit (const mbchar_t mbc);
132    extern bool          mb_isgraph (const mbchar_t mbc);
133    extern bool          mb_islower (const mbchar_t mbc);
134    extern bool          mb_isprint (const mbchar_t mbc);
135    extern bool          mb_ispunct (const mbchar_t mbc);
136    extern bool          mb_isspace (const mbchar_t mbc);
137    extern bool          mb_isupper (const mbchar_t mbc);
138    extern bool          mb_isxdigit (const mbchar_t mbc);
139    extern int           mb_width (const mbchar_t mbc);
140    extern void          mb_putc (const mbchar_t mbc, FILE *stream);
141    extern void          mb_setascii (mbchar_t *new, char sc);
142    extern void          mb_copy (mbchar_t *new, const mbchar_t *old);
143  */
144
145 #ifndef _MBCHAR_H
146 #define _MBCHAR_H 1
147
148 #include <stdbool.h>
149 #include <string.h>
150
151 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
152    <wchar.h>.
153    BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
154    <wchar.h>.  */
155 #include <stdio.h>
156 #include <time.h>
157 #include <wchar.h>
158
159 #include <wctype.h>
160 /* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
161    Assume all 12 functions are implemented the same way, or not at all.  */
162 #if !defined iswalnum && !HAVE_ISWCNTRL
163 ststic inline int
164 iswalnum (wint_t wc)
165 {
166   return (wc >= 0 && wc < 128
167           ? (wc >= '0' && wc <= '9') || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')
168           : 0);
169 }
170 #endif
171 #if !defined iswalpha && !HAVE_ISWCNTRL
172 ststic inline int
173 iswalpha (wint_t wc)
174 {
175   return (wc >= 0 && wc < 128
176           ? (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'
177           : 0);
178 }
179 #endif
180 #if !defined iswblank && !HAVE_ISWCNTRL
181 ststic inline int
182 iswblank (wint_t wc)
183 {
184   return (wc >= 0 && wc < 128
185           ? wc == ' ' || wc == '\t'
186           : 0);
187 }
188 #endif
189 #if !defined iswcntrl && !HAVE_ISWCNTRL
190 ststic inline int
191 iswcntrl (wint_t wc)
192 {
193   return (wc >= 0 && wc < 128
194           ? (wc & ~0x1f) == 0 || wc == 0x7f
195           : 0);
196 }
197 #endif
198 #if !defined iswdigit && !HAVE_ISWCNTRL
199 ststic inline int
200 iswdigit (wint_t wc)
201 {
202   return (wc >= '0' && wc <= '9');
203 }
204 #endif
205 #if !defined iswgraph && !HAVE_ISWCNTRL
206 ststic inline int
207 iswgraph (wint_t wc)
208 {
209   return (wc >= 0 && wc < 128
210           ? wc >= '!' && wc <= '~'
211           : 1);
212 }
213 #endif
214 #if !defined iswlower && !HAVE_ISWCNTRL
215 ststic inline int
216 iswlower (wint_t wc)
217 {
218   return (wc >= 0 && wc < 128
219           ? wc >= 'a' && wc <= 'z'
220           : 0);
221 }
222 #endif
223 #if !defined iswprint && !HAVE_ISWCNTRL
224 ststic inline int
225 iswprint (wint_t wc)
226 {
227   return (wc >= 0 && wc < 128
228           ? wc >= ' ' && wc <= '~'
229           : 1);
230 }
231 #endif
232 #if !defined iswpunct && !HAVE_ISWCNTRL
233 ststic inline int
234 iswpunct (wint_t wc)
235 {
236   return (wc >= 0 && wc < 128
237           ? wc >= '!' && wc <= '~'
238             && !((wc >= '0' && wc <= '9')
239                  || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'))
240           : 1);
241 }
242 #endif
243 #if !defined iswspace && !HAVE_ISWCNTRL
244 ststic inline int
245 iswspace (wint_t wc)
246 {
247   return (wc >= 0 && wc < 128
248           ? wc == ' ' || wc == '\t'
249             || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r'
250           : 0);
251 }
252 #endif
253 #if !defined iswupper && !HAVE_ISWCNTRL
254 ststic inline int
255 iswupper (wint_t wc)
256 {
257   return (wc >= 0 && wc < 128
258           ? wc >= 'A' && wc <= 'Z'
259           : 0);
260 }
261 #endif
262 #if !defined iswxdigit && !HAVE_ISWCNTRL
263 ststic inline int
264 iswxdigit (wint_t wc)
265 {
266   return (wc >= '0' && wc <= '9') || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F');
267 }
268 #endif
269
270 #include "wcwidth.h"
271
272 #define MBCHAR_BUF_SIZE 24
273
274 struct mbchar
275 {
276   const char *ptr;      /* pointer to current character */
277   size_t bytes;         /* number of bytes of current character, > 0 */
278   bool wc_valid;        /* true if wc is a valid wide character */
279   wchar_t wc;           /* if wc_valid: the current character */
280   char buf[MBCHAR_BUF_SIZE]; /* room for the bytes, used for file input only */
281 };
282
283 /* EOF (not a real character) is represented with bytes = 0 and
284    wc_valid = false.  */
285
286 typedef struct mbchar mbchar_t;
287
288 /* Access the current character.  */
289 #define mb_ptr(mbc) ((mbc).ptr)
290 #define mb_len(mbc) ((mbc).bytes)
291
292 /* Comparison of characters.  */
293 #define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc))
294 #define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0)
295 #define mb_cmp(mbc1, mbc2) \
296   ((mbc1).wc_valid                                                      \
297    ? ((mbc2).wc_valid                                                   \
298       ? (int) (mbc1).wc - (int) (mbc2).wc                               \
299       : -1)                                                             \
300    : ((mbc2).wc_valid                                                   \
301       ? 1                                                               \
302       : (mbc1).bytes == (mbc2).bytes                                    \
303         ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                 \
304         : (mbc1).bytes < (mbc2).bytes                                   \
305           ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
306           : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
307 #define mb_casecmp(mbc1, mbc2) \
308   ((mbc1).wc_valid                                                      \
309    ? ((mbc2).wc_valid                                                   \
310       ? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc)         \
311       : -1)                                                             \
312    : ((mbc2).wc_valid                                                   \
313       ? 1                                                               \
314       : (mbc1).bytes == (mbc2).bytes                                    \
315         ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                 \
316         : (mbc1).bytes < (mbc2).bytes                                   \
317           ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
318           : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
319 #define mb_equal(mbc1, mbc2) \
320   ((mbc1).wc_valid && (mbc2).wc_valid                                   \
321    ? (mbc1).wc == (mbc2).wc                                             \
322    : (mbc1).bytes == (mbc2).bytes                                       \
323      && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
324 #define mb_caseequal(mbc1, mbc2) \
325   ((mbc1).wc_valid && (mbc2).wc_valid                                   \
326    ? towlower ((mbc1).wc) == towlower ((mbc2).wc)                       \
327    : (mbc1).bytes == (mbc2).bytes                                       \
328      && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
329
330 /* <ctype.h>, <wctype.h> classification.  */
331 #define mb_isascii(mbc) \
332   ((mbc).wc_valid && (mbc).wc >= 0 && (mbc).wc <= 127)
333 #define mb_isalnum(mbc) ((mbc).wc_valid && iswalnum ((mbc).wc))
334 #define mb_isalpha(mbc) ((mbc).wc_valid && iswalpha ((mbc).wc))
335 #define mb_isblank(mbc) ((mbc).wc_valid && iswblank ((mbc).wc))
336 #define mb_iscntrl(mbc) ((mbc).wc_valid && iswcntrl ((mbc).wc))
337 #define mb_isdigit(mbc) ((mbc).wc_valid && iswdigit ((mbc).wc))
338 #define mb_isgraph(mbc) ((mbc).wc_valid && iswgraph ((mbc).wc))
339 #define mb_islower(mbc) ((mbc).wc_valid && iswlower ((mbc).wc))
340 #define mb_isprint(mbc) ((mbc).wc_valid && iswprint ((mbc).wc))
341 #define mb_ispunct(mbc) ((mbc).wc_valid && iswpunct ((mbc).wc))
342 #define mb_isspace(mbc) ((mbc).wc_valid && iswspace ((mbc).wc))
343 #define mb_isupper(mbc) ((mbc).wc_valid && iswupper ((mbc).wc))
344 #define mb_isxdigit(mbc) ((mbc).wc_valid && iswxdigit ((mbc).wc))
345
346 /* Extra <wchar.h> function.  */
347
348 /* Unprintable characters appear as a small box of width 1.  */
349 #define MB_UNPRINTABLE_WIDTH 1
350
351 static inline int
352 mb_width_aux (wint_t wc)
353 {
354   int w = wcwidth (wc);
355   /* For unprintable characters, arbitrarily return 0 for control characters
356      and MB_UNPRINTABLE_WIDTH otherwise.  */
357   return (w >= 0 ? w : iswcntrl (wc) ? 0 : MB_UNPRINTABLE_WIDTH);
358 }
359
360 #define mb_width(mbc) \
361   ((mbc).wc_valid ? mb_width_aux ((mbc).wc) : MB_UNPRINTABLE_WIDTH)
362
363 /* Output.  */
364 #define mb_putc(mbc, stream)  fwrite ((mbc).ptr, 1, (mbc).bytes, (stream))
365
366 /* Assignment.  */
367 #define mb_setascii(mbc, sc) \
368   ((mbc)->ptr = (mbc)->buf, (mbc)->bytes = 1, (mbc)->wc_valid = 1, \
369    (mbc)->wc = (mbc)->buf[0] = (sc))
370
371 /* Copying a character.  */
372 static inline void
373 mb_copy (mbchar_t *new, const mbchar_t *old)
374 {
375   if (old->ptr == &old->buf[0])
376     {
377       memcpy (&new->buf[0], &old->buf[0], old->bytes);
378       new->ptr = &new->buf[0];
379     }
380   else
381     new->ptr = old->ptr;
382   new->bytes = old->bytes;
383   if ((new->wc_valid = old->wc_valid))
384     new->wc = old->wc;
385 }
386
387
388 /* is_basic(c) tests whether the single-byte character c is in the
389    ISO C "basic character set".
390    This is a convenience function, and is in this file only to share code
391    between mbiter_multi.h and mbfile_multi.h.  */
392 #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
393     && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
394     && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
395     && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
396     && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
397     && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
398     && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
399     && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
400     && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
401     && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
402     && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
403     && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
404     && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
405     && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
406     && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
407     && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
408     && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
409     && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
410     && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
411     && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
412     && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
413     && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
414     && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)
415 /* The character set is ISO-646, not EBCDIC. */
416 # define IS_BASIC_ASCII 1
417
418 extern unsigned int is_basic_table[];
419
420 static inline bool
421 is_basic (char c)
422 {
423   return (is_basic_table [(unsigned char) c >> 5] >> ((unsigned char) c & 31))
424          & 1;
425 }
426
427 #else
428
429 static inline bool
430 is_basic (char c)
431 {
432   switch (c)
433     {
434     case '\t': case '\v': case '\f':
435     case ' ': case '!': case '"': case '#': case '%':
436     case '&': case '\'': case '(': case ')': case '*':
437     case '+': case ',': case '-': case '.': case '/':
438     case '0': case '1': case '2': case '3': case '4':
439     case '5': case '6': case '7': case '8': case '9':
440     case ':': case ';': case '<': case '=': case '>':
441     case '?':
442     case 'A': case 'B': case 'C': case 'D': case 'E':
443     case 'F': case 'G': case 'H': case 'I': case 'J':
444     case 'K': case 'L': case 'M': case 'N': case 'O':
445     case 'P': case 'Q': case 'R': case 'S': case 'T':
446     case 'U': case 'V': case 'W': case 'X': case 'Y':
447     case 'Z':
448     case '[': case '\\': case ']': case '^': case '_':
449     case 'a': case 'b': case 'c': case 'd': case 'e':
450     case 'f': case 'g': case 'h': case 'i': case 'j':
451     case 'k': case 'l': case 'm': case 'n': case 'o':
452     case 'p': case 'q': case 'r': case 's': case 't':
453     case 'u': case 'v': case 'w': case 'x': case 'y':
454     case 'z': case '{': case '|': case '}': case '~':
455       return 1;
456     default:
457       return 0;
458     }
459 }
460
461 #endif
462
463 #endif /* _MBCHAR_H */