annotate lib/mbuiter.h @ 9659:5680cf5b5595

md2: clarify comments to say that alignment is not required. * lib/md2.h: Remove warning about alignment in comment. * lib/md2.c (md2_read_ctx, md2_finish_ctx): Doc fix, alignment has never been required.
author Simon Josefsson <simon@josefsson.org>
date Thu, 31 Jan 2008 11:10:36 +0100
parents bbbbbf4cd1c5
children e8d2c6fc33ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Iterating through multibyte strings: macros for multi-byte encodings.
8127
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
2 Copyright (C) 2001, 2005, 2007 Free Software Foundation, Inc.
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8966
diff changeset
4 This program is free software: you can redistribute it and/or modify
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8966
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8966
diff changeset
7 (at your option) any later version.
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8966
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* The macros in this file implement forward iteration through a
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 multi-byte string, without knowing its length a-priori.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 With these macros, an iteration loop that looks like
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 char *iter;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 for (iter = buf; *iter != '\0'; iter++)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 do_something (*iter);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 becomes
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 mbui_iterator_t iter;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 for (mbui_init (iter, buf); mbui_avail (iter); mbui_advance (iter))
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 do_something (mbui_cur_ptr (iter), mb_len (mbui_cur (iter)));
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 The benefit of these macros over plain use of mbrtowc is:
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 - Handling of invalid multibyte sequences is possible without
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 making the code more complicated, while still preserving the
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 invalid multibyte sequences.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 Compared to mbiter.h, the macros here don't need to know the string's
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 length a-priori. The downside is that at each step, the look-ahead
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 that guards against overrunning the terminating '\0' is more expensive.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 The mbui_* macros are therefore suitable when there is a high probability
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 that only the first few multibyte characters need to be inspected.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 Whereas the mbi_* macros are better if usually the iteration runs
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 through the entire string.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 mbui_iterator_t
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 is a type usable for variable declarations.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 mbui_init (iter, startptr)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 initializes the iterator, starting at startptr.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 mbui_avail (iter)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 returns true if there are more multibyte chracters available before
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 the end of string is reached. In this case, mbui_cur (iter) is
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 initialized to the next multibyte chracter.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 mbui_advance (iter)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 advances the iterator by one multibyte character.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 mbui_cur (iter)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 returns the current multibyte character, of type mbchar_t. All the
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 macros defined in mbchar.h can be used on it.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 mbui_cur_ptr (iter)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 return a pointer to the beginning of the current multibyte character.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 mbui_reloc (iter, ptrdiff)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 relocates iterator when the string is moved by ptrdiff bytes.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
8127
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
75 mbui_copy (&destiter, &srciter)
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
76 copies srciter to destiter.
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
77
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 Here are the function prototypes of the macros.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 extern void mbui_init (mbui_iterator_t iter, const char *startptr);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 extern bool mbui_avail (mbui_iterator_t iter);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 extern void mbui_advance (mbui_iterator_t iter);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 extern mbchar_t mbui_cur (mbui_iterator_t iter);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 extern const char * mbui_cur_ptr (mbui_iterator_t iter);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 extern void mbui_reloc (mbui_iterator_t iter, ptrdiff_t ptrdiff);
8127
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
86 extern void mbui_copy (mbui_iterator_t *new, const mbui_iterator_t *old);
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 #ifndef _MBUITER_H
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 #define _MBUITER_H 1
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 #include <assert.h>
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 #include <stdbool.h>
8966
cbc204793cf7 Include <stddef.h>, needed for ptrdiff_t.
Bruno Haible <bruno@clisp.org>
parents: 8127
diff changeset
94 #include <stddef.h>
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 #include <stdlib.h>
8127
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
96 #include <string.h>
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 <wchar.h>.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 <wchar.h>. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 #include <stdio.h>
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 #include <time.h>
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 #include <wchar.h>
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 #include "mbchar.h"
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 #include "strnlen1.h"
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 struct mbuiter_multi
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 bool in_shift; /* true if next byte may not be interpreted as ASCII */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 mbstate_t state; /* if in_shift: current shift state */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 bool next_done; /* true if mbui_avail has already filled the following */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 struct mbchar cur; /* the current character:
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 const char *cur.ptr pointer to current character
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 The following are only valid after mbui_avail.
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 size_t cur.bytes number of bytes of current character
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 bool cur.wc_valid true if wc is a valid wide character
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 wchar_t cur.wc if wc_valid: the current character
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 };
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 static inline void
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 mbuiter_multi_next (struct mbuiter_multi *iter)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 if (iter->next_done)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 return;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 if (iter->in_shift)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 goto with_shift;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 /* Handle most ASCII characters quickly, without calling mbrtowc(). */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 if (is_basic (*iter->cur.ptr))
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 /* These characters are part of the basic character set. ISO C 99
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 guarantees that their wide character code is identical to their
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 char code. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 iter->cur.bytes = 1;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 iter->cur.wc = *iter->cur.ptr;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 iter->cur.wc_valid = true;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 else
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 assert (mbsinit (&iter->state));
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 iter->in_shift = true;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 with_shift:
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 iter->cur.bytes = mbrtowc (&iter->cur.wc, iter->cur.ptr,
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 strnlen1 (iter->cur.ptr, MB_CUR_MAX),
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 &iter->state);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (iter->cur.bytes == (size_t) -1)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 /* An invalid multibyte sequence was encountered. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 iter->cur.bytes = 1;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 iter->cur.wc_valid = false;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 /* Whether to set iter->in_shift = false and reset iter->state
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 or not is not very important; the string is bogus anyway. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 else if (iter->cur.bytes == (size_t) -2)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 /* An incomplete multibyte character at the end. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 iter->cur.bytes = strlen (iter->cur.ptr);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 iter->cur.wc_valid = false;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 /* Whether to set iter->in_shift = false and reset iter->state
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 or not is not important; the string end is reached anyway. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 else
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 if (iter->cur.bytes == 0)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 /* A null wide character was encountered. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 iter->cur.bytes = 1;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 assert (*iter->cur.ptr == '\0');
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 assert (iter->cur.wc == 0);
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 iter->cur.wc_valid = true;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 /* When in the initial state, we can go back treating ASCII
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 characters more quickly. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 if (mbsinit (&iter->state))
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 iter->in_shift = false;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 iter->next_done = true;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 static inline void
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 mbuiter_multi_reloc (struct mbuiter_multi *iter, ptrdiff_t ptrdiff)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 {
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 iter->cur.ptr += ptrdiff;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 }
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189
8127
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
190 static inline void
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
191 mbuiter_multi_copy (struct mbuiter_multi *new_iter, const struct mbuiter_multi *old_iter)
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
192 {
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
193 if ((new_iter->in_shift = old_iter->in_shift))
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
194 memcpy (&new_iter->state, &old_iter->state, sizeof (mbstate_t));
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
195 else
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
196 memset (&new_iter->state, 0, sizeof (mbstate_t));
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
197 new_iter->next_done = old_iter->next_done;
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
198 mb_copy (&new_iter->cur, &old_iter->cur);
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
199 }
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
200
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 /* Iteration macros. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 typedef struct mbuiter_multi mbui_iterator_t;
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 #define mbui_init(iter, startptr) \
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 ((iter).cur.ptr = (startptr), \
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 (iter).in_shift = false, memset (&(iter).state, '\0', sizeof (mbstate_t)), \
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 (iter).next_done = false)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 #define mbui_avail(iter) \
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 (mbuiter_multi_next (&(iter)), !mb_isnul ((iter).cur))
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 #define mbui_advance(iter) \
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 ((iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 /* Access to the current character. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 #define mbui_cur(iter) (iter).cur
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 #define mbui_cur_ptr(iter) (iter).cur.ptr
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 /* Relocation. */
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 #define mbui_reloc(iter, ptrdiff) mbuiter_multi_reloc (&iter, ptrdiff)
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218
8127
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
219 /* Copying an iterator. */
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
220 #define mbui_copy mbuiter_multi_copy
127a096061c8 Support for copying multibyte string iterators.
Bruno Haible <bruno@clisp.org>
parents: 6055
diff changeset
221
6055
a87afe9c4648 New module 'mbuiter'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 #endif /* _MBUITER_H */