~ubuntu-branches/ubuntu/intrepid/parted/intrepid

« back to all changes in this revision

Viewing changes to gnulib/lib/mbiter.h

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2008-06-24 14:31:05 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080624143105-rd7yw67a9qnvh51i
Tags: 1.8.8.git.2008.03.24-7ubuntu1
* Resynchronise with Debian (LP: #237568). Remaining changes:
  - swap-uuid.dpatch: Create UUIDs on new swap partitions.
  - gptsync.dpatch: On Intel Mac systems, write a synced MBR rather than a
    protective MBR.
  - Add -fno-stack-protector on sparc.
  - sparc-new-label.dpatch: Fix sparc disk label generation. This is
    required for LDOM and parallel installations with Solaris 10.
  - loop-partitions.dpatch: Loop devices can only have one partition, so
    don't generate device names such as "/dev/loop0p1".
  - unpartitioned-disks.dpatch: Don't try to call BLKPG ioctls on
    unpartitionable disks (only implemented for loop devices at the
    moment), as they will always fail.
  - When building with gcc-4.3, add -Wno-array-bounds to CFLAGS.
  - Cell partition tables are misdetected as pc98, so disable pc98 support
    on powerpc.
  - array-bounds.dpatch: Backport patch from git to allow building with
    gcc-4.3.

Show diffs side-by-side

added added

removed removed

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