comparison lphobos/std/ctype.d @ 86:fd32135dca3e trunk

[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!! Lots of bugfixes. Added support for special foreach on strings. Added std.array, std.utf, std.ctype and std.uni to phobos. Changed all the .c files in the gen dir to .cpp (it *is* C++ after all)
author lindquist
date Sat, 03 Nov 2007 14:44:58 +0100
parents
children
comparison
equal deleted inserted replaced
85:f869c636a113 86:fd32135dca3e
1 /*
2 * Placed into the Public Domain.
3 * Digital Mars, www.digitalmars.com
4 * Written by Walter Bright
5 */
6
7 /**
8 * Simple ASCII character classification functions.
9 * For Unicode classification, see $(LINK2 std_uni.html, std.uni).
10 * References:
11 * $(LINK2 http://www.digitalmars.com/d/ascii-table.html, ASCII Table),
12 * $(LINK2 http://en.wikipedia.org/wiki/Ascii, Wikipedia)
13 * Macros:
14 * WIKI=Phobos/StdCtype
15 */
16
17 module std.ctype;
18
19 /**
20 * Returns !=0 if c is a letter in the range (0..9, a..z, A..Z).
21 */
22 int isalnum(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_ALP|_DIG) : 0; }
23
24 /**
25 * Returns !=0 if c is an ascii upper or lower case letter.
26 */
27 int isalpha(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_ALP) : 0; }
28
29 /**
30 * Returns !=0 if c is a control character.
31 */
32 int iscntrl(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_CTL) : 0; }
33
34 /**
35 * Returns !=0 if c is a digit.
36 */
37 int isdigit(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_DIG) : 0; }
38
39 /**
40 * Returns !=0 if c is lower case ascii letter.
41 */
42 int islower(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_LC) : 0; }
43
44 /**
45 * Returns !=0 if c is a punctuation character.
46 */
47 int ispunct(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_PNC) : 0; }
48
49 /**
50 * Returns !=0 if c is a space, tab, vertical tab, form feed,
51 * carriage return, or linefeed.
52 */
53 int isspace(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_SPC) : 0; }
54
55 /**
56 * Returns !=0 if c is an upper case ascii character.
57 */
58 int isupper(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_UC) : 0; }
59
60 /**
61 * Returns !=0 if c is a hex digit (0..9, a..f, A..F).
62 */
63 int isxdigit(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_HEX) : 0; }
64
65 /**
66 * Returns !=0 if c is a printing character except for the space character.
67 */
68 int isgraph(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_ALP|_DIG|_PNC) : 0; }
69
70 /**
71 * Returns !=0 if c is a printing character including the space character.
72 */
73 int isprint(dchar c) { return (c <= 0x7F) ? _ctype[c] & (_ALP|_DIG|_PNC|_BLK) : 0; }
74
75 /**
76 * Returns !=0 if c is in the ascii character set, i.e. in the range 0..0x7F.
77 */
78 int isascii(dchar c) { return c <= 0x7F; }
79
80
81 /**
82 * If c is an upper case ascii character,
83 * return the lower case equivalent, otherwise return c.
84 */
85 dchar tolower(dchar c)
86 out (result)
87 {
88 assert(!isupper(result));
89 }
90 body
91 {
92 return isupper(c) ? c + (cast(dchar)'a' - 'A') : c;
93 }
94
95
96 /**
97 * If c is a lower case ascii character,
98 * return the upper case equivalent, otherwise return c.
99 */
100 dchar toupper(dchar c)
101 out (result)
102 {
103 assert(!islower(result));
104 }
105 body
106 {
107 return islower(c) ? c - (cast(dchar)'a' - 'A') : c;
108 }
109
110 private:
111
112 enum
113 {
114 _SPC = 8,
115 _CTL = 0x20,
116 _BLK = 0x40,
117 _HEX = 0x80,
118 _UC = 1,
119 _LC = 2,
120 _PNC = 0x10,
121 _DIG = 4,
122 _ALP = _UC|_LC,
123 }
124
125 ubyte _ctype[128] =
126 [
127 _CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,
128 _CTL,_CTL|_SPC,_CTL|_SPC,_CTL|_SPC,_CTL|_SPC,_CTL|_SPC,_CTL,_CTL,
129 _CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,
130 _CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,_CTL,
131 _SPC|_BLK,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,
132 _PNC,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,_PNC,
133 _DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,
134 _DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,_DIG|_HEX,
135 _PNC,_PNC,_PNC,_PNC,_PNC,_PNC,
136 _PNC,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC|_HEX,_UC,
137 _UC,_UC,_UC,_UC,_UC,_UC,_UC,_UC,
138 _UC,_UC,_UC,_UC,_UC,_UC,_UC,_UC,
139 _UC,_UC,_UC,_PNC,_PNC,_PNC,_PNC,_PNC,
140 _PNC,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC|_HEX,_LC,
141 _LC,_LC,_LC,_LC,_LC,_LC,_LC,_LC,
142 _LC,_LC,_LC,_LC,_LC,_LC,_LC,_LC,
143 _LC,_LC,_LC,_PNC,_PNC,_PNC,_PNC,_CTL
144 ];
145
146
147 unittest
148 {
149 assert(isspace(' '));
150 assert(!isspace('z'));
151 assert(toupper('a') == 'A');
152 assert(tolower('Q') == 'q');
153 assert(!isxdigit('G'));
154 }