Pico-Arduino
WCharacter.h
1 /*
2  WCharacter.h - Character utility functions for Wiring & Arduino
3  Copyright (c) 2010 Hernando Barragan. All right reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef Character_h
21 #define Character_h
22 
23 #include <ctype.h>
24 
25 namespace arduino {
26 
27 // WCharacter.h prototypes
28 inline bool isAlphaNumeric(int c) __attribute__((always_inline));
29 inline bool isAlpha(int c) __attribute__((always_inline));
30 inline bool isAscii(int c) __attribute__((always_inline));
31 inline bool isWhitespace(int c) __attribute__((always_inline));
32 inline bool isControl(int c) __attribute__((always_inline));
33 inline bool isDigit(int c) __attribute__((always_inline));
34 inline bool isGraph(int c) __attribute__((always_inline));
35 inline bool isLowerCase(int c) __attribute__((always_inline));
36 inline bool isPrintable(int c) __attribute__((always_inline));
37 inline bool isPunct(int c) __attribute__((always_inline));
38 inline bool isSpace(int c) __attribute__((always_inline));
39 inline bool isUpperCase(int c) __attribute__((always_inline));
40 inline bool isHexadecimalDigit(int c) __attribute__((always_inline));
41 inline int toAscii(int c) __attribute__((always_inline));
42 inline int toLowerCase(int c) __attribute__((always_inline));
43 inline int toUpperCase(int c)__attribute__((always_inline));
44 
45 
46 // Checks for an alphanumeric character.
47 // It is equivalent to (isalpha(c) || isdigit(c)).
48 inline bool isAlphaNumeric(int c)
49 {
50  return ( isalnum(c) == 0 ? false : true);
51 }
52 
53 
54 // Checks for an alphabetic character.
55 // It is equivalent to (isupper(c) || islower(c)).
56 inline bool isAlpha(int c)
57 {
58  return ( isalpha(c) == 0 ? false : true);
59 }
60 
61 
62 // Checks whether c is a 7-bit unsigned char value
63 // that fits into the ASCII character set.
64 inline bool isAscii(int c)
65 {
66  return ( isascii (c) == 0 ? false : true);
67 }
68 
69 
70 // Checks for a blank character, that is, a space or a tab.
71 inline bool isWhitespace(int c)
72 {
73  return ( isblank (c) == 0 ? false : true);
74 }
75 
76 
77 // Checks for a control character.
78 inline bool isControl(int c)
79 {
80  return ( iscntrl (c) == 0 ? false : true);
81 }
82 
83 
84 // Checks for a digit (0 through 9).
85 inline bool isDigit(int c)
86 {
87  return ( isdigit (c) == 0 ? false : true);
88 }
89 
90 
91 // Checks for any printable character except space.
92 inline bool isGraph(int c)
93 {
94  return ( isgraph (c) == 0 ? false : true);
95 }
96 
97 
98 // Checks for a lower-case character.
99 inline bool isLowerCase(int c)
100 {
101  return (islower (c) == 0 ? false : true);
102 }
103 
104 
105 // Checks for any printable character including space.
106 inline bool isPrintable(int c)
107 {
108  return ( isprint (c) == 0 ? false : true);
109 }
110 
111 
112 // Checks for any printable character which is not a space
113 // or an alphanumeric character.
114 inline bool isPunct(int c)
115 {
116  return ( ispunct (c) == 0 ? false : true);
117 }
118 
119 
120 // Checks for white-space characters. For the avr-libc library,
121 // these are: space, formfeed ('\f'), newline ('\n'), carriage
122 // return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
123 inline bool isSpace(int c)
124 {
125  return ( isspace (c) == 0 ? false : true);
126 }
127 
128 
129 // Checks for an uppercase letter.
130 inline bool isUpperCase(int c)
131 {
132  return ( isupper (c) == 0 ? false : true);
133 }
134 
135 
136 // Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
137 // 8 9 a b c d e f A B C D E F.
138 inline bool isHexadecimalDigit(int c)
139 {
140  return ( isxdigit (c) == 0 ? false : true);
141 }
142 
143 
144 // Converts c to a 7-bit unsigned char value that fits into the
145 // ASCII character set, by clearing the high-order bits.
146 inline int toAscii(int c)
147 {
148  return toascii (c);
149 }
150 
151 
152 // Warning:
153 // Many people will be unhappy if you use this function.
154 // This function will convert accented letters into random
155 // characters.
156 
157 // Converts the letter c to lower case, if possible.
158 inline int toLowerCase(int c)
159 {
160  return tolower (c);
161 }
162 
163 
164 // Converts the letter c to upper case, if possible.
165 inline int toUpperCase(int c)
166 {
167  return toupper (c);
168 }
169 
170 }
171 #endif