(root)/
texinfo-7.1/
gnulib/
lib/
mbchar.h
       1  /* Multibyte character data type.
       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 2.1 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  /* A multibyte character is a short subsequence of a char* string,
      20     representing a single 32-bit wide character.
      21  
      22     We use multibyte characters instead of 32-bit wide characters because
      23     of the following goals:
      24     1) correct multibyte handling, i.e. operate according to the LC_CTYPE
      25        locale,
      26     2) ease of maintenance, i.e. the maintainer needs not know all details
      27        of the ISO C 99 standard,
      28     3) don't fail grossly if the input is not in the encoding set by the
      29        locale, because often different encodings are in use in the same
      30        countries (ISO-8859-1/UTF-8, EUC-JP/Shift_JIS, ...),
      31     4) fast in the case of ASCII characters.
      32  
      33     Multibyte characters are only accessed through the mb* macros.
      34  
      35     mb_ptr (mbc)
      36       return a pointer to the beginning of the multibyte sequence.
      37  
      38     mb_len (mbc)
      39       returns the number of bytes occupied by the multibyte sequence.
      40       Always > 0.
      41  
      42     mb_iseq (mbc, sc)
      43       returns true if mbc is the standard ASCII character sc.
      44  
      45     mb_isnul (mbc)
      46       returns true if mbc is the nul character.
      47  
      48     mb_cmp (mbc1, mbc2)
      49       returns a positive, zero, or negative value depending on whether mbc1
      50       sorts after, same or before mbc2.
      51  
      52     mb_casecmp (mbc1, mbc2)
      53       returns a positive, zero, or negative value depending on whether mbc1
      54       sorts after, same or before mbc2, modulo upper/lowercase conversion.
      55  
      56     mb_equal (mbc1, mbc2)
      57       returns true if mbc1 and mbc2 are equal.
      58  
      59     mb_caseequal (mbc1, mbc2)
      60       returns true if mbc1 and mbc2 are equal modulo upper/lowercase conversion.
      61  
      62     mb_isalnum (mbc)
      63       returns true if mbc is alphanumeric.
      64  
      65     mb_isalpha (mbc)
      66       returns true if mbc is alphabetic.
      67  
      68     mb_isascii(mbc)
      69       returns true if mbc is plain ASCII.
      70  
      71     mb_isblank (mbc)
      72       returns true if mbc is a blank.
      73  
      74     mb_iscntrl (mbc)
      75       returns true if mbc is a control character.
      76  
      77     mb_isdigit (mbc)
      78       returns true if mbc is a decimal digit.
      79  
      80     mb_isgraph (mbc)
      81       returns true if mbc is a graphic character.
      82  
      83     mb_islower (mbc)
      84       returns true if mbc is lowercase.
      85  
      86     mb_isprint (mbc)
      87       returns true if mbc is a printable character.
      88  
      89     mb_ispunct (mbc)
      90       returns true if mbc is a punctuation character.
      91  
      92     mb_isspace (mbc)
      93       returns true if mbc is a space character.
      94  
      95     mb_isupper (mbc)
      96       returns true if mbc is uppercase.
      97  
      98     mb_isxdigit (mbc)
      99       returns true if mbc is a hexadecimal digit.
     100  
     101     mb_width (mbc)
     102       returns the number of columns on the output device occupied by mbc.
     103       Always >= 0.
     104  
     105     mb_putc (mbc, stream)
     106       outputs mbc on stream, a byte oriented FILE stream opened for output.
     107  
     108     mb_setascii (&mbc, sc)
     109       assigns the standard ASCII character sc to mbc.
     110       (Only available if the 'mbfile' module is in use.)
     111  
     112     mb_copy (&destmbc, &srcmbc)
     113       copies srcmbc to destmbc.
     114  
     115     Here are the function prototypes of the macros.
     116  
     117     extern const char *  mb_ptr (const mbchar_t mbc);
     118     extern size_t        mb_len (const mbchar_t mbc);
     119     extern bool          mb_iseq (const mbchar_t mbc, char sc);
     120     extern bool          mb_isnul (const mbchar_t mbc);
     121     extern int           mb_cmp (const mbchar_t mbc1, const mbchar_t mbc2);
     122     extern int           mb_casecmp (const mbchar_t mbc1, const mbchar_t mbc2);
     123     extern bool          mb_equal (const mbchar_t mbc1, const mbchar_t mbc2);
     124     extern bool          mb_caseequal (const mbchar_t mbc1, const mbchar_t mbc2);
     125     extern bool          mb_isalnum (const mbchar_t mbc);
     126     extern bool          mb_isalpha (const mbchar_t mbc);
     127     extern bool          mb_isascii (const mbchar_t mbc);
     128     extern bool          mb_isblank (const mbchar_t mbc);
     129     extern bool          mb_iscntrl (const mbchar_t mbc);
     130     extern bool          mb_isdigit (const mbchar_t mbc);
     131     extern bool          mb_isgraph (const mbchar_t mbc);
     132     extern bool          mb_islower (const mbchar_t mbc);
     133     extern bool          mb_isprint (const mbchar_t mbc);
     134     extern bool          mb_ispunct (const mbchar_t mbc);
     135     extern bool          mb_isspace (const mbchar_t mbc);
     136     extern bool          mb_isupper (const mbchar_t mbc);
     137     extern bool          mb_isxdigit (const mbchar_t mbc);
     138     extern int           mb_width (const mbchar_t mbc);
     139     extern void          mb_putc (const mbchar_t mbc, FILE *stream);
     140     extern void          mb_setascii (mbchar_t *new, char sc);
     141     extern void          mb_copy (mbchar_t *new, const mbchar_t *old);
     142   */
     143  
     144  #ifndef _MBCHAR_H
     145  #define _MBCHAR_H 1
     146  
     147  /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE.  */
     148  #if !_GL_CONFIG_H_INCLUDED
     149   #error "Please include config.h first."
     150  #endif
     151  
     152  #include <string.h>
     153  #include <uchar.h>
     154  
     155  _GL_INLINE_HEADER_BEGIN
     156  #ifndef MBCHAR_INLINE
     157  # define MBCHAR_INLINE _GL_INLINE
     158  #endif
     159  
     160  /* The longest multibyte characters, nowadays, are 4 bytes long.
     161     Regardless of the values of MB_CUR_MAX and MB_LEN_MAX.  */
     162  #define MBCHAR_BUF_SIZE 4
     163  
     164  struct mbchar
     165  {
     166    const char *ptr;      /* pointer to current character */
     167    size_t bytes;         /* number of bytes of current character, > 0 */
     168    bool wc_valid;        /* true if wc is a valid 32-bit wide character */
     169    char32_t wc;          /* if wc_valid: the current character */
     170  #if defined GNULIB_MBFILE
     171    char buf[MBCHAR_BUF_SIZE]; /* room for the bytes, used for file input only */
     172  #endif
     173  };
     174  
     175  /* EOF (not a real character) is represented with bytes = 0 and
     176     wc_valid = false.  */
     177  
     178  typedef struct mbchar mbchar_t;
     179  
     180  /* Access the current character.  */
     181  #define mb_ptr(mbc) ((mbc).ptr)
     182  #define mb_len(mbc) ((mbc).bytes)
     183  
     184  /* Comparison of characters.  */
     185  #define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc))
     186  #define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0)
     187  #define mb_cmp(mbc1, mbc2) \
     188    ((mbc1).wc_valid                                                      \
     189     ? ((mbc2).wc_valid                                                   \
     190        ? _GL_CMP ((mbc1).wc, (mbc2).wc)                                  \
     191        : -1)                                                             \
     192     : ((mbc2).wc_valid                                                   \
     193        ? 1                                                               \
     194        : (mbc1).bytes == (mbc2).bytes                                    \
     195          ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                 \
     196          : (mbc1).bytes < (mbc2).bytes                                   \
     197            ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
     198            : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
     199  #define mb_casecmp(mbc1, mbc2) \
     200    ((mbc1).wc_valid                                                      \
     201     ? ((mbc2).wc_valid                                                   \
     202        ? _GL_CMP (c32tolower ((mbc1).wc), c32tolower ((mbc2).wc))        \
     203        : -1)                                                             \
     204     : ((mbc2).wc_valid                                                   \
     205        ? 1                                                               \
     206        : (mbc1).bytes == (mbc2).bytes                                    \
     207          ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                 \
     208          : (mbc1).bytes < (mbc2).bytes                                   \
     209            ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
     210            : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
     211  #define mb_equal(mbc1, mbc2) \
     212    ((mbc1).wc_valid && (mbc2).wc_valid                                   \
     213     ? (mbc1).wc == (mbc2).wc                                             \
     214     : (mbc1).bytes == (mbc2).bytes                                       \
     215       && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
     216  #define mb_caseequal(mbc1, mbc2) \
     217    ((mbc1).wc_valid && (mbc2).wc_valid                                   \
     218     ? c32tolower ((mbc1).wc) == c32tolower ((mbc2).wc)                   \
     219     : (mbc1).bytes == (mbc2).bytes                                       \
     220       && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
     221  
     222  /* <ctype.h>, <wctype.h> classification.  */
     223  #define mb_isascii(mbc) \
     224    ((mbc).wc_valid && (mbc).wc >= 0 && (mbc).wc <= 127)
     225  #define mb_isalnum(mbc) ((mbc).wc_valid && c32isalnum ((mbc).wc))
     226  #define mb_isalpha(mbc) ((mbc).wc_valid && c32isalpha ((mbc).wc))
     227  #define mb_isblank(mbc) ((mbc).wc_valid && c32isblank ((mbc).wc))
     228  #define mb_iscntrl(mbc) ((mbc).wc_valid && c32iscntrl ((mbc).wc))
     229  #define mb_isdigit(mbc) ((mbc).wc_valid && c32isdigit ((mbc).wc))
     230  #define mb_isgraph(mbc) ((mbc).wc_valid && c32isgraph ((mbc).wc))
     231  #define mb_islower(mbc) ((mbc).wc_valid && c32islower ((mbc).wc))
     232  #define mb_isprint(mbc) ((mbc).wc_valid && c32isprint ((mbc).wc))
     233  #define mb_ispunct(mbc) ((mbc).wc_valid && c32ispunct ((mbc).wc))
     234  #define mb_isspace(mbc) ((mbc).wc_valid && c32isspace ((mbc).wc))
     235  #define mb_isupper(mbc) ((mbc).wc_valid && c32isupper ((mbc).wc))
     236  #define mb_isxdigit(mbc) ((mbc).wc_valid && c32isxdigit ((mbc).wc))
     237  
     238  /* Extra <wchar.h> function.  */
     239  
     240  /* Unprintable characters appear as a small box of width 1.  */
     241  #define MB_UNPRINTABLE_WIDTH 1
     242  
     243  MBCHAR_INLINE int
     244  mb_width_aux (char32_t wc)
     245  {
     246    int w = c32width (wc);
     247    /* For unprintable characters, arbitrarily return 0 for control characters
     248       and MB_UNPRINTABLE_WIDTH otherwise.  */
     249    return (w >= 0 ? w : c32iscntrl (wc) ? 0 : MB_UNPRINTABLE_WIDTH);
     250  }
     251  
     252  #define mb_width(mbc) \
     253    ((mbc).wc_valid ? mb_width_aux ((mbc).wc) : MB_UNPRINTABLE_WIDTH)
     254  
     255  /* Output.  */
     256  #define mb_putc(mbc, stream)  fwrite ((mbc).ptr, 1, (mbc).bytes, (stream))
     257  
     258  #if defined GNULIB_MBFILE
     259  /* Assignment.  */
     260  # define mb_setascii(mbc, sc) \
     261     ((mbc)->ptr = (mbc)->buf, (mbc)->bytes = 1, (mbc)->wc_valid = 1, \
     262      (mbc)->wc = (mbc)->buf[0] = (sc))
     263  #endif
     264  
     265  /* Copying a character.  */
     266  MBCHAR_INLINE void
     267  mb_copy (mbchar_t *new_mbc, const mbchar_t *old_mbc)
     268  {
     269  #if defined GNULIB_MBFILE
     270    if (old_mbc->ptr == &old_mbc->buf[0])
     271      {
     272        memcpy (&new_mbc->buf[0], &old_mbc->buf[0], old_mbc->bytes);
     273        new_mbc->ptr = &new_mbc->buf[0];
     274      }
     275    else
     276  #endif
     277      new_mbc->ptr = old_mbc->ptr;
     278    new_mbc->bytes = old_mbc->bytes;
     279    if ((new_mbc->wc_valid = old_mbc->wc_valid))
     280      new_mbc->wc = old_mbc->wc;
     281  }
     282  
     283  
     284  /* is_basic(c) tests whether the single-byte character c is
     285     - in the ISO C "basic character set" or is one of '@', '$', and '`'
     286       which ISO C 23 ยง 5.2.1.1.(1) guarantees to be single-byte and in
     287       practice are safe to treat as basic in the execution character set,
     288       or
     289     - in the POSIX "portable character set", which
     290       <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap06.html>
     291       equally guarantees to be single-byte.
     292     This is a convenience function, and is in this file only to share code
     293     between mbiter.h, mbuiter.h, and mbfile.h.  */
     294  #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
     295      && ('$' == 36) && ('%' == 37) && ('&' == 38) && ('\'' == 39) \
     296      && ('(' == 40) && (')' == 41) && ('*' == 42) && ('+' == 43) \
     297      && (',' == 44) && ('-' == 45) && ('.' == 46) && ('/' == 47) \
     298      && ('0' == 48) && ('1' == 49) && ('2' == 50) && ('3' == 51) \
     299      && ('4' == 52) && ('5' == 53) && ('6' == 54) && ('7' == 55) \
     300      && ('8' == 56) && ('9' == 57) && (':' == 58) && (';' == 59) \
     301      && ('<' == 60) && ('=' == 61) && ('>' == 62) && ('?' == 63) \
     302      && ('@' == 64) && ('A' == 65) && ('B' == 66) && ('C' == 67) \
     303      && ('D' == 68) && ('E' == 69) && ('F' == 70) && ('G' == 71) \
     304      && ('H' == 72) && ('I' == 73) && ('J' == 74) && ('K' == 75) \
     305      && ('L' == 76) && ('M' == 77) && ('N' == 78) && ('O' == 79) \
     306      && ('P' == 80) && ('Q' == 81) && ('R' == 82) && ('S' == 83) \
     307      && ('T' == 84) && ('U' == 85) && ('V' == 86) && ('W' == 87) \
     308      && ('X' == 88) && ('Y' == 89) && ('Z' == 90) && ('[' == 91) \
     309      && ('\\' == 92) && (']' == 93) && ('^' == 94) && ('_' == 95) \
     310      && ('`' == 96) && ('a' == 97) && ('b' == 98) && ('c' == 99) \
     311      && ('d' == 100) && ('e' == 101) && ('f' == 102) && ('g' == 103) \
     312      && ('h' == 104) && ('i' == 105) && ('j' == 106) && ('k' == 107) \
     313      && ('l' == 108) && ('m' == 109) && ('n' == 110) && ('o' == 111) \
     314      && ('p' == 112) && ('q' == 113) && ('r' == 114) && ('s' == 115) \
     315      && ('t' == 116) && ('u' == 117) && ('v' == 118) && ('w' == 119) \
     316      && ('x' == 120) && ('y' == 121) && ('z' == 122) && ('{' == 123) \
     317      && ('|' == 124) && ('}' == 125) && ('~' == 126)
     318  /* The character set is ISO-646, not EBCDIC. */
     319  # define IS_BASIC_ASCII 1
     320  
     321  /* All locale encodings (see localcharset.h) map the characters 0x00..0x7F
     322     to U+0000..U+007F, like ASCII, except for
     323       CP864      different mapping of '%'
     324       SHIFT_JIS  different mappings of 0x5C, 0x7E
     325       JOHAB      different mapping of 0x5C
     326     However, these characters in the range 0x20..0x7E are in the ISO C
     327     "basic character set" and in the POSIX "portable character set", which
     328     ISO C and POSIX guarantee to be single-byte.  Thus, locales with these
     329     encodings are not POSIX compliant.  And they are most likely not in use
     330     any more (as of 2023).  */
     331  # define is_basic(c) ((unsigned char) (c) < 0x80)
     332  
     333  #else
     334  
     335  MBCHAR_INLINE bool
     336  is_basic (char c)
     337  {
     338    switch (c)
     339      {
     340      case '\0':
     341      case '\007': case '\010':
     342      case '\t': case '\n': case '\v': case '\f': case '\r':
     343      case ' ': case '!': case '"': case '#': case '$': case '%':
     344      case '&': case '\'': case '(': case ')': case '*':
     345      case '+': case ',': case '-': case '.': case '/':
     346      case '0': case '1': case '2': case '3': case '4':
     347      case '5': case '6': case '7': case '8': case '9':
     348      case ':': case ';': case '<': case '=': case '>':
     349      case '?': case '@':
     350      case 'A': case 'B': case 'C': case 'D': case 'E':
     351      case 'F': case 'G': case 'H': case 'I': case 'J':
     352      case 'K': case 'L': case 'M': case 'N': case 'O':
     353      case 'P': case 'Q': case 'R': case 'S': case 'T':
     354      case 'U': case 'V': case 'W': case 'X': case 'Y':
     355      case 'Z':
     356      case '[': case '\\': case ']': case '^': case '_': case '`':
     357      case 'a': case 'b': case 'c': case 'd': case 'e':
     358      case 'f': case 'g': case 'h': case 'i': case 'j':
     359      case 'k': case 'l': case 'm': case 'n': case 'o':
     360      case 'p': case 'q': case 'r': case 's': case 't':
     361      case 'u': case 'v': case 'w': case 'x': case 'y':
     362      case 'z': case '{': case '|': case '}': case '~':
     363        return 1;
     364      default:
     365        return 0;
     366      }
     367  }
     368  
     369  #endif
     370  
     371  _GL_INLINE_HEADER_END
     372  
     373  #endif /* _MBCHAR_H */