inttypes print specifiers

This commit is contained in:
bumbread 2022-06-06 15:57:25 +11:00
parent 6412fdc006
commit 4f9381f052
7 changed files with 215 additions and 58 deletions

View File

@ -1,20 +1,23 @@
@echo off @echo off
setlocal enabledelayedexpansion setlocal enabledelayedexpansion
set PLATFORM=win32
set CIABATTA_OPTIONS=-Iinc -g -gcodeview -nodefaultlibs -D_CRT_SECURE_NO_WARNINGS set CIABATTA_OPTIONS=-Iinc -g -gcodeview -nodefaultlibs -D_CRT_SECURE_NO_WARNINGS
set PLATFORM=win32
if NOT "%1"=="fast" (
del ciabatta.lib del ciabatta.lib
for /R code %%F in (*.c) do ( for /R code %%F in (*.c) do (
echo %%F echo %%F
clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS% clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS%
)
for /R platform\%PLATFORM% %%F in (*.c) do (
echo %%F
clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS%
)
llvm-ar rc ciabatta.lib build\*.obj
del build\*.obj
) )
for /R platform\%PLATFORM% %%F in (*.c) do (
echo %%F
clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS%
)
llvm-ar rc ciabatta.lib build\*.obj
clang test\test.c ciabatta.lib -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS% echo Compiling test..
del build\*.obj clang test\test.c ciabatta.lib -std=c11 -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS%

View File

@ -2,8 +2,8 @@
#include <stdarg.h> #include <stdarg.h>
#include <ctype.h> #include <ctype.h>
// Even if the user doesn't enable LIB_EXT1 we still have it existing #define __STDC_WANT_LIB_EXT1__ 1
size_t strnlen_s(const char *s, size_t maxsize); #include <string.h>
typedef void(*OutputFunc)(void* ctx, size_t n, const char str[]); typedef void(*OutputFunc)(void* ctx, size_t n, const char str[]);

View File

@ -4,6 +4,7 @@
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
// TODO: rewrite memmove to not allocate anything // TODO: rewrite memmove to not allocate anything
@ -194,8 +195,6 @@ size_t strlen(const char *s) {
return i; return i;
} }
#if __STDC_WANT_LIB_EXT1__ == 1
size_t strnlen_s(const char *s, size_t maxsize) { size_t strnlen_s(const char *s, size_t maxsize) {
if (s == NULL) return 0; if (s == NULL) return 0;
@ -205,5 +204,3 @@ size_t strnlen_s(const char *s, size_t maxsize) {
} }
return i; return i;
} }
#endif

View File

@ -4,6 +4,8 @@
#define _str_(x) #x #define _str_(x) #x
#define _str(x) _str_(x) #define _str(x) _str_(x)
#define _con(a,b) a ## b
#if !defined(_func) #if !defined(_func)
#if defined(_compiler_msvc) #if defined(_compiler_msvc)
#define _func __FUNCTION__ #define _func __FUNCTION__

View File

@ -0,0 +1,180 @@
#pragma once
#include <stdint.h>
#include <limits.h>
// typedef struct imaxdiv_t imaxdiv_t
// It's C's fault not mine
#define _spec8 "hh"
#define _spec16 ""
#define _spec32 ""
#if INT64_MAX == LONG_MAX
#define _spec64 "l"
#elif INT64_MAX == LLONG_MAX
#define _spec64 "ll"
#endif
#if INT8_LEAST_MAX == CHAR_MAX
#define _spec8l "hh"
#elif INT8_LEAST_MAX == SHORT_MAX || INT8_LEAST_MAX==INT_MAX
#define _spec8l ""
#elif INT8_LEAST_MAX == LONG_MAX
#define _spec8l "l"
#elif INT8_LEAST_MAX == LLONG_MAX
#define _spec8l "ll"
#endif
#if INT16_LEAST_MAX == CHAR_MAX
#define _spec16l "hh"
#elif INT16_LEAST_MAX == SHORT_MAX || INT16_LEAST_MAX==INT_MAX
#define _spec16l ""
#elif INT16_LEAST_MAX == LONG_MAX
#define _spec16l "l"
#elif INT16_LEAST_MAX == LLONG_MAX
#define _spec16l "ll"
#endif
#if INT32_LEAST_MAX == CHAR_MAX
#define _spec32l "hh"
#elif INT32_LEAST_MAX == SHORT_MAX || INT32_LEAST_MAX==INT_MAX
#define _spec32l ""
#elif INT32_LEAST_MAX == LONG_MAX
#define _spec32l "l"
#elif INT32_LEAST_MAX == LLONG_MAX
#define _spec32l "ll"
#endif
#if INT64_LEAST_MAX == CHAR_MAX
#define _spec64l "hh"
#elif INT64_LEAST_MAX == SHORT_MAX || INT64_LEAST_MAX==INT_MAX
#define _spec64l ""
#elif INT64_LEAST_MAX == LONG_MAX
#define _spec64l "l"
#elif INT64_LEAST_MAX == LLONG_MAX
#define _spec64l "ll"
#endif
#if INT8_FAST_MAX == CHAR_MAX
#define _spec8f "hh"
#elif INT8_FAST_MAX == SHORT_MAX || INT8_FAST_MAX==INT_MAX
#define _spec8f ""
#elif INT8_FAST_MAX == LONG_MAX
#define _spec8f "l"
#elif INT8_FAST_MAX == LLONG_MAX
#define _spec8f "ll"
#endif
#if INT16_FAST_MAX == CHAR_MAX
#define _spec16f "hh"
#elif INT16_FAST_MAX == SHORT_MAX || INT16_FAST_MAX==INT_MAX
#define _spec16f ""
#elif INT16_FAST_MAX == LONG_MAX
#define _spec16f "l"
#elif INT16_FAST_MAX == LLONG_MAX
#define _spec16f "ll"
#endif
#if INT32_FAST_MAX == CHAR_MAX
#define _spec32f "hh"
#elif INT32_FAST_MAX == SHORT_MAX || INT32_FAST_MAX==INT_MAX
#define _spec32f ""
#elif INT32_FAST_MAX == LONG_MAX
#define _spec32f "l"
#elif INT32_FAST_MAX == LLONG_MAX
#define _spec32f "ll"
#endif
#if INT64_FAST_MAX == CHAR_MAX
#define _spec64f "hh"
#elif INT64_FAST_MAX == SHORT_MAX || INT64_FAST_MAX==INT_MAX
#define _spec64f ""
#elif INT64_FAST_MAX == LONG_MAX
#define _spec64f "l"
#elif INT64_FAST_MAX == LLONG_MAX
#define _spec64f "ll"
#endif
#define PRId8 _spec8 "d"
#define PRId16 _spec16 "d"
#define PRId32 _spec32 "d"
#define PRId64 _spec64 "d"
#define PRIdFAST8 _spec8f "d"
#define PRIdFAST16 _spec16f "d"
#define PRIdFAST32 _spec32f "d"
#define PRIdFAST64 _spec64f "d"
#define PRIdLEAST8 _spec8l "d"
#define PRIdLEAST16 _spec16l "d"
#define PRIdLEAST32 _spec32l "d"
#define PRIdLEAST64 _spec64l "d"
#define PRIdPTR _specptr "d"
#define PRIi8 _spec8 "i"
#define PRIi16 _spec16 "i"
#define PRIi32 _spec32 "i"
#define PRIi64 _spec64 "i"
#define PRIiFAST8 _spec8f "i"
#define PRIiFAST16 _spec16f "i"
#define PRIiFAST32 _spec32f "i"
#define PRIiFAST64 _spec64f "i"
#define PRIiLEAST8 _spec8l "i"
#define PRIiLEAST16 _spec16l "i"
#define PRIiLEAST32 _spec32l "i"
#define PRIiLEAST64 _spec64l "i"
#define PRIiPTR _specptr "i"
#define PRIo8 _spec8 "o"
#define PRIo16 _spec16 "o"
#define PRIo32 _spec16 "o"
#define PRIo64 _spec64 "o"
#define PRIoLEAST8 _spec8l "o"
#define PRIoLEAST16 _spec16l "o"
#define PRIoLEAST32 _spec32l "o"
#define PRIoLEAST64 _spec64l "o"
#define PRIoFAST8 _spec8f "o"
#define PRIoFAST16 _spec16f "o"
#define PRIoFAST32 _spec32f "o"
#define PRIoFAST64 _spec64f "o"
#define PRIoPTR _specptr "o"
#define PRIu8 _spec8 "u"
#define PRIu16 _spec16 "u"
#define PRIu32 _spec16 "u"
#define PRIu64 _spec64 "u"
#define PRIuLEAST8 _spec8l "u"
#define PRIuLEAST16 _spec16l "u"
#define PRIuLEAST32 _spec32l "u"
#define PRIuLEAST64 _spec64l "u"
#define PRIuFAST8 _spec8f "u"
#define PRIuFAST16 _spec16f "u"
#define PRIuFAST32 _spec32f "u"
#define PRIuFAST64 _spec64f "u"
#define PRIuPTR _specptr "u"
#define PRIx8 _spec8 "x"
#define PRIx16 _spec16 "x"
#define PRIx32 _spec32 "x"
#define PRIx64 _spec64 "x"
#define PRIxLEAST8 _spec8l "x"
#define PRIxLEAST16 _spec16l "x"
#define PRIxLEAST32 _spec32l "x"
#define PRIxLEAST64 _spec64l "x"
#define PRIxFAST8 _spec8f "x"
#define PRIxFAST16 _spec16f "x"
#define PRIxFAST32 _spec32f "x"
#define PRIxFAST64 _spec64f "x"
#define PRIxPTR _specptr "x"
#define PRIX8 _spec8 "X"
#define PRIX16 _spec16 "X"
#define PRIX32 _spec32 "X"
#define PRIX64 _spec64 "X"
#define PRIXLEAST8 _spec8l "X"
#define PRIXLEAST16 _spec16l "X"
#define PRIXLEAST32 _spec32l "X"
#define PRIXLEAST64 _spec64l "X"
#define PRIXFAST8 _spec8f "X"
#define PRIXFAST16 _spec16f "X"
#define PRIXFAST32 _spec32f "X"
#define PRIXFAST64 _spec64f "X"
#define PRIXPTR _specptr "X"
// TODO: scan formats for signed and unsigned integers

View File

@ -31,15 +31,15 @@ char *strerror(int errnum);
size_t strlen(const char *s); size_t strlen(const char *s);
#if __STDC_WANT_LIB_EXT1__ == 1 #if __STDC_WANT_LIB_EXT1__ == 1
errno_t memcpy_s(void * restrict s1, rsize_t s1max, const void * restrict s2, rsize_t n); int memcpy_s(void * restrict s1, size_t s1max, const void * restrict s2, size_t n);
errno_t memmove_s(void *s1, rsize_t s1max, const void *s2, rsize_t n); int memmove_s(void *s1, size_t s1max, const void *s2, size_t n);
errno_t strcpy_s(char * restrict s1, rsize_t s1max, const char * restrict s2); int strcpy_s(char * restrict s1, size_t s1max, const char * restrict s2);
errno_t strncpy_s(char * restrict s1, rsize_t s1max,const char * restrict s2, rsize_t n); int strncpy_s(char * restrict s1, size_t s1max,const char * restrict s2, size_t n);
errno_t strcat_s(char * restrict s1, rsize_t s1max, const char * restrict s2); int strcat_s(char * restrict s1, size_t s1max, const char * restrict s2);
errno_t strncat_s(char * restrict s1, rsize_t s1max, const char * restrict s2, rsize_t n); int strncat_s(char * restrict s1, size_t s1max, const char * restrict s2, size_t n);
char *strtok_s(char * restrict s1, rsize_t * restrict s1max, const char * restrict s2, char ** restrict ptr); char *strtok_s(char * restrict s1, size_t * restrict s1max, const char * restrict s2, char ** restrict ptr);
errno_t memset_s(void *s, rsize_t smax, int c, rsize_t n); int memset_s(void *s, size_t smax, int c, size_t n);
errno_t strerror_s(char *s, rsize_t maxsize, errno_t errnum); int strerror_s(char *s, size_t maxsize, int errnum);
size_t strerrorlen_s(errno_t errnum); size_t strerrorlen_s(int errnum);
size_t strnlen_s(const char *s, size_t maxsize); size_t strnlen_s(const char *s, size_t maxsize);
#endif #endif

View File

@ -4,35 +4,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <inttypes.h>
int main(int argc, char** argv) { int main(int argc, char** argv) {
for (int i = 0; i < argc; i++) { uint64_t mynumber = 4;
printf("[%d] = %s\n", i, argv[i]); printf("%"PRIu64"\n", mynumber);
} return 0;
printf("%d\n", 583875381);
printf("%.*s", (int)3, "Hello");
for (char c = 'a'; c != 'z'; ++c) {
assert(isupper(toupper(c)));
}
double v0 = strtod("0X1.BC70A3D70A3D7P+6", NULL);
// parsing with error handling
const char *p = "111.11 -2.22 Nan nan(2) inF 0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz";
char *end;
for (double f = strtod(p, &end); p != end; f = strtod(p, &end)) {
printf("'%.*s' -> ", (int)(end - p), p);
p = end;
if (errno == ERANGE){
printf("range error, got ");
}
printf("%f\n", f);
}
// parsing without error handling
double v1 = strtod(" -0.0000000123junk", NULL);
double v2 = strtod("junk", NULL);
} }