Import upstream version 1.28
[debian/tar] / gnu / mbuiter.h
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Iterating through multibyte strings: macros for multi-byte encodings.
4    Copyright (C) 2001, 2005, 2007, 2009-2014 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 /* The macros in this file implement forward iteration through a
22    multi-byte string, without knowing its length a-priori.
23
24    With these macros, an iteration loop that looks like
25
26       char *iter;
27       for (iter = buf; *iter != '\0'; iter++)
28         {
29           do_something (*iter);
30         }
31
32    becomes
33
34       mbui_iterator_t iter;
35       for (mbui_init (iter, buf); mbui_avail (iter); mbui_advance (iter))
36         {
37           do_something (mbui_cur_ptr (iter), mb_len (mbui_cur (iter)));
38         }
39
40    The benefit of these macros over plain use of mbrtowc is:
41    - Handling of invalid multibyte sequences is possible without
42      making the code more complicated, while still preserving the
43      invalid multibyte sequences.
44
45    Compared to mbiter.h, the macros here don't need to know the string's
46    length a-priori.  The downside is that at each step, the look-ahead
47    that guards against overrunning the terminating '\0' is more expensive.
48    The mbui_* macros are therefore suitable when there is a high probability
49    that only the first few multibyte characters need to be inspected.
50    Whereas the mbi_* macros are better if usually the iteration runs
51    through the entire string.
52
53    mbui_iterator_t
54      is a type usable for variable declarations.
55
56    mbui_init (iter, startptr)
57      initializes the iterator, starting at startptr.
58
59    mbui_avail (iter)
60      returns true if there are more multibyte characters available before
61      the end of string is reached. In this case, mbui_cur (iter) is
62      initialized to the next multibyte character.
63
64    mbui_advance (iter)
65      advances the iterator by one multibyte character.
66
67    mbui_cur (iter)
68      returns the current multibyte character, of type mbchar_t.  All the
69      macros defined in mbchar.h can be used on it.
70
71    mbui_cur_ptr (iter)
72      return a pointer to the beginning of the current multibyte character.
73
74    mbui_reloc (iter, ptrdiff)
75      relocates iterator when the string is moved by ptrdiff bytes.
76
77    mbui_copy (&destiter, &srciter)
78      copies srciter to destiter.
79
80    Here are the function prototypes of the macros.
81
82    extern void          mbui_init (mbui_iterator_t iter, const char *startptr);
83    extern bool          mbui_avail (mbui_iterator_t iter);
84    extern void          mbui_advance (mbui_iterator_t iter);
85    extern mbchar_t      mbui_cur (mbui_iterator_t iter);
86    extern const char *  mbui_cur_ptr (mbui_iterator_t iter);
87    extern void          mbui_reloc (mbui_iterator_t iter, ptrdiff_t ptrdiff);
88    extern void          mbui_copy (mbui_iterator_t *new, const mbui_iterator_t *old);
89  */
90
91 #ifndef _MBUITER_H
92 #define _MBUITER_H 1
93
94 #include <assert.h>
95 #include <stdbool.h>
96 #include <stddef.h>
97 #include <stdlib.h>
98 #include <string.h>
99
100 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
101    <wchar.h>.
102    BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
103    <wchar.h>.  */
104 #include <stdio.h>
105 #include <time.h>
106 #include <wchar.h>
107
108 #include "mbchar.h"
109 #include "strnlen1.h"
110
111 #ifndef _GL_INLINE_HEADER_BEGIN
112  #error "Please include config.h first."
113 #endif
114 _GL_INLINE_HEADER_BEGIN
115 #ifndef MBUITER_INLINE
116 # define MBUITER_INLINE _GL_INLINE
117 #endif
118
119 struct mbuiter_multi
120 {
121   bool in_shift;        /* true if next byte may not be interpreted as ASCII */
122   mbstate_t state;      /* if in_shift: current shift state */
123   bool next_done;       /* true if mbui_avail has already filled the following */
124   struct mbchar cur;    /* the current character:
125         const char *cur.ptr             pointer to current character
126         The following are only valid after mbui_avail.
127         size_t cur.bytes                number of bytes of current character
128         bool cur.wc_valid               true if wc is a valid wide character
129         wchar_t cur.wc                  if wc_valid: the current character
130         */
131 };
132
133 MBUITER_INLINE void
134 mbuiter_multi_next (struct mbuiter_multi *iter)
135 {
136   if (iter->next_done)
137     return;
138   if (iter->in_shift)
139     goto with_shift;
140   /* Handle most ASCII characters quickly, without calling mbrtowc().  */
141   if (is_basic (*iter->cur.ptr))
142     {
143       /* These characters are part of the basic character set.  ISO C 99
144          guarantees that their wide character code is identical to their
145          char code.  */
146       iter->cur.bytes = 1;
147       iter->cur.wc = *iter->cur.ptr;
148       iter->cur.wc_valid = true;
149     }
150   else
151     {
152       assert (mbsinit (&iter->state));
153       iter->in_shift = true;
154     with_shift:
155       iter->cur.bytes = mbrtowc (&iter->cur.wc, iter->cur.ptr,
156                                  strnlen1 (iter->cur.ptr, MB_CUR_MAX),
157                                  &iter->state);
158       if (iter->cur.bytes == (size_t) -1)
159         {
160           /* An invalid multibyte sequence was encountered.  */
161           iter->cur.bytes = 1;
162           iter->cur.wc_valid = false;
163           /* Whether to set iter->in_shift = false and reset iter->state
164              or not is not very important; the string is bogus anyway.  */
165         }
166       else if (iter->cur.bytes == (size_t) -2)
167         {
168           /* An incomplete multibyte character at the end.  */
169           iter->cur.bytes = strlen (iter->cur.ptr);
170           iter->cur.wc_valid = false;
171           /* Whether to set iter->in_shift = false and reset iter->state
172              or not is not important; the string end is reached anyway.  */
173         }
174       else
175         {
176           if (iter->cur.bytes == 0)
177             {
178               /* A null wide character was encountered.  */
179               iter->cur.bytes = 1;
180               assert (*iter->cur.ptr == '\0');
181               assert (iter->cur.wc == 0);
182             }
183           iter->cur.wc_valid = true;
184
185           /* When in the initial state, we can go back treating ASCII
186              characters more quickly.  */
187           if (mbsinit (&iter->state))
188             iter->in_shift = false;
189         }
190     }
191   iter->next_done = true;
192 }
193
194 MBUITER_INLINE void
195 mbuiter_multi_reloc (struct mbuiter_multi *iter, ptrdiff_t ptrdiff)
196 {
197   iter->cur.ptr += ptrdiff;
198 }
199
200 MBUITER_INLINE void
201 mbuiter_multi_copy (struct mbuiter_multi *new_iter, const struct mbuiter_multi *old_iter)
202 {
203   if ((new_iter->in_shift = old_iter->in_shift))
204     memcpy (&new_iter->state, &old_iter->state, sizeof (mbstate_t));
205   else
206     memset (&new_iter->state, 0, sizeof (mbstate_t));
207   new_iter->next_done = old_iter->next_done;
208   mb_copy (&new_iter->cur, &old_iter->cur);
209 }
210
211 /* Iteration macros.  */
212 typedef struct mbuiter_multi mbui_iterator_t;
213 #define mbui_init(iter, startptr) \
214   ((iter).cur.ptr = (startptr), \
215    (iter).in_shift = false, memset (&(iter).state, '\0', sizeof (mbstate_t)), \
216    (iter).next_done = false)
217 #define mbui_avail(iter) \
218   (mbuiter_multi_next (&(iter)), !mb_isnul ((iter).cur))
219 #define mbui_advance(iter) \
220   ((iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false)
221
222 /* Access to the current character.  */
223 #define mbui_cur(iter) (iter).cur
224 #define mbui_cur_ptr(iter) (iter).cur.ptr
225
226 /* Relocation.  */
227 #define mbui_reloc(iter, ptrdiff) mbuiter_multi_reloc (&iter, ptrdiff)
228
229 /* Copying an iterator.  */
230 #define mbui_copy mbuiter_multi_copy
231
232 _GL_INLINE_HEADER_END
233
234 #endif /* _MBUITER_H */