From 04a2c0a8daa07239ce710d248e0c802468622dcb Mon Sep 17 00:00:00 2001 From: Aleksander Kaminski Date: Wed, 9 Aug 2023 12:17:40 +0200 Subject: [PATCH] ctype: Provide fast macro implementation DONE: RTOS-549 --- ctype/ctype.c | 49 +++++++++++++++++++++--------- include/ctype.h | 81 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 89 insertions(+), 41 deletions(-) diff --git a/ctype/ctype.c b/ctype/ctype.c index 4a98c634..e2b124a5 100644 --- a/ctype/ctype.c +++ b/ctype/ctype.c @@ -16,85 +16,106 @@ #include "ctype.h" +#undef isalpha int isalpha(int c) { - return (islower(c) || isupper(c)); + return __isalpha(c); } +#undef isdigit int isdigit(int c) { - return (c >= '0' && c <= '9'); + return __isdigit(c); } +#undef islower int islower(int c) { - return (c >= 'a' && c <= 'z'); + return __islower(c); } +#undef isspace int isspace(int c) { - return (c == ' ' || c == '\f' || c == '\t' || c == '\n' || c == '\r' || c == '\v'); + return __isspace(c); } +#undef isupper int isupper(int c) { - return (c >= 'A' && c <= 'Z'); + return __isupper(c); } +#undef tolower int tolower(int c) { - return isupper(c) ? c - 'A' + 'a' : c; + return __tolower(c); } +#undef toupper int toupper(int c) { - return islower(c) ? c - 'a' + 'A' : c; + return __toupper(c); } +#undef isalnum int isalnum(int c) { - return isalpha(c) || isdigit(c); + return __isalnum(c); } +#undef iscntrl int iscntrl(int c) { - return (c >= 0x00 && c <= 0x1f) || c == 0x7f; + return __iscntrl(c); } +#undef isprint int isprint(int c) { - return c > 0x1f && c < 0x7f; + return __isprint(c); } +#undef isgraph int isgraph(int c) { - return c != ' ' && isprint(c); + return __isgraph(c); } +#undef ispunct int ispunct(int c) { - return isgraph(c) && !isalnum(c); + return __ispunct(c); } +#undef isxdigit int isxdigit(int c) { - return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); + return __isxdigit(c); } +#undef isblank int isblank(int c) { - return c == ' ' || c == '\t'; + return __isblank(c); +} + + +#undef isascii +int isascii(int c) +{ + return __isascii(c); } diff --git a/include/ctype.h b/include/ctype.h index a98466bf..f2ebe716 100644 --- a/include/ctype.h +++ b/include/ctype.h @@ -5,8 +5,8 @@ * * ctypes.h * - * Copyright 2017 Phoenix Systems - * Author: Pawel Pisarczyk + * Copyright 2017, 2023 Phoenix Systems + * Author: Pawel Pisarczyk, Adrian Kepka, Aleksander Kaminski * * This file is part of Phoenix-RTOS. * @@ -22,66 +22,93 @@ extern "C" { #endif -/* This function checks whether the passed character is alphanumeric. */ -extern int isalnum(int c); +/* This function checks whether the passed character is lowercase letter. */ +int islower(int c); +#define __islower(c) (((c) >= 'a' && (c) <= 'z') ? 1 : 0) +#define islower(c) __islower(c) + +/* This function checks whether the passed character is an uppercase letter. */ +int isupper(int c); +#define __isupper(c) (((c) >= 'A' && (c) <= 'Z') ? 1 : 0) +#define isupper(c) __isupper(c) /* This function checks whether the passed character is alphabetic. */ -extern int isalpha(int c); +int isalpha(int c); +#define __isalpha(c) (((islower(c) != 0) || (isupper(c) != 0)) ? 1 : 0) +#define isaplha(c) __isalpha(c) /* This function checks whether the passed character is control character. */ -extern int iscntrl(int c); +int iscntrl(int c); +#define __iscntrl(c) (((((c) >= 0x00) && ((c) <= 0x1f)) || (c) == 0x7f) ? 1 : 0) +#define iscntrl(c) __iscntrl(c) /* This function checks whether the passed character is decimal digit. */ -extern int isdigit(int c); +int isdigit(int c); +#define __isdigit(c) (((c) >= '0' && (c) <= '9') ? 1 : 0) +#define isdigit(c) __isdigit(c) -/* This function checks whether the passed character has graphical representation using locale. */ -extern int isgraph(int c); +/* This function checks whether the passed character is alphanumeric. */ +int isalnum(int c); +#define __isalnum(c) (((isalpha(c) != 0) || (isdigit(c) != 0)) ? 1 : 0) +#define isalnum(c) __isalnum(c) -/* This function checks whether the passed character is lowercase letter. */ -extern int islower(int c); +/* This function checks whether the passed character is printable. */ +int isprint(int c); +#define __isprint(c) ((((c) > 0x1f) && ((c) < 0x7f)) ? 1 : 0) +#define isprint(c) __isprint(c) -/* This function checks whether the passed character is printable. */ -extern int isprint(int c); +/* This function checks whether the passed character has graphical representation using locale. */ +int isgraph(int c); +#define __isgraph(c) ((((c) != ' ') && (isprint(c) != 0)) ? 1 : 0) +#define isgraph(c) __isgraph(c) /* This function checks whether the passed character is a punctuation character. */ -extern int ispunct(int c); +int ispunct(int c); +#define __ispunct(c) (((isgraph(c) != 0) && (isalnum(c) == 0)) ? 1 : 0) +#define ispunct(c) __ispunct(c) /* This function checks whether the passed character is white-space. */ -extern int isspace(int c); - - -/* This function checks whether the passed character is an uppercase letter. */ -extern int isupper(int c); +int isspace(int c); +#define __isspace(c) (((c) == ' ' || (c) == '\f' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\v') ? 1 : 0) +#define isspace(c) __isspace(c) /* This function checks whether the passed character is a hexadecimal digit. */ -extern int isxdigit(int c); +int isxdigit(int c); +#define __isxdigit(c) (((((c) >= '0') && ((c) <= '9')) || (((c) >= 'a') && ((c) <= 'f')) || (((c) >= 'A') && ((c) <= 'F'))) ? 1 : 0) +#define isxdigit(c) __isxdigit(c) /* This function checks whether the passed character is a blank character. */ -extern int isblank(int c); +int isblank(int c); +#define __isblank(c) ((((c) == ' ') || ((c) == '\t')) ? 1 : 0) +#define isblank(c) __isblank(c) /* This function converts uppercase letters to lowercase. */ -extern int tolower(int c); +int tolower(int c); +#define __tolower(c) ((isupper(c) != 0) ? ((c) - 'A' + 'a') : c) +#define tolower(c) __tolower(c) /* This function converts lowercase letters to uppercase. */ -extern int toupper(int c); +int toupper(int c); +#define __toupper(c) ((islower(c) != 0) ? ((c) - 'a' + 'A') : c) +#define toupper(c) __toupper(c) -static inline int isascii(int c) -{ - return (c & ~0x7f); -} +/* This function test if character is representable as ASCII value. */ +int isascii(int c); +#define __isascii(c) ((((c) < 0) || ((c) > 0x7f)) ? 0 : 1) +#define isascii(c) __isascii(c) #ifdef __cplusplus