-
Notifications
You must be signed in to change notification settings - Fork 160
/
ctype_h.c
60 lines (46 loc) · 1.46 KB
/
ctype_h.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
# ctype.h
Character classification and conversion functions.
# unsigned char typecast
I think it is required to avoid undefined behaviour, because
char may or not be signed
http://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default
and ctype functions UB if input is neither fits into `unsigned char` nor is EOF.
Mentioned at: http://www.greenend.org.uk/rjk/tech/cfu.html
Conversions to unsigned types are always defined (modulo),
and char -> unsigned char is a bijection.
*/
#include "common.h"
int main(void) {
/* # isspace */
{
assert(isspace((unsigned char)' '));
assert(isspace((unsigned char)'\n'));
assert(!isspace((unsigned char)'a'));
}
/* # isdigit */
{
assert(isdigit((unsigned char)'0'));
assert(!isdigit((unsigned char)'a'));
}
/* # ispunct */
{
assert(ispunct((unsigned char)'"'));
assert(ispunct((unsigned char)'('));
assert(ispunct((unsigned char)'.'));
assert(!ispunct((unsigned char)'a'));
assert(!ispunct((unsigned char)'0'));
}
/*
# toupper
# tolower
Work on characters.
There is no built-in string version:
http://stackoverflow.com/questions/2661766/c-convert-a-mixed-case-string-to-all-lower-case
*/
{
assert(tolower((unsigned char)'A') == 'a');
assert(toupper((unsigned char)'a') == 'A');
}
return EXIT_SUCCESS;
}