From eb67ab113e9463fcab83f9ce0847a3df197cc68d Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 14 Oct 2022 21:12:26 +0200 Subject: [PATCH] libc: Add ctype.h --- libs/libc/include/ctype.h | 58 ++++++++++++++++++++++++++++ libs/libc/src/ctype.cpp | 81 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 libs/libc/include/ctype.h create mode 100644 libs/libc/src/ctype.cpp diff --git a/libs/libc/include/ctype.h b/libs/libc/include/ctype.h new file mode 100644 index 00000000..003b3d45 --- /dev/null +++ b/libs/libc/include/ctype.h @@ -0,0 +1,58 @@ +#ifndef _CTYPE_H +#define _CTYPE_H + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Is this character alphanumeric? */ + int isalnum(int c); + + /* Is this character a letter? */ + int isalpha(int c); + + /* Is this character part of ASCII? */ + int isascii(int c); + + /* Is this character a blank character (space or tab)? */ + int isblank(int c); + + /* Is this character a control character? */ + int iscntrl(int c); + + /* Is this character a digit? */ + int isdigit(int c); + + /* Is this character any printable character except space? */ + int isgraph(int c); + + /* Is this character a lowercase letter? */ + int islower(int c); + + /* Is this character any printable character (including space)? */ + int isprint(int c); + + /* Is this character any printable character which is not a space or an alphanumeric character? */ + int ispunct(int c); + + /* Is this character any space character (space, form feed, newline, carriage return, tab, vertical tab)? */ + int isspace(int c); + + /* Is this character an uppercase letter? */ + int isupper(int c); + + /* Is this character a hexadecimal digit (0-9, a-f, A-F)? */ + int isxdigit(int c); + + /* Returns the lowercase form of the specified character. */ + int tolower(int c); + + /* Returns the uppercase form of the specified character. */ + int toupper(int c); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libs/libc/src/ctype.cpp b/libs/libc/src/ctype.cpp new file mode 100644 index 00000000..bf74cd5d --- /dev/null +++ b/libs/libc/src/ctype.cpp @@ -0,0 +1,81 @@ +#include + +extern "C" +{ + int isalnum(int c) + { + return isalpha(c) || isdigit(c); + } + + int isalpha(int c) + { + return islower(c) || isupper(c); + } + + int isascii(int c) + { + return !(c & ~0x7f); + } + + int isblank(int c) + { + return c == ' ' || c == '\t'; + } + + int iscntrl(int c) + { + return (unsigned int)c < 0x20 || c == 0x7f; + } + + int isdigit(int c) + { + return c >= '0' && c < ':'; + } + + int isgraph(int c) + { + return (unsigned int)c - 0x21 < 0x5e; + } + + int islower(int c) + { + return c >= 'a' && c < '{'; + } + + int isprint(int c) + { + return (unsigned int)c - 0x20 < 0x5f; + } + + int ispunct(int c) + { + return isgraph(c) && !isalnum(c); + } + + int isspace(int c) + { + return c == ' ' || (unsigned int)c - '\t' < 5; + } + + int isupper(int c) + { + return c >= 'A' && c < '['; + } + + int isxdigit(int c) + { + return isdigit(c) || ((unsigned int)c | 32) - 'a' < 6; + } + + int tolower(int c) + { + if (isupper(c)) return c | 32; + return c; + } + + int toupper(int c) + { + if (islower(c)) return c & 0x5f; + return c; + } +} \ No newline at end of file