mbchar.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Multibyte character data type.
  2. Copyright (C) 2001, 2005-2006 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Bruno Haible <bruno@clisp.org>. */
  15. /* A multibyte character is a short subsequence of a char* string,
  16. representing a single wide character.
  17. We use multibyte characters instead of wide characters because of
  18. the following goals:
  19. 1) correct multibyte handling, i.e. operate according to the LC_CTYPE
  20. locale,
  21. 2) ease of maintenance, i.e. the maintainer needs not know all details
  22. of the ISO C 99 standard,
  23. 3) don't fail grossly if the input is not in the encoding set by the
  24. locale, because often different encodings are in use in the same
  25. countries (ISO-8859-1/UTF-8, EUC-JP/Shift_JIS, ...),
  26. 4) fast in the case of ASCII characters,
  27. 5) portability, i.e. don't make unportable assumptions about wchar_t.
  28. Multibyte characters are only accessed through the mb* macros.
  29. mb_ptr (mbc)
  30. return a pointer to the beginning of the multibyte sequence.
  31. mb_len (mbc)
  32. returns the number of bytes occupied by the multibyte sequence.
  33. Always > 0.
  34. mb_iseq (mbc, sc)
  35. returns true if mbc is the standard ASCII character sc.
  36. mb_isnul (mbc)
  37. returns true if mbc is the nul character.
  38. mb_cmp (mbc1, mbc2)
  39. returns a positive, zero, or negative value depending on whether mbc1
  40. sorts after, same or before mbc2.
  41. mb_casecmp (mbc1, mbc2)
  42. returns a positive, zero, or negative value depending on whether mbc1
  43. sorts after, same or before mbc2, modulo upper/lowercase conversion.
  44. mb_equal (mbc1, mbc2)
  45. returns true if mbc1 and mbc2 are equal.
  46. mb_caseequal (mbc1, mbc2)
  47. returns true if mbc1 and mbc2 are equal modulo upper/lowercase conversion.
  48. mb_isalnum (mbc)
  49. returns true if mbc is alphanumeric.
  50. mb_isalpha (mbc)
  51. returns true if mbc is alphabetic.
  52. mb_isascii(mbc)
  53. returns true if mbc is plain ASCII.
  54. mb_isblank (mbc)
  55. returns true if mbc is a blank.
  56. mb_iscntrl (mbc)
  57. returns true if mbc is a control character.
  58. mb_isdigit (mbc)
  59. returns true if mbc is a decimal digit.
  60. mb_isgraph (mbc)
  61. returns true if mbc is a graphic character.
  62. mb_islower (mbc)
  63. returns true if mbc is lowercase.
  64. mb_isprint (mbc)
  65. returns true if mbc is a printable character.
  66. mb_ispunct (mbc)
  67. returns true if mbc is a punctuation character.
  68. mb_isspace (mbc)
  69. returns true if mbc is a space character.
  70. mb_isupper (mbc)
  71. returns true if mbc is uppercase.
  72. mb_isxdigit (mbc)
  73. returns true if mbc is a hexadecimal digit.
  74. mb_width (mbc)
  75. returns the number of columns on the output device occupied by mbc.
  76. Always >= 0.
  77. mb_putc (mbc, stream)
  78. outputs mbc on stream, a byte oriented FILE stream opened for output.
  79. mb_setascii (&mbc, sc)
  80. assigns the standard ASCII character sc to mbc.
  81. mb_copy (&destmbc, &srcmbc)
  82. copies srcmbc to destmbc.
  83. Here are the function prototypes of the macros.
  84. extern const char * mb_ptr (const mbchar_t mbc);
  85. extern size_t mb_len (const mbchar_t mbc);
  86. extern bool mb_iseq (const mbchar_t mbc, char sc);
  87. extern bool mb_isnul (const mbchar_t mbc);
  88. extern int mb_cmp (const mbchar_t mbc1, const mbchar_t mbc2);
  89. extern int mb_casecmp (const mbchar_t mbc1, const mbchar_t mbc2);
  90. extern bool mb_equal (const mbchar_t mbc1, const mbchar_t mbc2);
  91. extern bool mb_caseequal (const mbchar_t mbc1, const mbchar_t mbc2);
  92. extern bool mb_isalnum (const mbchar_t mbc);
  93. extern bool mb_isalpha (const mbchar_t mbc);
  94. extern bool mb_isascii (const mbchar_t mbc);
  95. extern bool mb_isblank (const mbchar_t mbc);
  96. extern bool mb_iscntrl (const mbchar_t mbc);
  97. extern bool mb_isdigit (const mbchar_t mbc);
  98. extern bool mb_isgraph (const mbchar_t mbc);
  99. extern bool mb_islower (const mbchar_t mbc);
  100. extern bool mb_isprint (const mbchar_t mbc);
  101. extern bool mb_ispunct (const mbchar_t mbc);
  102. extern bool mb_isspace (const mbchar_t mbc);
  103. extern bool mb_isupper (const mbchar_t mbc);
  104. extern bool mb_isxdigit (const mbchar_t mbc);
  105. extern int mb_width (const mbchar_t mbc);
  106. extern void mb_putc (const mbchar_t mbc, FILE *stream);
  107. extern void mb_setascii (mbchar_t *new, char sc);
  108. extern void mb_copy (mbchar_t *new, const mbchar_t *old);
  109. */
  110. #ifndef _MBCHAR_H
  111. #define _MBCHAR_H 1
  112. #include <stdbool.h>
  113. #include <string.h>
  114. /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
  115. <wchar.h>.
  116. BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
  117. <wchar.h>. */
  118. #include <stdio.h>
  119. #include <time.h>
  120. #include <wchar.h>
  121. #include <wctype.h>
  122. #include "wcwidth.h"
  123. #define MBCHAR_BUF_SIZE 24
  124. struct mbchar
  125. {
  126. const char *ptr; /* pointer to current character */
  127. size_t bytes; /* number of bytes of current character, > 0 */
  128. bool wc_valid; /* true if wc is a valid wide character */
  129. wchar_t wc; /* if wc_valid: the current character */
  130. char buf[MBCHAR_BUF_SIZE]; /* room for the bytes, used for file input only */
  131. };
  132. /* EOF (not a real character) is represented with bytes = 0 and
  133. wc_valid = false. */
  134. typedef struct mbchar mbchar_t;
  135. /* Access the current character. */
  136. #define mb_ptr(mbc) ((mbc).ptr)
  137. #define mb_len(mbc) ((mbc).bytes)
  138. /* Comparison of characters. */
  139. #define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc))
  140. #define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0)
  141. #define mb_cmp(mbc1, mbc2) \
  142. ((mbc1).wc_valid \
  143. ? ((mbc2).wc_valid \
  144. ? (int) (mbc1).wc - (int) (mbc2).wc \
  145. : -1) \
  146. : ((mbc2).wc_valid \
  147. ? 1 \
  148. : (mbc1).bytes == (mbc2).bytes \
  149. ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
  150. : (mbc1).bytes < (mbc2).bytes \
  151. ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
  152. : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
  153. #define mb_casecmp(mbc1, mbc2) \
  154. ((mbc1).wc_valid \
  155. ? ((mbc2).wc_valid \
  156. ? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc) \
  157. : -1) \
  158. : ((mbc2).wc_valid \
  159. ? 1 \
  160. : (mbc1).bytes == (mbc2).bytes \
  161. ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
  162. : (mbc1).bytes < (mbc2).bytes \
  163. ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
  164. : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
  165. #define mb_equal(mbc1, mbc2) \
  166. ((mbc1).wc_valid && (mbc2).wc_valid \
  167. ? (mbc1).wc == (mbc2).wc \
  168. : (mbc1).bytes == (mbc2).bytes \
  169. && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
  170. #define mb_caseequal(mbc1, mbc2) \
  171. ((mbc1).wc_valid && (mbc2).wc_valid \
  172. ? towlower ((mbc1).wc) == towlower ((mbc2).wc) \
  173. : (mbc1).bytes == (mbc2).bytes \
  174. && memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) == 0)
  175. /* <ctype.h>, <wctype.h> classification. */
  176. #define mb_isascii(mbc) \
  177. ((mbc).wc_valid && (mbc).wc >= 0 && (mbc).wc <= 127)
  178. #define mb_isalnum(mbc) ((mbc).wc_valid && iswalnum ((mbc).wc))
  179. #define mb_isalpha(mbc) ((mbc).wc_valid && iswalpha ((mbc).wc))
  180. #define mb_isblank(mbc) ((mbc).wc_valid && iswblank ((mbc).wc))
  181. #define mb_iscntrl(mbc) ((mbc).wc_valid && iswcntrl ((mbc).wc))
  182. #define mb_isdigit(mbc) ((mbc).wc_valid && iswdigit ((mbc).wc))
  183. #define mb_isgraph(mbc) ((mbc).wc_valid && iswgraph ((mbc).wc))
  184. #define mb_islower(mbc) ((mbc).wc_valid && iswlower ((mbc).wc))
  185. #define mb_isprint(mbc) ((mbc).wc_valid && iswprint ((mbc).wc))
  186. #define mb_ispunct(mbc) ((mbc).wc_valid && iswpunct ((mbc).wc))
  187. #define mb_isspace(mbc) ((mbc).wc_valid && iswspace ((mbc).wc))
  188. #define mb_isupper(mbc) ((mbc).wc_valid && iswupper ((mbc).wc))
  189. #define mb_isxdigit(mbc) ((mbc).wc_valid && iswxdigit ((mbc).wc))
  190. /* Extra <wchar.h> function. */
  191. /* Unprintable characters appear as a small box of width 1. */
  192. #define MB_UNPRINTABLE_WIDTH 1
  193. static inline int
  194. mb_width_aux (wint_t wc)
  195. {
  196. int w = wcwidth (wc);
  197. /* For unprintable characters, arbitrarily return 0 for control characters
  198. and MB_UNPRINTABLE_WIDTH otherwise. */
  199. return (w >= 0 ? w : iswcntrl (wc) ? 0 : MB_UNPRINTABLE_WIDTH);
  200. }
  201. #define mb_width(mbc) \
  202. ((mbc).wc_valid ? mb_width_aux ((mbc).wc) : MB_UNPRINTABLE_WIDTH)
  203. /* Output. */
  204. #define mb_putc(mbc, stream) fwrite ((mbc).ptr, 1, (mbc).bytes, (stream))
  205. /* Assignment. */
  206. #define mb_setascii(mbc, sc) \
  207. ((mbc)->ptr = (mbc)->buf, (mbc)->bytes = 1, (mbc)->wc_valid = 1, \
  208. (mbc)->wc = (mbc)->buf[0] = (sc))
  209. /* Copying a character. */
  210. static inline void
  211. mb_copy (mbchar_t *new_mbc, const mbchar_t *old_mbc)
  212. {
  213. if (old_mbc->ptr == &old_mbc->buf[0])
  214. {
  215. memcpy (&new_mbc->buf[0], &old_mbc->buf[0], old_mbc->bytes);
  216. new_mbc->ptr = &new_mbc->buf[0];
  217. }
  218. else
  219. new_mbc->ptr = old_mbc->ptr;
  220. new_mbc->bytes = old_mbc->bytes;
  221. if ((new_mbc->wc_valid = old_mbc->wc_valid))
  222. new_mbc->wc = old_mbc->wc;
  223. }
  224. /* is_basic(c) tests whether the single-byte character c is in the
  225. ISO C "basic character set".
  226. This is a convenience function, and is in this file only to share code
  227. between mbiter_multi.h and mbfile_multi.h. */
  228. #if (' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
  229. && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
  230. && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
  231. && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
  232. && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
  233. && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
  234. && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
  235. && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
  236. && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
  237. && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
  238. && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
  239. && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
  240. && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
  241. && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
  242. && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
  243. && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
  244. && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
  245. && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
  246. && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
  247. && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
  248. && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
  249. && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
  250. && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)
  251. /* The character set is ISO-646, not EBCDIC. */
  252. # define IS_BASIC_ASCII 1
  253. extern const unsigned int is_basic_table[];
  254. static inline bool
  255. is_basic (char c)
  256. {
  257. return (is_basic_table [(unsigned char) c >> 5] >> ((unsigned char) c & 31))
  258. & 1;
  259. }
  260. #else
  261. static inline bool
  262. is_basic (char c)
  263. {
  264. switch (c)
  265. {
  266. case '\t': case '\v': case '\f':
  267. case ' ': case '!': case '"': case '#': case '%':
  268. case '&': case '\'': case '(': case ')': case '*':
  269. case '+': case ',': case '-': case '.': case '/':
  270. case '0': case '1': case '2': case '3': case '4':
  271. case '5': case '6': case '7': case '8': case '9':
  272. case ':': case ';': case '<': case '=': case '>':
  273. case '?':
  274. case 'A': case 'B': case 'C': case 'D': case 'E':
  275. case 'F': case 'G': case 'H': case 'I': case 'J':
  276. case 'K': case 'L': case 'M': case 'N': case 'O':
  277. case 'P': case 'Q': case 'R': case 'S': case 'T':
  278. case 'U': case 'V': case 'W': case 'X': case 'Y':
  279. case 'Z':
  280. case '[': case '\\': case ']': case '^': case '_':
  281. case 'a': case 'b': case 'c': case 'd': case 'e':
  282. case 'f': case 'g': case 'h': case 'i': case 'j':
  283. case 'k': case 'l': case 'm': case 'n': case 'o':
  284. case 'p': case 'q': case 'r': case 's': case 't':
  285. case 'u': case 'v': case 'w': case 'x': case 'y':
  286. case 'z': case '{': case '|': case '}': case '~':
  287. return 1;
  288. default:
  289. return 0;
  290. }
  291. }
  292. #endif
  293. #endif /* _MBCHAR_H */