cadf157693304608356c94d78f17caf6d852fca2
[debian/tar] / gnu / mbchar.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Multibyte character data type.
4    Copyright (C) 2001, 2005-2007, 2009-2013 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program 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
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Bruno Haible <bruno@clisp.org>.  */
20
21 /* A multibyte character is a short subsequence of a char* string,
22    representing a single wide character.
23
24    We use multibyte characters instead of wide characters because of
25    the following goals:
26    1) correct multibyte handling, i.e. operate according to the LC_CTYPE
27       locale,
28    2) ease of maintenance, i.e. the maintainer needs not know all details
29       of the ISO C 99 standard,
30    3) don't fail grossly if the input is not in the encoding set by the
31       locale, because often different encodings are in use in the same
32       countries (ISO-8859-1/UTF-8, EUC-JP/Shift_JIS, ...),
33    4) fast in the case of ASCII characters,
34    5) portability, i.e. don't make unportable assumptions about wchar_t.
35
36    Multibyte characters are only accessed through the mb* macros.
37
38    mb_ptr (mbc)
39      return a pointer to the beginning of the multibyte sequence.
40
41    mb_len (mbc)
42      returns the number of bytes occupied by the multibyte sequence.
43      Always > 0.
44
45    mb_iseq (mbc, sc)
46      returns true if mbc is the standard ASCII character sc.
47
48    mb_isnul (mbc)
49      returns true if mbc is the nul character.
50
51    mb_cmp (mbc1, mbc2)
52      returns a positive, zero, or negative value depending on whether mbc1
53      sorts after, same or before mbc2.
54
55    mb_casecmp (mbc1, mbc2)
56      returns a positive, zero, or negative value depending on whether mbc1
57      sorts after, same or before mbc2, modulo upper/lowercase conversion.
58
59    mb_equal (mbc1, mbc2)
60      returns true if mbc1 and mbc2 are equal.
61
62    mb_caseequal (mbc1, mbc2)
63      returns true if mbc1 and mbc2 are equal modulo upper/lowercase conversion.
64
65    mb_isalnum (mbc)
66      returns true if mbc is alphanumeric.
67
68    mb_isalpha (mbc)
69      returns true if mbc is alphabetic.
70
71    mb_isascii(mbc)
72      returns true if mbc is plain ASCII.
73
74    mb_isblank (mbc)
75      returns true if mbc is a blank.
76
77    mb_iscntrl (mbc)
78      returns true if mbc is a control character.
79
80    mb_isdigit (mbc)
81      returns true if mbc is a decimal digit.
82
83    mb_isgraph (mbc)
84      returns true if mbc is a graphic character.
85
86    mb_islower (mbc)
87      returns true if mbc is lowercase.
88
89    mb_isprint (mbc)
90      returns true if mbc is a printable character.
91
92    mb_ispunct (mbc)
93      returns true if mbc is a punctuation character.
94
95    mb_isspace (mbc)
96      returns true if mbc is a space character.
97
98    mb_isupper (mbc)
99      returns true if mbc is uppercase.
100
101    mb_isxdigit (mbc)
102      returns true if mbc is a hexadecimal digit.
103
104    mb_width (mbc)
105      returns the number of columns on the output device occupied by mbc.
106      Always >= 0.
107
108    mb_putc (mbc, stream)
109      outputs mbc on stream, a byte oriented FILE stream opened for output.
110
111    mb_setascii (&mbc, sc)
112      assigns the standard ASCII character sc to mbc.
113
114    mb_copy (&destmbc, &srcmbc)
115      copies srcmbc to destmbc.
116
117    Here are the function prototypes of the macros.
118
119    extern const char *  mb_ptr (const mbchar_t mbc);
120    extern size_t        mb_len (const mbchar_t mbc);
121    extern bool          mb_iseq (const mbchar_t mbc, char sc);
122    extern bool          mb_isnul (const mbchar_t mbc);
123    extern int           mb_cmp (const mbchar_t mbc1, const mbchar_t mbc2);
124    extern int           mb_casecmp (const mbchar_t mbc1, const mbchar_t mbc2);
125    extern bool          mb_equal (const mbchar_t mbc1, const mbchar_t mbc2);
126    extern bool          mb_caseequal (const mbchar_t mbc1, const mbchar_t mbc2);
127    extern bool          mb_isalnum (const mbchar_t mbc);
128    extern bool          mb_isalpha (const mbchar_t mbc);
129    extern bool          mb_isascii (const mbchar_t mbc);
130    extern bool          mb_isblank (const mbchar_t mbc);
131    extern bool          mb_iscntrl (const mbchar_t mbc);
132    extern bool          mb_isdigit (const mbchar_t mbc);
133    extern bool          mb_isgraph (const mbchar_t mbc);
134    extern bool          mb_islower (const mbchar_t mbc);
135    extern bool          mb_isprint (const mbchar_t mbc);
136    extern bool          mb_ispunct (const mbchar_t mbc);
137    extern bool          mb_isspace (const mbchar_t mbc);
138    extern bool          mb_isupper (const mbchar_t mbc);
139    extern bool          mb_isxdigit (const mbchar_t mbc);
140    extern int           mb_width (const mbchar_t mbc);
141    extern void          mb_putc (const mbchar_t mbc, FILE *stream);
142    extern void          mb_setascii (mbchar_t *new, char sc);
143    extern void          mb_copy (mbchar_t *new, const mbchar_t *old);
144  */
145
146 #ifndef _MBCHAR_H
147 #define _MBCHAR_H 1
148
149 #include <stdbool.h>
150 #include <string.h>
151
152 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
153    <wchar.h>.
154    BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
155    <wchar.h>.  */
156 #include <stdio.h>
157 #include <time.h>
158 #include <wchar.h>
159 #include <wctype.h>
160
161 _GL_INLINE_HEADER_BEGIN
162 #ifndef MBCHAR_INLINE
163 # define MBCHAR_INLINE _GL_INLINE
164 #endif
165
166 #define MBCHAR_BUF_SIZE 24
167
168 struct mbchar
169 {
170   const char *ptr;      /* pointer to current character */
171   size_t bytes;         /* number of bytes of current character, > 0 */
172   bool wc_valid;        /* true if wc is a valid wide character */
173   wchar_t wc;           /* if wc_valid: the current character */
174   char buf[MBCHAR_BUF_SIZE]; /* room for the bytes, used for file input only */
175 };
176
177 /* EOF (not a real character) is represented with bytes = 0 and
178    wc_valid = false.  */
179
180 typedef struct mbchar mbchar_t;
181
182 /* Access the current character.  */
183 #define mb_ptr(mbc) ((mbc).ptr)
184 #define mb_len(mbc) ((mbc).bytes)
185
186 /* Comparison of characters.  */
187 #define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc))
188 #define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0)
189 #define mb_cmp(mbc1, mbc2) \
190   ((mbc1).wc_valid                                                      \
191    ? ((mbc2).wc_valid                                                   \
192       ? (int) (mbc1).wc - (int) (mbc2).wc                               \
193       : -1)                                                             \
194    : ((mbc2).wc_valid                                                   \
195       ? 1                                                               \
196       : (mbc1).bytes == (mbc2).bytes                                    \
197         ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                 \
198         : (mbc1).bytes < (mbc2).bytes                                   \
199           ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
200           : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
201 #define mb_casecmp(mbc1, mbc2) \
202   ((mbc1).wc_valid                                                      \
203    ? ((mbc2).wc_valid                                                   \
204       ? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc)         \
205       : -1)                                                             \
206    : ((mbc2).wc_valid                                                   \
207       ? 1                                                               \
208       : (mbc1).bytes == (mbc2).bytes                                    \
209         ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                 \
210         : (mbc1).bytes < (mbc2).bytes                                   \
211           ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
212           : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
213 #define mb_equal(mbc1, mbc2) \
214   ((mbc1).wc_valid && (mbc2).wc_valid                                   \
215    ? (mbc1).wc == (mbc2).wc                                             \
216    : (mbc1).bytes == (mbc2).bytes                                       \
217      && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
218 #define mb_caseequal(mbc1, mbc2) \
219   ((mbc1).wc_valid && (mbc2).wc_valid                                   \
220    ? towlower ((mbc1).wc) == towlower ((mbc2).wc)                       \
221    : (mbc1).bytes == (mbc2).bytes                                       \
222      && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
223
224 /* <ctype.h>, <wctype.h> classification.  */
225 #define mb_isascii(mbc) \
226   ((mbc).wc_valid && (mbc).wc >= 0 && (mbc).wc <= 127)
227 #define mb_isalnum(mbc) ((mbc).wc_valid && iswalnum ((mbc).wc))
228 #define mb_isalpha(mbc) ((mbc).wc_valid && iswalpha ((mbc).wc))
229 #define mb_isblank(mbc) ((mbc).wc_valid && iswblank ((mbc).wc))
230 #define mb_iscntrl(mbc) ((mbc).wc_valid && iswcntrl ((mbc).wc))
231 #define mb_isdigit(mbc) ((mbc).wc_valid && iswdigit ((mbc).wc))
232 #define mb_isgraph(mbc) ((mbc).wc_valid && iswgraph ((mbc).wc))
233 #define mb_islower(mbc) ((mbc).wc_valid && iswlower ((mbc).wc))
234 #define mb_isprint(mbc) ((mbc).wc_valid && iswprint ((mbc).wc))
235 #define mb_ispunct(mbc) ((mbc).wc_valid && iswpunct ((mbc).wc))
236 #define mb_isspace(mbc) ((mbc).wc_valid && iswspace ((mbc).wc))
237 #define mb_isupper(mbc) ((mbc).wc_valid && iswupper ((mbc).wc))
238 #define mb_isxdigit(mbc) ((mbc).wc_valid && iswxdigit ((mbc).wc))
239
240 /* Extra <wchar.h> function.  */
241
242 /* Unprintable characters appear as a small box of width 1.  */
243 #define MB_UNPRINTABLE_WIDTH 1
244
245 MBCHAR_INLINE int
246 mb_width_aux (wint_t wc)
247 {
248   int w = wcwidth (wc);
249   /* For unprintable characters, arbitrarily return 0 for control characters
250      and MB_UNPRINTABLE_WIDTH otherwise.  */
251   return (w >= 0 ? w : iswcntrl (wc) ? 0 : MB_UNPRINTABLE_WIDTH);
252 }
253
254 #define mb_width(mbc) \
255   ((mbc).wc_valid ? mb_width_aux ((mbc).wc) : MB_UNPRINTABLE_WIDTH)
256
257 /* Output.  */
258 #define mb_putc(mbc, stream)  fwrite ((mbc).ptr, 1, (mbc).bytes, (stream))
259
260 /* Assignment.  */
261 #define mb_setascii(mbc, sc) \
262   ((mbc)->ptr = (mbc)->buf, (mbc)->bytes = 1, (mbc)->wc_valid = 1, \
263    (mbc)->wc = (mbc)->buf[0] = (sc))
264
265 /* Copying a character.  */
266 MBCHAR_INLINE void
267 mb_copy (mbchar_t *new_mbc, const mbchar_t *old_mbc)
268 {
269   if (old_mbc->ptr == &old_mbc->buf[0])
270     {
271       memcpy (&new_mbc->buf[0], &old_mbc->buf[0], old_mbc->bytes);
272       new_mbc->ptr = &new_mbc->buf[0];
273     }
274   else
275     new_mbc->ptr = old_mbc->ptr;
276   new_mbc->bytes = old_mbc->bytes;
277   if ((new_mbc->wc_valid = old_mbc->wc_valid))
278     new_mbc->wc = old_mbc->wc;
279 }
280
281
282 /* is_basic(c) tests whether the single-byte character c is in the
283    ISO C "basic character set".
284    This is a convenience function, and is in this file only to share code
285    between mbiter_multi.h and mbfile_multi.h.  */
286 #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
287     && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
288     && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
289     && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
290     && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
291     && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
292     && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
293     && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
294     && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
295     && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
296     && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
297     && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
298     && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
299     && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
300     && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
301     && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
302     && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
303     && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
304     && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
305     && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
306     && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
307     && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
308     && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)
309 /* The character set is ISO-646, not EBCDIC. */
310 # define IS_BASIC_ASCII 1
311
312 extern const unsigned int is_basic_table[];
313
314 MBCHAR_INLINE bool
315 is_basic (char c)
316 {
317   return (is_basic_table [(unsigned char) c >> 5] >> ((unsigned char) c & 31))
318          & 1;
319 }
320
321 #else
322
323 MBCHAR_INLINE bool
324 is_basic (char c)
325 {
326   switch (c)
327     {
328     case '\t': case '\v': case '\f':
329     case ' ': case '!': case '"': case '#': case '%':
330     case '&': case '\'': case '(': case ')': case '*':
331     case '+': case ',': case '-': case '.': case '/':
332     case '0': case '1': case '2': case '3': case '4':
333     case '5': case '6': case '7': case '8': case '9':
334     case ':': case ';': case '<': case '=': case '>':
335     case '?':
336     case 'A': case 'B': case 'C': case 'D': case 'E':
337     case 'F': case 'G': case 'H': case 'I': case 'J':
338     case 'K': case 'L': case 'M': case 'N': case 'O':
339     case 'P': case 'Q': case 'R': case 'S': case 'T':
340     case 'U': case 'V': case 'W': case 'X': case 'Y':
341     case 'Z':
342     case '[': case '\\': case ']': case '^': case '_':
343     case 'a': case 'b': case 'c': case 'd': case 'e':
344     case 'f': case 'g': case 'h': case 'i': case 'j':
345     case 'k': case 'l': case 'm': case 'n': case 'o':
346     case 'p': case 'q': case 'r': case 's': case 't':
347     case 'u': case 'v': case 'w': case 'x': case 'y':
348     case 'z': case '{': case '|': case '}': case '~':
349       return 1;
350     default:
351       return 0;
352     }
353 }
354
355 #endif
356
357 _GL_INLINE_HEADER_END
358
359 #endif /* _MBCHAR_H */