(root)/
gettext-0.22.4/
gettext-runtime/
gnulib-lib/
mbuiter.h
       1  /* Iterating through multibyte strings: macros for multi-byte encodings.
       2     Copyright (C) 2001, 2005, 2007, 2009-2023 Free Software Foundation, Inc.
       3  
       4     This file is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU Lesser General Public License as
       6     published by the Free Software Foundation, either version 3 of the
       7     License, or (at your option) any later version.
       8  
       9     This file 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 Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public License
      15     along with this program.  If not, see <https://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, without knowing its length a-priori.
      21  
      22     With these macros, an iteration loop that looks like
      23  
      24        char *iter;
      25        for (iter = buf; *iter != '\0'; iter++)
      26          {
      27            do_something (*iter);
      28          }
      29  
      30     becomes
      31  
      32        mbui_iterator_t iter;
      33        for (mbui_init (iter, buf); mbui_avail (iter); mbui_advance (iter))
      34          {
      35            do_something (mbui_cur_ptr (iter), mb_len (mbui_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     Compared to mbiter.h, the macros here don't need to know the string's
      44     length a-priori.  The downside is that at each step, the look-ahead
      45     that guards against overrunning the terminating '\0' is more expensive.
      46     The mbui_* macros are therefore suitable when there is a high probability
      47     that only the first few multibyte characters need to be inspected.
      48     Whereas the mbi_* macros are better if usually the iteration runs
      49     through the entire string.
      50  
      51     mbui_iterator_t
      52       is a type usable for variable declarations.
      53  
      54     mbui_init (iter, startptr)
      55       initializes the iterator, starting at startptr.
      56  
      57     mbui_avail (iter)
      58       returns true if there are more multibyte characters available before
      59       the end of string is reached. In this case, mbui_cur (iter) is
      60       initialized to the next multibyte character.
      61  
      62     mbui_advance (iter)
      63       advances the iterator by one multibyte character.
      64  
      65     mbui_cur (iter)
      66       returns the current multibyte character, of type mbchar_t.  All the
      67       macros defined in mbchar.h can be used on it.
      68  
      69     mbui_cur_ptr (iter)
      70       return a pointer to the beginning of the current multibyte character.
      71  
      72     mbui_reloc (iter, ptrdiff)
      73       relocates iterator when the string is moved by ptrdiff bytes.
      74  
      75     mbui_copy (&destiter, &srciter)
      76       copies srciter to destiter.
      77  
      78     Here are the function prototypes of the macros.
      79  
      80     extern void          mbui_init (mbui_iterator_t iter, const char *startptr);
      81     extern bool          mbui_avail (mbui_iterator_t iter);
      82     extern void          mbui_advance (mbui_iterator_t iter);
      83     extern mbchar_t      mbui_cur (mbui_iterator_t iter);
      84     extern const char *  mbui_cur_ptr (mbui_iterator_t iter);
      85     extern void          mbui_reloc (mbui_iterator_t iter, ptrdiff_t ptrdiff);
      86     extern void          mbui_copy (mbui_iterator_t *new, const mbui_iterator_t *old);
      87   */
      88  
      89  #ifndef _MBUITER_H
      90  #define _MBUITER_H 1
      91  
      92  /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE,
      93     _GL_ATTRIBUTE_ALWAYS_INLINE.  */
      94  #if !_GL_CONFIG_H_INCLUDED
      95   #error "Please include config.h first."
      96  #endif
      97  
      98  #include <assert.h>
      99  #include <stddef.h>
     100  #include <stdlib.h>
     101  #include <string.h>
     102  #include <uchar.h>
     103  #include <wchar.h>
     104  
     105  #include "mbchar.h"
     106  #include "strnlen1.h"
     107  
     108  _GL_INLINE_HEADER_BEGIN
     109  #ifndef MBUITER_INLINE
     110  # define MBUITER_INLINE _GL_INLINE _GL_ATTRIBUTE_ALWAYS_INLINE
     111  #endif
     112  
     113  struct mbuiter_multi
     114  {
     115    #if !GNULIB_MBRTOC32_REGULAR
     116    bool in_shift;        /* true if next byte may not be interpreted as ASCII */
     117                          /* If GNULIB_MBRTOC32_REGULAR, it is always false,
     118                             so optimize it away.  */
     119    #endif
     120    mbstate_t state;      /* if in_shift: current shift state */
     121                          /* If GNULIB_MBRTOC32_REGULAR, it is in an initial state
     122                             before and after every mbuiter_multi_next invocation.
     123                           */
     124    bool next_done;       /* true if mbui_avail has already filled the following */
     125    unsigned int cur_max; /* A cache of MB_CUR_MAX.  */
     126    struct mbchar cur;    /* the current character:
     127          const char *cur.ptr          pointer to current character
     128          The following are only valid after mbui_avail.
     129          size_t cur.bytes             number of bytes of current character
     130          bool cur.wc_valid            true if wc is a valid 32-bit wide character
     131          char32_t cur.wc              if wc_valid: the current character
     132          */
     133  };
     134  
     135  MBUITER_INLINE void
     136  mbuiter_multi_next (struct mbuiter_multi *iter)
     137  {
     138    if (iter->next_done)
     139      return;
     140    #if !GNULIB_MBRTOC32_REGULAR
     141    if (iter->in_shift)
     142      goto with_shift;
     143    #endif
     144    /* Handle most ASCII characters quickly, without calling mbrtowc().  */
     145    if (is_basic (*iter->cur.ptr))
     146      {
     147        /* These characters are part of the POSIX portable character set.
     148           For most of them, namely those in the ISO C basic character set,
     149           ISO C 99 guarantees that their wide character code is identical to
     150           their char code.  For the few other ones, this is the case as well,
     151           in all locale encodings that are in use.  The 32-bit wide character
     152           code is the same as well.  */
     153        iter->cur.bytes = 1;
     154        iter->cur.wc = *iter->cur.ptr;
     155        iter->cur.wc_valid = true;
     156      }
     157    else
     158      {
     159        assert (mbsinit (&iter->state));
     160        #if !GNULIB_MBRTOC32_REGULAR
     161        iter->in_shift = true;
     162      with_shift:
     163        #endif
     164        iter->cur.bytes = mbrtoc32 (&iter->cur.wc, iter->cur.ptr,
     165                                    strnlen1 (iter->cur.ptr, iter->cur_max),
     166                                    &iter->state);
     167        if (iter->cur.bytes == (size_t) -1)
     168          {
     169            /* An invalid multibyte sequence was encountered.  */
     170            iter->cur.bytes = 1;
     171            iter->cur.wc_valid = false;
     172            /* Allow the next invocation to continue from a sane state.  */
     173            #if !GNULIB_MBRTOC32_REGULAR
     174            iter->in_shift = false;
     175            #endif
     176            mbszero (&iter->state);
     177          }
     178        else if (iter->cur.bytes == (size_t) -2)
     179          {
     180            /* An incomplete multibyte character at the end.  */
     181            iter->cur.bytes = strlen (iter->cur.ptr);
     182            iter->cur.wc_valid = false;
     183            /* Whether to set iter->in_shift = false and reset iter->state
     184               or not is not important; the string end is reached anyway.  */
     185          }
     186        else
     187          {
     188            if (iter->cur.bytes == 0)
     189              {
     190                /* A null wide character was encountered.  */
     191                iter->cur.bytes = 1;
     192                assert (*iter->cur.ptr == '\0');
     193                assert (iter->cur.wc == 0);
     194              }
     195            #if !GNULIB_MBRTOC32_REGULAR
     196            else if (iter->cur.bytes == (size_t) -3)
     197              /* The previous multibyte sequence produced an additional 32-bit
     198                 wide character.  */
     199              iter->cur.bytes = 0;
     200            #endif
     201            iter->cur.wc_valid = true;
     202  
     203            /* When in an initial state, we can go back treating ASCII
     204               characters more quickly.  */
     205            #if !GNULIB_MBRTOC32_REGULAR
     206            if (mbsinit (&iter->state))
     207              iter->in_shift = false;
     208            #endif
     209          }
     210      }
     211    iter->next_done = true;
     212  }
     213  
     214  MBUITER_INLINE void
     215  mbuiter_multi_reloc (struct mbuiter_multi *iter, ptrdiff_t ptrdiff)
     216  {
     217    iter->cur.ptr += ptrdiff;
     218  }
     219  
     220  MBUITER_INLINE void
     221  mbuiter_multi_copy (struct mbuiter_multi *new_iter, const struct mbuiter_multi *old_iter)
     222  {
     223    #if !GNULIB_MBRTOC32_REGULAR
     224    if ((new_iter->in_shift = old_iter->in_shift))
     225      memcpy (&new_iter->state, &old_iter->state, sizeof (mbstate_t));
     226    else
     227    #endif
     228      mbszero (&new_iter->state);
     229    new_iter->next_done = old_iter->next_done;
     230    new_iter->cur_max = old_iter->cur_max;
     231    mb_copy (&new_iter->cur, &old_iter->cur);
     232  }
     233  
     234  /* Iteration macros.  */
     235  typedef struct mbuiter_multi mbui_iterator_t;
     236  #if !GNULIB_MBRTOC32_REGULAR
     237  #define mbui_init(iter, startptr) \
     238    ((iter).cur.ptr = (startptr), \
     239     (iter).in_shift = false, mbszero (&(iter).state), \
     240     (iter).next_done = false, \
     241     (iter).cur_max = MB_CUR_MAX)
     242  #else
     243  /* Optimized: no in_shift.  */
     244  #define mbui_init(iter, startptr) \
     245    ((iter).cur.ptr = (startptr), \
     246     mbszero (&(iter).state), \
     247     (iter).next_done = false, \
     248     (iter).cur_max = MB_CUR_MAX)
     249  #endif
     250  #define mbui_avail(iter) \
     251    (mbuiter_multi_next (&(iter)), !mb_isnul ((iter).cur))
     252  #define mbui_advance(iter) \
     253    ((iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false)
     254  
     255  /* Access to the current character.  */
     256  #define mbui_cur(iter) (iter).cur
     257  #define mbui_cur_ptr(iter) (iter).cur.ptr
     258  
     259  /* Relocation.  */
     260  #define mbui_reloc(iter, ptrdiff) mbuiter_multi_reloc (&iter, ptrdiff)
     261  
     262  /* Copying an iterator.  */
     263  #define mbui_copy mbuiter_multi_copy
     264  
     265  _GL_INLINE_HEADER_END
     266  
     267  #endif /* _MBUITER_H */