1 /*****************************************************************************/
5 /* Character handling */
9 /* (C) 1998-2013, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
40 /* The array containing character classification data */
41 extern unsigned char _ctype[256];
43 /* Bits used to specify character classes */
44 #define _CT_LOWER 0x01 /* 0 - Lower case char */
45 #define _CT_UPPER 0x02 /* 1 - Upper case char */
46 #define _CT_DIGIT 0x04 /* 2 - Numeric digit */
47 #define _CT_XDIGIT 0x08 /* 3 - Hex digit (both lower and upper) */
48 #define _CT_CNTRL 0x10 /* 4 - Control character */
49 #define _CT_SPACE 0x20 /* 5 - The space character itself */
50 #define _CT_OTHER_WS 0x40 /* 6 - Other whitespace ('\f', '\n', '\r', '\t', and '\v') */
51 #define _CT_SPACE_TAB 0x80 /* 7 - Space or tab character */
53 /* Bit combinations */
54 #define _CT_ALNUM (_CT_LOWER | _CT_UPPER | _CT_DIGIT)
55 #define _CT_ALPHA (_CT_LOWER | _CT_UPPER)
56 #define _CT_NOT_GRAPH (_CT_CNTRL | _CT_SPACE)
57 #define _CT_NOT_PRINT (_CT_CNTRL)
58 #define _CT_NOT_PUNCT (_CT_SPACE | _CT_CNTRL | _CT_DIGIT | _CT_UPPER | _CT_LOWER)
59 #define _CT_WS (_CT_SPACE | _CT_OTHER_WS)
61 /* Character classification functions */
62 int __fastcall__ isalnum (int c);
63 int __fastcall__ isalpha (int c);
64 int __fastcall__ iscntrl (int c);
65 int __fastcall__ isdigit (int c);
66 int __fastcall__ isgraph (int c);
67 int __fastcall__ islower (int c);
68 int __fastcall__ isprint (int c);
69 int __fastcall__ ispunct (int c);
70 int __fastcall__ isspace (int c);
71 int __fastcall__ isupper (int c);
72 int __fastcall__ isxdigit (int c);
73 #if __CC65_STD__ >= __CC65_STD_C99__
74 int __fastcall__ isblank (int c); /* New in C99 */
77 int __fastcall__ toupper (int c); /* Always external */
78 int __fastcall__ tolower (int c); /* Always external */
80 #if __CC65_STD__ >= __CC65_STD_CC65__
81 unsigned char __fastcall__ toascii (unsigned char c);
82 /* Convert a target-specific character to ASCII. */
87 /* When inlining-of-known-functions is enabled, overload most of the above
88 ** functions by macroes. The function prototypes are available again after
89 ** #undef'ing the macroes.
90 ** Please note that the following macroes do NOT handle EOF correctly, as
91 ** stated in the manual. If you need correct behaviour for EOF, don't
92 ** use -Os, or #undefine the following macroes.
96 #define isalnum(c) (__AX__ = (c), \
98 __asm__ ("lda %v,y", _ctype), \
99 __asm__ ("and #%b", _CT_ALNUM), \
102 #define isalpha(c) (__AX__ = (c), \
104 __asm__ ("lda %v,y", _ctype), \
105 __asm__ ("and #%b", _CT_ALPHA), \
108 #if __CC65_STD__ >= __CC65_STD_C99__
109 #define isblank(c) (__AX__ = (c), \
111 __asm__ ("lda %v,y", _ctype), \
112 __asm__ ("and #%b", _CT_SPACE_TAB), \
116 #define iscntrl(c) (__AX__ = (c), \
118 __asm__ ("lda %v,y", _ctype), \
119 __asm__ ("and #%b", _CT_CNTRL), \
122 #define isdigit(c) (__AX__ = (c), \
124 __asm__ ("lda %v,y", _ctype), \
125 __asm__ ("and #%b", _CT_DIGIT), \
128 #define isgraph(c) (__AX__ = (c), \
130 __asm__ ("lda %v,y", _ctype), \
131 __asm__ ("and #%b", _CT_NOT_GRAPH), \
132 __asm__ ("cmp #1"), \
133 __asm__ ("lda #1"), \
134 __asm__ ("sbc #1"), \
137 #define islower(c) (__AX__ = (c), \
139 __asm__ ("lda %v,y", _ctype), \
140 __asm__ ("and #%b", _CT_LOWER), \
143 #define isprint(c) (__AX__ = (c), \
145 __asm__ ("lda %v,y", _ctype), \
146 __asm__ ("and #%b", _CT_NOT_PRINT), \
147 __asm__ ("eor #%b", _CT_NOT_PRINT), \
150 #define ispunct(c) (__AX__ = (c), \
152 __asm__ ("lda %v,y", _ctype), \
153 __asm__ ("and #%b", _CT_NOT_PUNCT), \
154 __asm__ ("cmp #1"), \
155 __asm__ ("lda #1"), \
156 __asm__ ("sbc #1"), \
159 #define isspace(c) (__AX__ = (c), \
161 __asm__ ("lda %v,y", _ctype), \
162 __asm__ ("and #%b", _CT_WS), \
165 #define isupper(c) (__AX__ = (c), \
167 __asm__ ("lda %v,y", _ctype), \
168 __asm__ ("and #%b", _CT_UPPER), \
171 #define isxdigit(c) (__AX__ = (c), \
173 __asm__ ("lda %v,y", _ctype), \
174 __asm__ ("and #%b", _CT_XDIGIT), \