1 
2 /*
3  *  Copyright (C) 2004-2005 by Digital Mars, www.digitalmars.com
4  *  Written by Walter Bright
5  *
6  *  This software is provided 'as-is', without any express or implied
7  *  warranty. In no event will the authors be held liable for any damages
8  *  arising from the use of this software.
9  *
10  *  Permission is granted to anyone to use this software for any purpose,
11  *  including commercial applications, and to alter it and redistribute it
12  *  freely, in both source and binary form, subject to the following
13  *  restrictions:
14  *
15  *  o  The origin of this software must not be misrepresented; you must not
16  *     claim that you wrote the original software. If you use this software
17  *     in a product, an acknowledgment in the product documentation would be
18  *     appreciated but is not required.
19  *  o  Altered source versions must be plainly marked as such, and must not
20  *     be misrepresented as being the original software.
21  *  o  This notice may not be removed or altered from any source
22  *     distribution.
23  */
24 
25 // Simple ASCII char classification functions
26 
27 module rt.compiler.util.ctype;
28 
29 int isalnum(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_ALP|_DIG) : 0; }
30 int isalpha(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_ALP)      : 0; }
31 int iscntrl(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_CTL)      : 0; }
32 int isdigit(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_DIG)      : 0; }
33 int islower(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_LC)       : 0; }
34 int ispunct(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_PNC)      : 0; }
35 int isspace(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_SPC)      : 0; }
36 int isupper(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_UC)       : 0; }
37 int isxdigit(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_HEX)      : 0; }
38 int isgraph(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_ALP|_DIG|_PNC) : 0; }
39 int isprint(dchar c)  { return (c <= 0x7F) ? _ctype[c] & (_ALP|_DIG|_PNC|_BLK) : 0; }
40 int isascii(dchar c)  { return c <= 0x7F; }
41 
42 dchar tolower(dchar c)
43     out (result)
44     {
45 	assert(!isupper(result));
46     }
47     body
48     {
49 	return isupper(c) ? c + (cast(dchar)'a' - 'A') : c;
50     }
51 
52 dchar toupper(dchar c)
53     out (result)
54     {
55 	assert(!islower(result));
56     }
57     body
58     {
59 	return islower(c) ? c - (cast(dchar)'a' - 'A') : c;
60     }
61 
62 private:
63 
64 enum
65 {
66     _SPC =	8,
67     _CTL =	0x20,
68     _BLK =	0x40,
69     _HEX =	0x80,
70     _UC  =	1,
71     _LC  =	2,
72     _PNC =	0x10,
73     _DIG =	4,
74     _ALP =	_UC|_LC,
75 }
76 
77 ubyte _ctype[128] =
78 [
79 	_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,
80 	_CTL,_CTL|_SPC,_CTL|_SPC,_CTL|_SPC,_CTL|_SPC,_CTL|_SPC,_CTL,_CTL,
81 	_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,
82 	_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,
83 	_SPC|_BLK,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,
84 	_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,
85 	_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,
86 	_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,
87 	_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,
88 	_PNC,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC,
89 	_UC,_UC,_UC,_UC,_UC,_UC,_UC,_UC,
90 	_UC,_UC,_UC,_UC,_UC,_UC,_UC,_UC,
91 	_UC,_UC,_UC,_PNC,_PNC,_PNC,_PNC,_PNC,
92 	_PNC,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC,
93 	_LC,_LC,_LC,_LC,_LC,_LC,_LC,_LC,
94 	_LC,_LC,_LC,_LC,_LC,_LC,_LC,_LC,
95 	_LC,_LC,_LC,_PNC,_PNC,_PNC,_PNC,_CTL
96 ];
97 
98 
99 unittest
100 {
101     assert(isspace(' '));
102     assert(!isspace('z'));
103     assert(toupper('a') == 'A');
104     assert(tolower('Q') == 'q');
105     assert(!isxdigit('G'));
106 }