PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/external/bison/lib/mbchar.h

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