Blame lib/mbuiter.h

Packit 709fb3
/* Iterating through multibyte strings: macros for multi-byte encodings.
Packit 709fb3
   Copyright (C) 2001, 2005, 2007, 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by Bruno Haible <bruno@clisp.org>.  */
Packit 709fb3
Packit 709fb3
/* The macros in this file implement forward iteration through a
Packit 709fb3
   multi-byte string, without knowing its length a-priori.
Packit 709fb3
Packit 709fb3
   With these macros, an iteration loop that looks like
Packit 709fb3
Packit 709fb3
      char *iter;
Packit 709fb3
      for (iter = buf; *iter != '\0'; iter++)
Packit 709fb3
        {
Packit 709fb3
          do_something (*iter);
Packit 709fb3
        }
Packit 709fb3
Packit 709fb3
   becomes
Packit 709fb3
Packit 709fb3
      mbui_iterator_t iter;
Packit 709fb3
      for (mbui_init (iter, buf); mbui_avail (iter); mbui_advance (iter))
Packit 709fb3
        {
Packit 709fb3
          do_something (mbui_cur_ptr (iter), mb_len (mbui_cur (iter)));
Packit 709fb3
        }
Packit 709fb3
Packit 709fb3
   The benefit of these macros over plain use of mbrtowc is:
Packit 709fb3
   - Handling of invalid multibyte sequences is possible without
Packit 709fb3
     making the code more complicated, while still preserving the
Packit 709fb3
     invalid multibyte sequences.
Packit 709fb3
Packit 709fb3
   Compared to mbiter.h, the macros here don't need to know the string's
Packit 709fb3
   length a-priori.  The downside is that at each step, the look-ahead
Packit 709fb3
   that guards against overrunning the terminating '\0' is more expensive.
Packit 709fb3
   The mbui_* macros are therefore suitable when there is a high probability
Packit 709fb3
   that only the first few multibyte characters need to be inspected.
Packit 709fb3
   Whereas the mbi_* macros are better if usually the iteration runs
Packit 709fb3
   through the entire string.
Packit 709fb3
Packit 709fb3
   mbui_iterator_t
Packit 709fb3
     is a type usable for variable declarations.
Packit 709fb3
Packit 709fb3
   mbui_init (iter, startptr)
Packit 709fb3
     initializes the iterator, starting at startptr.
Packit 709fb3
Packit 709fb3
   mbui_avail (iter)
Packit 709fb3
     returns true if there are more multibyte characters available before
Packit 709fb3
     the end of string is reached. In this case, mbui_cur (iter) is
Packit 709fb3
     initialized to the next multibyte character.
Packit 709fb3
Packit 709fb3
   mbui_advance (iter)
Packit 709fb3
     advances the iterator by one multibyte character.
Packit 709fb3
Packit 709fb3
   mbui_cur (iter)
Packit 709fb3
     returns the current multibyte character, of type mbchar_t.  All the
Packit 709fb3
     macros defined in mbchar.h can be used on it.
Packit 709fb3
Packit 709fb3
   mbui_cur_ptr (iter)
Packit 709fb3
     return a pointer to the beginning of the current multibyte character.
Packit 709fb3
Packit 709fb3
   mbui_reloc (iter, ptrdiff)
Packit 709fb3
     relocates iterator when the string is moved by ptrdiff bytes.
Packit 709fb3
Packit 709fb3
   mbui_copy (&destiter, &srciter)
Packit 709fb3
     copies srciter to destiter.
Packit 709fb3
Packit 709fb3
   Here are the function prototypes of the macros.
Packit 709fb3
Packit 709fb3
   extern void          mbui_init (mbui_iterator_t iter, const char *startptr);
Packit 709fb3
   extern bool          mbui_avail (mbui_iterator_t iter);
Packit 709fb3
   extern void          mbui_advance (mbui_iterator_t iter);
Packit 709fb3
   extern mbchar_t      mbui_cur (mbui_iterator_t iter);
Packit 709fb3
   extern const char *  mbui_cur_ptr (mbui_iterator_t iter);
Packit 709fb3
   extern void          mbui_reloc (mbui_iterator_t iter, ptrdiff_t ptrdiff);
Packit 709fb3
   extern void          mbui_copy (mbui_iterator_t *new, const mbui_iterator_t *old);
Packit 709fb3
 */
Packit 709fb3
Packit 709fb3
#ifndef _MBUITER_H
Packit 709fb3
#define _MBUITER_H 1
Packit 709fb3
Packit 709fb3
#include <assert.h>
Packit 709fb3
#include <stdbool.h>
Packit 709fb3
#include <stddef.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
Packit 709fb3
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
Packit 709fb3
   <wchar.h>.
Packit 709fb3
   BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
Packit 709fb3
   <wchar.h>.  */
Packit 709fb3
#include <stdio.h>
Packit 709fb3
#include <time.h>
Packit 709fb3
#include <wchar.h>
Packit 709fb3
Packit 709fb3
#include "mbchar.h"
Packit 709fb3
#include "strnlen1.h"
Packit 709fb3
Packit 709fb3
#ifndef _GL_INLINE_HEADER_BEGIN
Packit 709fb3
 #error "Please include config.h first."
Packit 709fb3
#endif
Packit 709fb3
_GL_INLINE_HEADER_BEGIN
Packit 709fb3
#ifndef MBUITER_INLINE
Packit 709fb3
# define MBUITER_INLINE _GL_INLINE
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
struct mbuiter_multi
Packit 709fb3
{
Packit 709fb3
  bool in_shift;        /* true if next byte may not be interpreted as ASCII */
Packit 709fb3
  mbstate_t state;      /* if in_shift: current shift state */
Packit 709fb3
  bool next_done;       /* true if mbui_avail has already filled the following */
Packit 709fb3
  struct mbchar cur;    /* the current character:
Packit 709fb3
        const char *cur.ptr             pointer to current character
Packit 709fb3
        The following are only valid after mbui_avail.
Packit 709fb3
        size_t cur.bytes                number of bytes of current character
Packit 709fb3
        bool cur.wc_valid               true if wc is a valid wide character
Packit 709fb3
        wchar_t cur.wc                  if wc_valid: the current character
Packit 709fb3
        */
Packit 709fb3
};
Packit 709fb3
Packit 709fb3
MBUITER_INLINE void
Packit 709fb3
mbuiter_multi_next (struct mbuiter_multi *iter)
Packit 709fb3
{
Packit 709fb3
  if (iter->next_done)
Packit 709fb3
    return;
Packit 709fb3
  if (iter->in_shift)
Packit 709fb3
    goto with_shift;
Packit 709fb3
  /* Handle most ASCII characters quickly, without calling mbrtowc().  */
Packit 709fb3
  if (is_basic (*iter->cur.ptr))
Packit 709fb3
    {
Packit 709fb3
      /* These characters are part of the basic character set.  ISO C 99
Packit 709fb3
         guarantees that their wide character code is identical to their
Packit 709fb3
         char code.  */
Packit 709fb3
      iter->cur.bytes = 1;
Packit 709fb3
      iter->cur.wc = *iter->cur.ptr;
Packit 709fb3
      iter->cur.wc_valid = true;
Packit 709fb3
    }
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
      assert (mbsinit (&iter->state));
Packit 709fb3
      iter->in_shift = true;
Packit 709fb3
    with_shift:
Packit 709fb3
      iter->cur.bytes = mbrtowc (&iter->cur.wc, iter->cur.ptr,
Packit 709fb3
                                 strnlen1 (iter->cur.ptr, MB_CUR_MAX),
Packit 709fb3
                                 &iter->state);
Packit 709fb3
      if (iter->cur.bytes == (size_t) -1)
Packit 709fb3
        {
Packit 709fb3
          /* An invalid multibyte sequence was encountered.  */
Packit 709fb3
          iter->cur.bytes = 1;
Packit 709fb3
          iter->cur.wc_valid = false;
Packit 709fb3
          /* Whether to set iter->in_shift = false and reset iter->state
Packit 709fb3
             or not is not very important; the string is bogus anyway.  */
Packit 709fb3
        }
Packit 709fb3
      else if (iter->cur.bytes == (size_t) -2)
Packit 709fb3
        {
Packit 709fb3
          /* An incomplete multibyte character at the end.  */
Packit 709fb3
          iter->cur.bytes = strlen (iter->cur.ptr);
Packit 709fb3
          iter->cur.wc_valid = false;
Packit 709fb3
          /* Whether to set iter->in_shift = false and reset iter->state
Packit 709fb3
             or not is not important; the string end is reached anyway.  */
Packit 709fb3
        }
Packit 709fb3
      else
Packit 709fb3
        {
Packit 709fb3
          if (iter->cur.bytes == 0)
Packit 709fb3
            {
Packit 709fb3
              /* A null wide character was encountered.  */
Packit 709fb3
              iter->cur.bytes = 1;
Packit 709fb3
              assert (*iter->cur.ptr == '\0');
Packit 709fb3
              assert (iter->cur.wc == 0);
Packit 709fb3
            }
Packit 709fb3
          iter->cur.wc_valid = true;
Packit 709fb3
Packit 709fb3
          /* When in the initial state, we can go back treating ASCII
Packit 709fb3
             characters more quickly.  */
Packit 709fb3
          if (mbsinit (&iter->state))
Packit 709fb3
            iter->in_shift = false;
Packit 709fb3
        }
Packit 709fb3
    }
Packit 709fb3
  iter->next_done = true;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
MBUITER_INLINE void
Packit 709fb3
mbuiter_multi_reloc (struct mbuiter_multi *iter, ptrdiff_t ptrdiff)
Packit 709fb3
{
Packit 709fb3
  iter->cur.ptr += ptrdiff;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
MBUITER_INLINE void
Packit 709fb3
mbuiter_multi_copy (struct mbuiter_multi *new_iter, const struct mbuiter_multi *old_iter)
Packit 709fb3
{
Packit 709fb3
  if ((new_iter->in_shift = old_iter->in_shift))
Packit 709fb3
    memcpy (&new_iter->state, &old_iter->state, sizeof (mbstate_t));
Packit 709fb3
  else
Packit 709fb3
    memset (&new_iter->state, 0, sizeof (mbstate_t));
Packit 709fb3
  new_iter->next_done = old_iter->next_done;
Packit 709fb3
  mb_copy (&new_iter->cur, &old_iter->cur);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Iteration macros.  */
Packit 709fb3
typedef struct mbuiter_multi mbui_iterator_t;
Packit 709fb3
#define mbui_init(iter, startptr) \
Packit 709fb3
  ((iter).cur.ptr = (startptr), \
Packit 709fb3
   (iter).in_shift = false, memset (&(iter).state, '\0', sizeof (mbstate_t)), \
Packit 709fb3
   (iter).next_done = false)
Packit 709fb3
#define mbui_avail(iter) \
Packit 709fb3
  (mbuiter_multi_next (&(iter)), !mb_isnul ((iter).cur))
Packit 709fb3
#define mbui_advance(iter) \
Packit 709fb3
  ((iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false)
Packit 709fb3
Packit 709fb3
/* Access to the current character.  */
Packit 709fb3
#define mbui_cur(iter) (iter).cur
Packit 709fb3
#define mbui_cur_ptr(iter) (iter).cur.ptr
Packit 709fb3
Packit 709fb3
/* Relocation.  */
Packit 709fb3
#define mbui_reloc(iter, ptrdiff) mbuiter_multi_reloc (&iter, ptrdiff)
Packit 709fb3
Packit 709fb3
/* Copying an iterator.  */
Packit 709fb3
#define mbui_copy mbuiter_multi_copy
Packit 709fb3
Packit 709fb3
_GL_INLINE_HEADER_END
Packit 709fb3
Packit 709fb3
#endif /* _MBUITER_H */