Browse Source

[libc] Rewrite unrelicensable portions of ctype.h

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
d1f0e89e4e
2 changed files with 105 additions and 20 deletions
  1. 4
    3
      src/core/ctype.c
  2. 101
    17
      src/include/ctype.h

+ 4
- 3
src/core/ctype.c View File

@@ -35,11 +35,12 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
35 35
 /**
36 36
  * Check to see if character is a space
37 37
  *
38
- * @v c			Character
38
+ * @v character		Character
39 39
  * @ret isspace		Character is a space
40 40
  */
41
-int isspace ( int c ) {
42
-	switch ( c ) {
41
+int isspace ( int character ) {
42
+
43
+	switch ( character ) {
43 44
 	case ' ' :
44 45
 	case '\f' :
45 46
 	case '\n' :

+ 101
- 17
src/include/ctype.h View File

@@ -4,30 +4,114 @@
4 4
 /** @file
5 5
  *
6 6
  * Character types
7
+ *
7 8
  */
8 9
 
9
-FILE_LICENCE ( GPL2_OR_LATER );
10
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
10 11
 
11
-#define isdigit(c)	((c) >= '0' && (c) <= '9')
12
-#define islower(c)	((c) >= 'a' && (c) <= 'z')
13
-#define isupper(c)	((c) >= 'A' && (c) <= 'Z')
14
-#define isxdigit(c)	(isdigit(c) || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
15
-#define isprint(c)	((c) >= ' ' && (c) <= '~' )
12
+/**
13
+ * Check if character is a decimal digit
14
+ *
15
+ * @v character		ASCII character
16
+ * @ret is_digit	Character is a decimal digit
17
+ */
18
+static inline int isdigit ( int character ) {
16 19
 
17
-static inline unsigned char tolower(unsigned char c)
18
-{
19
-	if (isupper(c))
20
-		c -= 'A'-'a';
21
-	return c;
20
+	return ( ( character >= '0' ) && ( character <= '9' ) );
22 21
 }
23 22
 
24
-static inline unsigned char toupper(unsigned char c)
25
-{
26
-	if (islower(c))
27
-		c -= 'a'-'A';
28
-	return c;
23
+/**
24
+ * Check if character is a hexadecimal digit
25
+ *
26
+ * @v character		ASCII character
27
+ * @ret is_xdigit	Character is a hexadecimal digit
28
+ */
29
+static inline int isxdigit ( int character ) {
30
+
31
+	return ( ( ( character >= '0' ) && ( character <= '9' ) ) ||
32
+		 ( ( character >= 'A' ) && ( character <= 'F' ) ) ||
33
+		 ( ( character >= 'a' ) && ( character <= 'f' ) ) );
34
+}
35
+
36
+/**
37
+ * Check if character is an upper-case letter
38
+ *
39
+ * @v character		ASCII character
40
+ * @ret is_upper	Character is an upper-case letter
41
+ */
42
+static inline int isupper ( int character ) {
43
+
44
+	return ( ( character >= 'A' ) && ( character <= 'Z' ) );
45
+}
46
+
47
+/**
48
+ * Check if character is a lower-case letter
49
+ *
50
+ * @v character		ASCII character
51
+ * @ret is_lower	Character is a lower-case letter
52
+ */
53
+static inline int islower ( int character ) {
54
+
55
+	return ( ( character >= 'a' ) && ( character <= 'z' ) );
56
+}
57
+
58
+/**
59
+ * Check if character is alphabetic
60
+ *
61
+ * @v character		ASCII character
62
+ * @ret is_alpha	Character is alphabetic
63
+ */
64
+static inline int isalpha ( int character ) {
65
+
66
+	return ( isupper ( character ) || islower ( character ) );
67
+}
68
+
69
+/**
70
+ * Check if character is alphanumeric
71
+ *
72
+ * @v character		ASCII character
73
+ * @ret is_alnum	Character is alphanumeric
74
+ */
75
+static inline int isalnum ( int character ) {
76
+
77
+	return ( isalpha ( character ) || isdigit ( character ) );
78
+}
79
+
80
+/**
81
+ * Check if character is printable
82
+ *
83
+ * @v character		ASCII character
84
+ * @ret is_print	Character is printable
85
+ */
86
+static inline int isprint ( int character ) {
87
+
88
+	return ( ( character >= ' ' ) && ( character <= '~' ) );
89
+}
90
+
91
+/**
92
+ * Convert character to lower case
93
+ *
94
+ * @v character		Character
95
+ * @v character		Lower-case character
96
+ */
97
+static inline int tolower ( int character ) {
98
+
99
+	return ( isupper ( character ) ?
100
+		 ( character - 'A' + 'a' ) : character );
101
+}
102
+
103
+/**
104
+ * Convert character to upper case
105
+ *
106
+ * @v character		Character
107
+ * @v character		Upper-case character
108
+ */
109
+static inline int toupper ( int character ) {
110
+
111
+	return ( islower ( character ) ?
112
+		 ( character - 'a' + 'A' ) : character );
29 113
 }
30 114
 
31
-extern int isspace ( int c );
115
+extern int isspace ( int character );
32 116
 
33 117
 #endif /* _CTYPE_H */

Loading…
Cancel
Save