From d889bebc4eb07623c4621112f0bb70bb4131888d Mon Sep 17 00:00:00 2001 From: bumbread Date: Sat, 30 Jul 2022 13:06:35 +1100 Subject: [PATCH] Update assert.h --- doc/assert.md | 24 ++--- doc/ctype.md | 252 -------------------------------------------------- inc/assert.h | 11 +++ inc/std.h | 23 +++++ 4 files changed, 43 insertions(+), 267 deletions(-) delete mode 100644 doc/ctype.md create mode 100644 inc/std.h diff --git a/doc/assert.md b/doc/assert.md index 67ed8be..329e52a 100644 --- a/doc/assert.md +++ b/doc/assert.md @@ -10,26 +10,25 @@ Macro definitions: Assert a given condition is true, otherwise abort execution of a program. -
Description - The macro checks whether `expr` is true, and if not, prints the -information about where the error occurs and then aborts execution in a way -equivalent to calling abort() function (See SIGABRT). +diagnostic information and then aborts execution in a way equivalent to calling +abort() function (See SIGABRT). -You can disable the assertion checks by defining NDEBUG macro. +If DEBUG macro is defined, assert does not print a diagnostic message, and +instead simply causes a debug break. -
- -
Declaration +If NDEBUG macro is defined assert expands to an empty statement. If both NDEBUG +and DEBUG are defined, then DEBUG macro is ignored. ```c #if defined(NDEBUG) #define assert(expr) ((void)0) +#elif defined(DEBUG) + #define assert(expr) /* debug break */ #else - #define assert(expr) /* see description */ + #define assert(expr) /* print diagnostic, then abort */ #endif ``` -
Example @@ -74,11 +73,6 @@ Trace: Keyword macro that expands to C11 keyword `_Static_assert`. -
-Definition - ```c #define static_assert _Static_assert ``` -
- diff --git a/doc/ctype.md b/doc/ctype.md deleted file mode 100644 index a264719..0000000 --- a/doc/ctype.md +++ /dev/null @@ -1,252 +0,0 @@ - -# ctype.h - Classification of ASCII characters - -Function declarations: - -- [isalnum](#isalnum) -- [isalpha](#isalpha) -- [isblank](#isblank) -- [iscntrl](#iscntrl) -- [isdigit](#isdigit) -- [isgraph](#isgraph) -- [islower](#islower) -- [isprint](#isprint) -- [ispunct](#ispunct) -- [isspace](#isspace) -- [isupper](#isupper) -- [isxdigit](#isxdigit) -- [tolower](#tolower) -- [toupper](#toupper) - -## [isalnum](#isalnum) - -
Description - -Function checks whether given integer is an ASCII code representing a letter or -a digit. These characters are lowercase letters ('a'..'z'), uppercase letters -('A'..'Z') and digits ('0'..'9'). - -
- -
Declaration - -```c -int isalnum(int c); -``` - -
- -## [isalpha](#isalpha) - -
Description - -Function checks whether given integer is an ASCII code representing a letter. -These characters are lowercase letters ('a'..'z') and uppercase letters. - -
- -
Declaration - -```c -int isalpha(int c); -``` - -
- -## [isblank](#isblank) - -
Description - -Checks whether a character is a space (' ') or a horizontal tab ('\t') - -
- -
Declaration - -```c -int isblank(int c); -``` - -
- -## [iscntrl](#iscntrl) - -
Description - -Checks whether a character is a control character -('\x00' through '\x1f' and '\x7f'). - -
- -
Declaration - -```c -int iscntrl(int c); -``` - -
- -## [isdigit](#isdigit) - -
Description - -Checks whether a character is a digit ('0' through '9') - -
- -
Declaration - -```c -int isdigit(int c); -``` -
- -## [isxdigit](#isxdigit) - -
Description - -Checks whether a character is a hexadecimal digit ('0' through '9', 'a' through 'f' or 'A' through 'F') - -
- -
Declaration - -```c -int isxdigit(int c); -``` -
- -## [isgraph](#isgraph) - -
Description - -Checks whether a character is a - -- Number; -- Letter; -- or punctuation - -
- -
Declaration - -```c -int isgraph(int c); -``` -
- -## [islower](#islower) - -
Description - -Checks whether a character is a lowercase letter ('a' through 'z') - -
- -
Declaration - -```c -int islower(int c); -``` -
- -## [isupper](#isupper) - -
Description - -Checks whether a character is a uppercase letter ('A' through 'Z') - -
- -
Declaration - -```c -int islower(int c); -``` -
- -## [isprint](#isprint) - -
Description - -Checks whether a character is a - -- Number; -- Letter; -- Space; -- or punctuation - -
- -
Declaration - -```c -int isprint(int c); -``` -
- -## [ispunct](#ispunct) - -
Description - -Checks whether a character is a punctuation character (one of `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~` -or backtick). - -
- -
Declaration - -```c -int ispunct(int c); -``` -
- -## [isspace](#isspace) - -
Description - -Function checks whether given integer is a whitespace character. These -characters include whitespace (' '), form feed ('\f'), line feed ('\n'), -carriage return ('\r'), horizontal and vertical tab ('\t' and '\v'). - -
- -
Declaration - -```c -int isspace(int c); -``` - -
- -## [tolower](#tolower) - -
Description - -If the character is an uppercase letter returns lowercase version of it. Otherwise -returns the character unmodified. - -
- -
Declaration - -```c -int tolower(int c); -``` -
- -## [toupper](#toupper) - -
Description - -If the character is an lowercase letter returns uppercase version of it. Otherwise -returns the character unmodified. - -
- -
Declaration - -```c -int toupper(int c); -``` -
diff --git a/inc/assert.h b/inc/assert.h index c321100..c3dce26 100644 --- a/inc/assert.h +++ b/inc/assert.h @@ -14,6 +14,17 @@ void _Noreturn _assert( #if defined(NDEBUG) #define assert(ignore) ((void)0) +#elif defined(DEBUG) + #if __GNUC__ + #define assert(c) if (!(c)) __builtin_trap() + #elif _MSC_VER + #define assert(c) if (!(c)) __debugbreak() + // TODO: Handle Cuik, and if such handling is not required remove this comment + #else + // In debug mode there shouldn't be any optimizations so this should + // work as a simple way to cause a trap. + #define assert(c) if (!(c)) *(volatile int *)0 = 0 + #endif #else #define _static_assert _Static_assert #define assert(condition) \ diff --git a/inc/std.h b/inc/std.h new file mode 100644 index 0000000..f91d4bc --- /dev/null +++ b/inc/std.h @@ -0,0 +1,23 @@ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include