commit 9ef220998f18f0008df1dd172ec59b8cf6375aec
parent 12c32ea64864af188a0da2827b207a171f6bc2e1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 17 Feb 2017 22:17:34 +0100
[libc] Implement ctype functions
Diffstat:
17 files changed, 221 insertions(+), 14 deletions(-)
diff --git a/libc/include/ctype.h b/libc/include/ctype.h
@@ -2,19 +2,49 @@
#ifndef _CTYPE_H
#define _CTYPE_H
-int isalnum(int c);
-int isalpha(int c);
-int islower(int c);
-int isupper(int c);
-int isdigit(int c);
-int isxdigit(int c);
-int iscntrl(int c);
-int isgraph(int c);
-int isspace(int c);
-int isblank(int c);
-int isprint(int c);
-int ispunct(int c);
-int tolower(int c);
-int toupper(int c);
+extern int isalnum(int c);
+extern int isalpha(int c);
+extern int islower(int c);
+extern int isupper(int c);
+extern int isdigit(int c);
+extern int isxdigit(int c);
+extern int iscntrl(int c);
+extern int isgraph(int c);
+extern int isspace(int c);
+extern int isblank(int c);
+extern int isprint(int c);
+extern int ispunct(int c);
+extern int tolower(int c);
+extern int toupper(int c);
+
+#ifdef __USE_MACROS
+
+#define _U 0x01 /* upper */
+#define _L 0x02 /* lower */
+#define _D 0x04 /* digit */
+#define _C 0x08 /* cntrl */
+#define _P 0x10 /* punct */
+#define _S 0x20 /* white space (space/lf/tab) */
+#define _X 0x40 /* hex digit */
+#define _SP 0x80 /* hard space (0x20) */
+
+extern unsigned char _ctype[];
+
+#define isalnum(c) (_ctype[(unsigned char) c] & (_U|_L|_D))
+#define isalpha(c) (_ctype[(unsigned char) c] & (_U|_L))
+#define iscntrl(c) (_ctype[(unsigned char) c] & (_C))
+#define isdigit(c) (_ctype[(unsigned char) c] & (_D))
+#define isgraph(c) (_ctype[(unsigned char) c] & (_P|_U|_L|_D))
+#define islower(c) (_ctype[(unsigned char) c] & (_L))
+#define isprint(c) (_ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP))
+#define ispunct(c) (_ctype[(unsigned char) c] & (_P))
+#define isspace(c) (_ctype[(unsigned char) c] & (_S))
+#define isupper(c) (_ctype[(unsigned char) c] & (_U))
+#define isxdigit(c) (_ctype[(unsigned char) c] & (_D|_X))
+
+#define isascii(c) (((unsigned) c)<=0x7f)
+
+#endif
+
#endif
diff --git a/libc/src/Makefile b/libc/src/Makefile
@@ -4,6 +4,9 @@
LIBCOBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \
strrchr.o strcat.o strncpy.o strncat.o strcoll.o \
memset.o memcpy.o memmove.o memcmp.o memchr.o \
+ isalnum.o isalpha.o isascii.o iscntrl.o isdigit.o isgraph.o \
+ islower.o isprint.o ispunct.o isspace.o isupper.o isxdigit.o \
+ toupper.o tolower.o \
setlocale.o
all: libc.a
diff --git a/libc/src/ctype.c b/libc/src/ctype.c
@@ -0,0 +1,21 @@
+
+#include <ctype.h>
+
+unsigned char _ctype[255] = {
+ _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
+ _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
+ _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
+ _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
+ _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
+ _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
+ _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
+ _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
+ _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
+ _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
+ _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
+ _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
+ _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
+ _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
+ _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
+ _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
+};
diff --git a/libc/src/isalnum.c b/libc/src/isalnum.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isalnum
+
+int
+isalnum(int c)
+{
+ return _ctype[(unsigned char) c] & (_U|_L|_D);
+}
diff --git a/libc/src/isalpha.c b/libc/src/isalpha.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isalpha
+
+int
+isalpha(int c)
+{
+ return _ctype[(unsigned char) c] & (_U|_L);
+}
diff --git a/libc/src/isascii.c b/libc/src/isascii.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isascii
+
+int
+isascii(int c)
+{
+ return c <= 0x7f;
+}
diff --git a/libc/src/iscntrl.c b/libc/src/iscntrl.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef iscntrl
+
+int
+iscntrl(int c)
+{
+ return _ctype[(unsigned char) c] & (_C);
+}
diff --git a/libc/src/isdigit.c b/libc/src/isdigit.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isdigit
+
+int
+isdigit(int c)
+{
+ return _ctype[(unsigned char) c] & (_D);
+}
diff --git a/libc/src/isgraph.c b/libc/src/isgraph.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isgraph
+
+int
+isgraph(int c)
+{
+ return _ctype[(unsigned char) c] & (_P|_U|_L|_D);
+}
diff --git a/libc/src/islower.c b/libc/src/islower.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef islower
+
+int
+islower(int c)
+{
+ return _ctype[(unsigned char) c] & _L;
+}
diff --git a/libc/src/isprint.c b/libc/src/isprint.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isprint
+
+int
+isprint(int c)
+{
+ return _ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP);
+}
diff --git a/libc/src/ispunct.c b/libc/src/ispunct.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef ispunct
+
+int
+ispunct(int c)
+{
+ return _ctype[(unsigned char) c] & (_P);
+}
diff --git a/libc/src/isspace.c b/libc/src/isspace.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isspace
+
+int
+isspace(int c)
+{
+ return _ctype[(unsigned char) c] & _S;
+}
diff --git a/libc/src/isupper.c b/libc/src/isupper.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isupper
+
+int
+isupper(int c)
+{
+ return _ctype[(unsigned char) c] & _U;
+}
diff --git a/libc/src/isxdigit.c b/libc/src/isxdigit.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef isxdigit
+
+int
+isxdigit(int c)
+{
+ return _ctype[(unsigned char) c] & (_D|_X);
+}
diff --git a/libc/src/tolower.c b/libc/src/tolower.c
@@ -0,0 +1,10 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+
+int
+tolower(int c)
+{
+ return (isupper(c)) ? c & ~0x20 : c;
+}
diff --git a/libc/src/toupper.c b/libc/src/toupper.c
@@ -0,0 +1,11 @@
+/* See LICENSE file for copyright and license details. */
+
+#define __USE_MACROS
+#include <ctype.h>
+#undef toupper
+
+int
+toupper(int c)
+{
+ return (islower(c)) ? c & 0x20 : c;
+}