summaryrefslogtreecommitdiff
path: root/usual/ctype.h
blob: 81a8ac08e8c02bc626ca3379ed72057cb274d006 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * ctype wrappers
 *
 * Copyright (c) 2011  Marko Kreen
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/**
 * @file
 * ctype compat.
 *
 * Provides wrappers that make sure the functions work on 'char' values.
 *
 * @note
 * POSIX requires that these functions accept EOF/-1 in addition
 * to ordinary byte values.  That means when working on 'char',
 * the functions cannot differetiate between 0xFF and EOF.
 * As no code should give EOF to <ctype.h> functions and no code
 * should depend whether 0xFF is labeled ispunct() or not,
 * it seems no worthwhile to fix it.
 */

#ifndef _USUAL_CTYPE_H_
#define _USUAL_CTYPE_H_

#include <usual/base.h>

#include <ctype.h>

#ifndef isblank
#define isblank usual_isblank
static inline int isblank(int c) { return (c == ' ') || (c == '\t'); }
#endif

/* keep right signature, cast to uchar internally */
#define _WRAP_CTYPE_FN(name) \
	static inline int safe_ ## name (int c) { \
		return name((unsigned char)(c)); \
	}

_WRAP_CTYPE_FN(isalnum)
#undef isalnum
/** Safe isalnum */
#define isalnum safe_isalnum

_WRAP_CTYPE_FN(isalpha)
#undef isalpha
/** Safe isalpha */
#define isalpha safe_isalpha

_WRAP_CTYPE_FN(isascii)
#undef isascii
/** Safe isascii */
#define isascii safe_isascii

_WRAP_CTYPE_FN(isblank)
#undef isblank
/** Safe isblank */
#define isblank safe_isblank

_WRAP_CTYPE_FN(iscntrl)
#undef iscntrl
/** Safe iscntrl */
#define iscntrl safe_iscntrl

_WRAP_CTYPE_FN(isdigit)
#undef isdigit
/** Safe isdigit */
#define isdigit safe_isdigit

_WRAP_CTYPE_FN(isgraph)
#undef isgraph
/** Safe isgraph */
#define isgraph safe_isgraph

_WRAP_CTYPE_FN(islower)
#undef islower
/** Safe islower */
#define islower safe_islower

_WRAP_CTYPE_FN(isprint)
#undef isprint
/** Safe isprint */
#define isprint safe_isprint

_WRAP_CTYPE_FN(ispunct)
#undef ispunct
/** Safe ispunct */
#define ispunct safe_ispunct

_WRAP_CTYPE_FN(isspace)
#undef isspace
/** Safe isspace */
#define isspace safe_isspace

_WRAP_CTYPE_FN(isupper)
#undef isupper
/** Safe isupper */
#define isupper safe_isupper

_WRAP_CTYPE_FN(isxdigit)
#undef isxdigit
/** Safe isxdigit */
#define isxdigit safe_isxdigit

_WRAP_CTYPE_FN(tolower)
#undef tolower
/** Safe tolower */
#define tolower safe_tolower

_WRAP_CTYPE_FN(toupper)
#undef toupper
/** Safe toupper */
#define toupper safe_toupper

#undef _WRAP_CTYPE_FN

#endif /* _USUAL_CTYPE_H_ */