Merge in ctype

This commit is contained in:
bumbread 2022-06-03 10:41:48 +11:00
parent f5819e800a
commit b5de96b237
10 changed files with 202 additions and 100 deletions

View File

@ -3,6 +3,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <_platform.h> #include <_platform.h>
// TODO: use abort() to break
#if defined(_compiler_msvc)
#include <intrin.h>
#define brk __debugbreak
#else
#define brk __builtin_trap
#endif
#if defined(_os_win) #if defined(_os_win)
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include<Windows.h> #include<Windows.h>
@ -22,7 +30,7 @@
WriteConsole(conout, str, _assert_strlen(str), &written, NULL); WriteConsole(conout, str, _assert_strlen(str), &written, NULL);
} }
extern void _assert_print(char *cond, char const *func, char const *file, char const *line) extern void _assert_error(char *cond, char const *func, char const *file, char const *line)
{ {
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
if(conout != INVALID_HANDLE_VALUE) { if(conout != INVALID_HANDLE_VALUE) {
@ -42,5 +50,6 @@
"Error", "Error",
MB_OK); MB_OK);
} }
brk();
} }
#endif #endif

92
code/ctype.c Normal file
View File

@ -0,0 +1,92 @@
// TODO: table-driven?
// TODO: other locales
#include <ctype.h>
#include <locale.h>
#define in_range(low, v, high) ((low <= v) && (v <= high))
int isalnum(int c)
{
return isalpha(c) || isdigit(c);
}
int isalpha(int c)
{
return islower(c) || isupper(c);
}
int isblank(int c)
{
return c == ' ' || c == '\t';
}
int iscntrl(int c)
{
return in_range('\x00', c, '\x1f') || c == '\x7f';
}
int isdigit(int c)
{
return in_range('0', c, '9');
}
int isgraph(int c)
{
return isprint(c) && (c != ' ');
}
int islower(int c)
{
return in_range('a', c, 'z');
}
int isprint(int c)
{
return in_range(' ', c, '\x7e');
}
int ispunct(int c)
{
return in_range('\x21', c, '\x2f')
|| in_range('\x3a', c, '\x40')
|| in_range('\x5b', c, '\x60')
|| in_range('\x7b', c, '\x7e');
}
int isspace(int c)
{
return in_range('\x09', c, '\x0d')
|| c == ' ';
}
int isupper(int c)
{
return in_range('A', c, 'Z');
}
int isxdigit(int c)
{
return in_range('0', c, '9')
|| in_range('a', c, 'f')
|| in_range('A', c, 'F');
}
int tolower(int c)
{
if(isupper(c)) {
return c-'A'+'a';
}
else return c;
}
int toupper(int c)
{
if(islower(c)) {
return c-'a'+'A';
}
else return c;
}

View File

@ -1,3 +1,4 @@
#include <_platform.h> #include <_platform.h>
#include <locale.h> #include <locale.h>

13
inc/_macros.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#define _str_(x) #x
#define _str(x) _str_(x)
#if !defined(_func)
#if defined(_compiler_msvc)
#define _func __FUNCTION__
#else
#define _func __func__
#endif
#endif

View File

@ -32,14 +32,6 @@
#error "Unsupported Compiler" #error "Unsupported Compiler"
#endif #endif
#if !defined(_func)
#if defined(_compiler_msvc)
#define _func __FUNCTION__
#else
#define _func __func__
#endif
#endif
// OS Identification // OS Identification
#if defined(_WIN32) #if defined(_WIN32)
@ -58,4 +50,4 @@
#ifdef __STDC_WANT_LIB_EXT1__ #ifdef __STDC_WANT_LIB_EXT1__
typedef int errno_t; typedef int errno_t;
typedef size_t rsize_t; typedef size_t rsize_t;
#endif #endif

View File

@ -1,20 +1,9 @@
#if !defined(_assert_h)
#define _assert_h
#include "_platform.h" #pragma once
extern void _assert_print(char *cond, char const *func, char const *file, char const *line); #include "_macros.h"
// TODO: use abort() to break extern void _assert_error(char *cond, char const *func, char const *file, char const *line);
#if defined(_compiler_msvc)
#include <intrin.h>
#define _compiler_brk __debugbreak
#else
#define _compiler_brk __builtin_trap
#endif
#define _str_(x) #x
#define _str(x) _str_(x)
#if defined(NDEBUG) #if defined(NDEBUG)
#define assert(ignore) ((void)0) #define assert(ignore) ((void)0)
@ -23,10 +12,7 @@ extern void _assert_print(char *cond, char const *func, char const *file, char c
#define assert(condition) \ #define assert(condition) \
do { \ do { \
if(!(condition)) { \ if(!(condition)) { \
_assert_print(#condition, _func, __FILE__, _str(__LINE__)); \ _assert_error(#condition, _func, __FILE__, _str(__LINE__)); \
_compiler_brk(); \
} \ } \
} while(0) } while(0)
#endif #endif
#endif

View File

@ -4,5 +4,5 @@
#define EILSEQ 2 #define EILSEQ 2
#define ERANGE 3 #define ERANGE 3
// TODO: implement this // TODO:
#define errno 0 //_Thread_local int errno;

View File

@ -1,71 +1,71 @@
#include "_platform.h" #include "_platform.h"
typedef struct div_t { // typedef struct div_t {
int quot; // int quot;
int rem; // int rem;
} div_t; // } div_t;
typedef struct ldiv_t { // typedef struct ldiv_t {
long quot; // long quot;
long rem; // long rem;
} ldiv_t; // } ldiv_t;
typedef struct lldiv_t { // typedef struct lldiv_t {
long long quot; // long long quot;
long long rem; // long long rem;
} lldiv_t; // } lldiv_t;
#define EXIT_FAILURE 1 // #define EXIT_FAILURE 1
#define EXIT_SUCCESS 0 // #define EXIT_SUCCESS 0
#define RAND_MAX 65536 // #define RAND_MAX 65536
#define MB_CUR_MAX 5 // #define MB_CUR_MAX 5
double atof(const char *nptr); // double atof(const char *nptr);
int atoi(const char *nptr); // int atoi(const char *nptr);
long int atol(const char *nptr); // long int atol(const char *nptr);
long long int atoll(const char *nptr); // long long int atoll(const char *nptr);
double strtod(const char * restrict nptr, char ** restrict endptr); // double strtod(const char * restrict nptr, char ** restrict endptr);
float strtof(const char * restrict nptr, char ** restrict endptr); // float strtof(const char * restrict nptr, char ** restrict endptr);
long double strtold(const char * restrict nptr, char ** restrict endptr); // long double strtold(const char * restrict nptr, char ** restrict endptr);
long int strtol(const char * restrict nptr, char ** restrict endptr, int base); // long int strtol(const char * restrict nptr, char ** restrict endptr, int base);
long long int strtoll(const char * restrict nptr, char ** restrict endptr, int base); // long long int strtoll(const char * restrict nptr, char ** restrict endptr, int base);
unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, int base); // unsigned long int strtoul(const char * restrict nptr, char ** restrict endptr, int base);
unsigned long long int strtoull(const char * restrict nptr, char ** restrict endptr, int base); // unsigned long long int strtoull(const char * restrict nptr, char ** restrict endptr, int base);
int rand(void); // int rand(void);
void srand(unsigned int seed); // void srand(unsigned int seed);
void *aligned_alloc(size_t alignment, size_t size); // void *aligned_alloc(size_t alignment, size_t size);
void *calloc(size_t nmemb, size_t size); // void *calloc(size_t nmemb, size_t size);
void free(void *ptr); // void free(void *ptr);
void *malloc(size_t size); // void *malloc(size_t size);
void *realloc(void *ptr, size_t size); // void *realloc(void *ptr, size_t size);
_Noreturn void abort(void); // _Noreturn void abort(void);
int atexit(void (*func)(void)); // int atexit(void (*func)(void));
int at_quick_exit(void (*func)(void)); // int at_quick_exit(void (*func)(void));
_Noreturn void exit(int status); // _Noreturn void exit(int status);
_Noreturn void _Exit(int status); // _Noreturn void _Exit(int status);
char *getenv(const char *name); // char *getenv(const char *name);
_Noreturn void quick_exit(int status); // _Noreturn void quick_exit(int status);
int system(const char *string); // int system(const char *string);
void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); // void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); // void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
int abs(int j); // int abs(int j);
long int labs(long int j); // long int labs(long int j);
long long int llabs(long long int j); // long long int llabs(long long int j);
div_t div(int numer, int denom); // div_t div(int numer, int denom);
ldiv_t ldiv(long int numer, long int denom); // ldiv_t ldiv(long int numer, long int denom);
lldiv_t lldiv(long long int numer, long long int denom); // lldiv_t lldiv(long long int numer, long long int denom);
int mblen(const char *s, size_t n); // int mblen(const char *s, size_t n);
int mbtowc(wchar_t * restrict pwc, const char * restrict s, size_t n); // int mbtowc(wchar_t * restrict pwc, const char * restrict s, size_t n);
int wctomb(char *s, wchar_t wchar); // int wctomb(char *s, wchar_t wchar);
size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n); // size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n);
size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n); // size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n);
#ifdef __STDC_WANT_LIB_EXT1__ // #ifdef __STDC_WANT_LIB_EXT1__
typedef void (*constraint_handler_t)(const char * restrict msg, void * restrict ptr, errno_t error); // typedef void (*constraint_handler_t)(const char * restrict msg, void * restrict ptr, errno_t error);
constraint_handler_t set_constraint_handler_s(constraint_handler_t handler); // constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
void abort_handler_s(const char * restrict msg, void * restrict ptr, errno_t error); // void abort_handler_s(const char * restrict msg, void * restrict ptr, errno_t error);
void ignore_handler_s(const char * restrict msg, void * restrict ptr, errno_t error); // void ignore_handler_s(const char * restrict msg, void * restrict ptr, errno_t error);
errno_t getenv_s(size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name); // errno_t getenv_s(size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name);
#endif // #endif

View File

@ -1,5 +1,7 @@
#include "_platform.h" #include "_platform.h"
int _wcsicmp(const wchar_t *string1, const wchar_t *string2);
void *memcpy(void * restrict s1, const void * restrict s2, size_t n); void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n); void *memmove(void *s1, const void *s2, size_t n);
char *strcpy(char * restrict s1, const char * restrict s2); char *strcpy(char * restrict s1, const char * restrict s2);

View File

@ -1,10 +1,10 @@
GNUFLAGS=-g -gcodeview -Werror -Wall -Iinc -Icode # TODO: make shell variable-based path
CLFLAGS=/Zi /I:inc /link /incremental:no /subsystem:windows /nodefaultlib kernel32.lib CLFLAGS=/X /GS- /Iinc /Icode $(shell test\inctoarg)
CC=clang CC=clang-cl
CFLAGS=$(GNUFLAGS) CFLAGS=$(CLFLAGS)
LDFLAGS=/nologo /nodefaultlib /entry:mainCRTStartup LDFLAGS=/nologo /nodefaultlib /entry:mainCRTStartup /subsystem:console
SRC_DIR := code SRC_DIR := code
OBJ_DIR := build OBJ_DIR := build
@ -14,7 +14,14 @@ OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.obj,$(SRC_FILES))
ciabatta.lib: $(OBJ_FILES) ciabatta.lib: $(OBJ_FILES)
lib $(LDFLAGS) /out:$@ $^ lib $(LDFLAGS) /out:$@ $^
$(OBJ_DIR)/%.obj: $(SRC_DIR)/%.c $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c -o $@ $< $(CC) -c -o $@ $< $(CFLAGS)
.PHONY: ciabatta.lib clean:
del /F /Q build\*
del /F /Q lib\ciabatta.lib
inctoarg:
cl test\inctoarg.c
.PHONY: inctoarg ciabatta.lib