dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / c-ctype.h
CommitLineData
1479465f
GJ
1/*
2 * libdpkg - Debian packaging suite library routines
3 * c-ctype.h - ASCII C locale-only functions
4 *
5 * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef LIBDPKG_C_CTYPE_H
22#define LIBDPKG_C_CTYPE_H
23
24#include <stdbool.h>
25
26#include <dpkg/macros.h>
27
28DPKG_BEGIN_DECLS
29
30#define C_CTYPE_BIT(bit) (1 << (bit))
31
32enum c_ctype_bit {
33 C_CTYPE_BLANK = C_CTYPE_BIT(0),
34 C_CTYPE_WHITE = C_CTYPE_BIT(1),
35 C_CTYPE_SPACE = C_CTYPE_BIT(2),
36 C_CTYPE_UPPER = C_CTYPE_BIT(3),
37 C_CTYPE_LOWER = C_CTYPE_BIT(4),
38 C_CTYPE_DIGIT = C_CTYPE_BIT(5),
39
40 C_CTYPE_ALPHA = C_CTYPE_UPPER | C_CTYPE_LOWER,
41 C_CTYPE_ALNUM = C_CTYPE_ALPHA | C_CTYPE_DIGIT,
42};
43
44bool
45c_isbits(int c, enum c_ctype_bit bits);
46
47/**
48 * Check if the character is [ \t].
49 */
50static inline bool
51c_isblank(int c)
52{
53 return c_isbits(c, C_CTYPE_BLANK);
54}
55
56/**
57 * Check if the character is [ \t\n].
58 */
59static inline bool
60c_iswhite(int c)
61{
62 return c_isbits(c, C_CTYPE_WHITE);
63}
64
65/**
66 * Check if the character is [ \v\t\f\r\n].
67 */
68static inline bool
69c_isspace(int c)
70{
71 return c_isbits(c, C_CTYPE_SPACE);
72}
73
74/**
75 * Check if the character is [0-9].
76 */
77static inline bool
78c_isdigit(int c)
79{
80 return c_isbits(c, C_CTYPE_DIGIT);
81}
82
83/**
84 * Check if the character is [A-Z].
85 */
86static inline bool
87c_isupper(int c)
88{
89 return c_isbits(c, C_CTYPE_UPPER);
90}
91
92/**
93 * Check if the character is [a-z].
94 */
95static inline bool
96c_islower(int c)
97{
98 return c_isbits(c, C_CTYPE_LOWER);
99}
100
101/**
102 * Check if the character is [a-zA-Z].
103 */
104static inline bool
105c_isalpha(int c)
106{
107 return c_isbits(c, C_CTYPE_ALPHA);
108}
109
110/**
111 * Check if the character is [a-zA-Z0-9].
112 */
113static inline bool
114c_isalnum(int c)
115{
116 return c_isbits(c, C_CTYPE_ALNUM);
117}
118
119/**
120 * Maps the character to its lower-case form.
121 */
122static inline int
123c_tolower(int c)
124{
125 return (c_isupper(c) ? ((unsigned char)c & ~0x20) | 0x20 : c);
126}
127
128DPKG_END_DECLS
129
130#endif