1 /* Iterating through multibyte strings: macros for multi-byte encodings. 2 Copyright (C) 2001, 2005, 2007, 2009-2013 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 3 of the License, or 7 (at your option) 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, see <http://www.gnu.org/licenses/>. */ 16 17 /* Written by Bruno Haible <bruno@clisp.org>. */ 18 19 /* The macros in this file implement forward iteration through a 20 multi-byte string. 21 22 With these macros, an iteration loop that looks like 23 24 char *iter; 25 for (iter = buf; iter < buf + buflen; iter++) 26 { 27 do_something (*iter); 28 } 29 30 becomes 31 32 mbi_iterator_t iter; 33 for (mbi_init (iter, buf, buflen); mbi_avail (iter); mbi_advance (iter)) 34 { 35 do_something (mbi_cur_ptr (iter), mb_len (mbi_cur (iter))); 36 } 37 38 The benefit of these macros over plain use of mbrtowc is: 39 - Handling of invalid multibyte sequences is possible without 40 making the code more complicated, while still preserving the 41 invalid multibyte sequences. 42 43 mbi_iterator_t 44 is a type usable for variable declarations. 45 46 mbi_init (iter, startptr, length) 47 initializes the iterator, starting at startptr and crossing length bytes. 48 49 mbi_avail (iter) 50 returns true if there are more multibyte characters available before 51 the end of string is reached. In this case, mbi_cur (iter) is 52 initialized to the next multibyte character. 53 54 mbi_advance (iter) 55 advances the iterator by one multibyte character. 56 57 mbi_cur (iter) 58 returns the current multibyte character, of type mbchar_t. All the 59 macros defined in mbchar.h can be used on it. 60 61 mbi_cur_ptr (iter) 62 return a pointer to the beginning of the current multibyte character. 63 64 mbi_reloc (iter, ptrdiff) 65 relocates iterator when the string is moved by ptrdiff bytes. 66 67 mbi_copy (&destiter, &srciter) 68 copies srciter to destiter. 69 70 Here are the function prototypes of the macros. 71 72 extern void mbi_init (mbi_iterator_t iter, 73 const char *startptr, size_t length); 74 extern bool mbi_avail (mbi_iterator_t iter); 75 extern void mbi_advance (mbi_iterator_t iter); 76 extern mbchar_t mbi_cur (mbi_iterator_t iter); 77 extern const char * mbi_cur_ptr (mbi_iterator_t iter); 78 extern void mbi_reloc (mbi_iterator_t iter, ptrdiff_t ptrdiff); 79 extern void mbi_copy (mbi_iterator_t *new, const mbi_iterator_t *old); 80 */ 81 82 #ifndef _MBITER_H 83 #define _MBITER_H 1 84 85 #include <assert.h> 86 #include <stdbool.h> 87 #include <stddef.h> 88 #include <string.h> 89 90 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before 91 <wchar.h>. 92 BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before 93 <wchar.h>. */ 94 #include <stdio.h> 95 #include <time.h> 96 #include <wchar.h> 97 98 #include "mbchar.h" 99 100 _GL_INLINE_HEADER_BEGIN 101 #ifndef MBITER_INLINE 102 # define MBITER_INLINE _GL_INLINE 103 #endif 104 105 struct mbiter_multi 106 { 107 const char *limit; /* pointer to end of string */ 108 bool in_shift; /* true if next byte may not be interpreted as ASCII */ 109 mbstate_t state; /* if in_shift: current shift state */ 110 bool next_done; /* true if mbi_avail has already filled the following */ 111 struct mbchar cur; /* the current character: 112 const char *cur.ptr pointer to current character 113 The following are only valid after mbi_avail. 114 size_t cur.bytes number of bytes of current character 115 bool cur.wc_valid true if wc is a valid wide character 116 wchar_t cur.wc if wc_valid: the current character 117 */ 118 }; 119 120 MBITER_INLINE void 121 mbiter_multi_next (struct mbiter_multi *iter) 122 { 123 if (iter->next_done) 124 return; 125 if (iter->in_shift) 126 goto with_shift; 127 /* Handle most ASCII characters quickly, without calling mbrtowc(). */ 128 if (is_basic (*iter->cur.ptr)) 129 { 130 /* These characters are part of the basic character set. ISO C 99 131 guarantees that their wide character code is identical to their 132 char code. */ 133 iter->cur.bytes = 1; 134 iter->cur.wc = *iter->cur.ptr; 135 iter->cur.wc_valid = true; 136 } 137 else 138 { 139 assert (mbsinit (&iter->state)); 140 iter->in_shift = true; 141 with_shift: 142 iter->cur.bytes = mbrtowc (&iter->cur.wc, iter->cur.ptr, 143 iter->limit - iter->cur.ptr, &iter->state); 144 if (iter->cur.bytes == (size_t) -1) 145 { 146 /* An invalid multibyte sequence was encountered. */ 147 iter->cur.bytes = 1; 148 iter->cur.wc_valid = false; 149 /* Whether to set iter->in_shift = false and reset iter->state 150 or not is not very important; the string is bogus anyway. */ 151 } 152 else if (iter->cur.bytes == (size_t) -2) 153 { 154 /* An incomplete multibyte character at the end. */ 155 iter->cur.bytes = iter->limit - iter->cur.ptr; 156 iter->cur.wc_valid = false; 157 /* Whether to set iter->in_shift = false and reset iter->state 158 or not is not important; the string end is reached anyway. */ 159 } 160 else 161 { 162 if (iter->cur.bytes == 0) 163 { 164 /* A null wide character was encountered. */ 165 iter->cur.bytes = 1; 166 assert (*iter->cur.ptr == '\0'); 167 assert (iter->cur.wc == 0); 168 } 169 iter->cur.wc_valid = true; 170 171 /* When in the initial state, we can go back treating ASCII 172 characters more quickly. */ 173 if (mbsinit (&iter->state)) 174 iter->in_shift = false; 175 } 176 } 177 iter->next_done = true; 178 } 179 180 MBITER_INLINE void 181 mbiter_multi_reloc (struct mbiter_multi *iter, ptrdiff_t ptrdiff) 182 { 183 iter->cur.ptr += ptrdiff; 184 iter->limit += ptrdiff; 185 } 186 187 MBITER_INLINE void 188 mbiter_multi_copy (struct mbiter_multi *new_iter, const struct mbiter_multi *old_iter) 189 { 190 new_iter->limit = old_iter->limit; 191 if ((new_iter->in_shift = old_iter->in_shift)) 192 memcpy (&new_iter->state, &old_iter->state, sizeof (mbstate_t)); 193 else 194 memset (&new_iter->state, 0, sizeof (mbstate_t)); 195 new_iter->next_done = old_iter->next_done; 196 mb_copy (&new_iter->cur, &old_iter->cur); 197 } 198 199 /* Iteration macros. */ 200 typedef struct mbiter_multi mbi_iterator_t; 201 #define mbi_init(iter, startptr, length) \ 202 ((iter).cur.ptr = (startptr), (iter).limit = (iter).cur.ptr + (length), \ 203 (iter).in_shift = false, memset (&(iter).state, '\0', sizeof (mbstate_t)), \ 204 (iter).next_done = false) 205 #define mbi_avail(iter) \ 206 ((iter).cur.ptr < (iter).limit && (mbiter_multi_next (&(iter)), true)) 207 #define mbi_advance(iter) \ 208 ((iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false) 209 210 /* Access to the current character. */ 211 #define mbi_cur(iter) (iter).cur 212 #define mbi_cur_ptr(iter) (iter).cur.ptr 213 214 /* Relocation. */ 215 #define mbi_reloc(iter, ptrdiff) mbiter_multi_reloc (&iter, ptrdiff) 216 217 /* Copying an iterator. */ 218 #define mbi_copy mbiter_multi_copy 219 220 _GL_INLINE_HEADER_END 221 222 #endif /* _MBITER_H */ 223