shithub: scc

Download patch

ref: 060bf3254fd3df2f037225dcd9074fba8e5671a5
parent: e3b66dff7ca05a7ca74497a0275d1adaab88de8f
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Feb 17 17:17:34 EST 2017

[libc] Implement ctype functions

--- 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
--- 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
--- /dev/null
+++ b/libc/src/ctype.c
@@ -1,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 */
+};
--- /dev/null
+++ b/libc/src/isalnum.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/isalpha.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/isascii.c
@@ -1,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;
+}
--- /dev/null
+++ b/libc/src/iscntrl.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/isdigit.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/isgraph.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/islower.c
@@ -1,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;
+}
--- /dev/null
+++ b/libc/src/isprint.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/ispunct.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/isspace.c
@@ -1,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;
+}
--- /dev/null
+++ b/libc/src/isupper.c
@@ -1,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;
+}
--- /dev/null
+++ b/libc/src/isxdigit.c
@@ -1,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);
+}
--- /dev/null
+++ b/libc/src/tolower.c
@@ -1,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;
+}
--- /dev/null
+++ b/libc/src/toupper.c
@@ -1,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;
+}