In rare cases, it is necessary to copy iterators (including the multibyte state), rather than creating a new iterator.
2007-02-11 Bruno Haible <[EMAIL PROTECTED]> * lib/mbiter.h: Include <string.h>. (mbiter_multi_copy): New function. (mbi_copy): New macro. * lib/mbuiter.h: Include <string.h>. (mbuiter_multi_copy): New function. (mbui_copy): New macro. *** lib/mbiter.h 17 Aug 2005 13:56:26 -0000 1.2 --- lib/mbiter.h 11 Feb 2007 17:11:21 -0000 *************** *** 1,5 **** /* Iterating through multibyte strings: macros for multi-byte encodings. ! Copyright (C) 2001, 2005 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by --- 1,5 ---- /* Iterating through multibyte strings: macros for multi-byte encodings. ! Copyright (C) 2001, 2005, 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by *************** *** 65,70 **** --- 65,73 ---- mbi_reloc (iter, ptrdiff) relocates iterator when the string is moved by ptrdiff bytes. + mbi_copy (&destiter, &srciter) + copies srciter to destiter. + Here are the function prototypes of the macros. extern void mbi_init (mbi_iterator_t iter, *************** *** 74,79 **** --- 77,83 ---- extern mbchar_t mbi_cur (mbi_iterator_t iter); extern const char * mbi_cur_ptr (mbi_iterator_t iter); extern void mbi_reloc (mbi_iterator_t iter, ptrdiff_t ptrdiff); + extern void mbi_copy (mbi_iterator_t *new, const mbi_iterator_t *old); */ #ifndef _MBITER_H *************** *** 81,86 **** --- 85,91 ---- #include <assert.h> #include <stdbool.h> + #include <string.h> /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before <wchar.h>. *************** *** 174,179 **** --- 179,196 ---- iter->limit += ptrdiff; } + static inline void + mbiter_multi_copy (struct mbiter_multi *new_iter, const struct mbiter_multi *old_iter) + { + new_iter->limit = old_iter->limit; + if ((new_iter->in_shift = old_iter->in_shift)) + memcpy (&new_iter->state, &old_iter->state, sizeof (mbstate_t)); + else + memset (&new_iter->state, 0, sizeof (mbstate_t)); + new_iter->next_done = old_iter->next_done; + mb_copy (&new_iter->cur, &old_iter->cur); + } + /* Iteration macros. */ typedef struct mbiter_multi mbi_iterator_t; #define mbi_init(iter, startptr, length) \ *************** *** 192,195 **** --- 209,215 ---- /* Relocation. */ #define mbi_reloc(iter, ptrdiff) mbiter_multi_reloc (&iter, ptrdiff) + /* Copying an iterator. */ + #define mbi_copy mbiter_multi_copy + #endif /* _MBITER_H */ *** lib/mbuiter.h 17 Aug 2005 13:58:34 -0000 1.1 --- lib/mbuiter.h 11 Feb 2007 17:11:21 -0000 *************** *** 1,5 **** /* Iterating through multibyte strings: macros for multi-byte encodings. ! Copyright (C) 2001, 2005 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by --- 1,5 ---- /* Iterating through multibyte strings: macros for multi-byte encodings. ! Copyright (C) 2001, 2005, 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by *************** *** 73,78 **** --- 73,81 ---- mbui_reloc (iter, ptrdiff) relocates iterator when the string is moved by ptrdiff bytes. + mbui_copy (&destiter, &srciter) + copies srciter to destiter. + Here are the function prototypes of the macros. extern void mbui_init (mbui_iterator_t iter, const char *startptr); *************** *** 81,86 **** --- 84,90 ---- extern mbchar_t mbui_cur (mbui_iterator_t iter); extern const char * mbui_cur_ptr (mbui_iterator_t iter); extern void mbui_reloc (mbui_iterator_t iter, ptrdiff_t ptrdiff); + extern void mbui_copy (mbui_iterator_t *new, const mbui_iterator_t *old); */ #ifndef _MBUITER_H *************** *** 89,94 **** --- 93,99 ---- #include <assert.h> #include <stdbool.h> #include <stdlib.h> + #include <string.h> /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before <wchar.h>. *************** *** 182,187 **** --- 187,203 ---- iter->cur.ptr += ptrdiff; } + static inline void + mbuiter_multi_copy (struct mbuiter_multi *new_iter, const struct mbuiter_multi *old_iter) + { + if ((new_iter->in_shift = old_iter->in_shift)) + memcpy (&new_iter->state, &old_iter->state, sizeof (mbstate_t)); + else + memset (&new_iter->state, 0, sizeof (mbstate_t)); + new_iter->next_done = old_iter->next_done; + mb_copy (&new_iter->cur, &old_iter->cur); + } + /* Iteration macros. */ typedef struct mbuiter_multi mbui_iterator_t; #define mbui_init(iter, startptr) \ *************** *** 200,203 **** --- 216,222 ---- /* Relocation. */ #define mbui_reloc(iter, ptrdiff) mbuiter_multi_reloc (&iter, ptrdiff) + /* Copying an iterator. */ + #define mbui_copy mbuiter_multi_copy + #endif /* _MBUITER_H */