This commit is contained in:
iceman1001 2019-08-01 11:15:39 -04:00
commit 2400418067
11 changed files with 1882 additions and 1914 deletions

View file

@ -85,7 +85,8 @@ static const uint8_t is_hex[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static inline uint64_t hex2i(const char *s) {
uint64_t val = 0;

View file

@ -130,8 +130,7 @@ typedef struct {
// internal buffer output
static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen)
{
static inline void _out_buffer(char character, void *buffer, size_t idx, size_t maxlen) {
if (idx < maxlen) {
((char *)buffer)[idx] = character;
}
@ -139,16 +138,19 @@ static inline void _out_buffer(char character, void* buffer, size_t idx, size_t
// internal null output
static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen)
{
(void)character; (void)buffer; (void)idx; (void)maxlen;
static inline void _out_null(char character, void *buffer, size_t idx, size_t maxlen) {
(void)character;
(void)buffer;
(void)idx;
(void)maxlen;
}
// internal _putchar wrapper
static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen)
{
(void)buffer; (void)idx; (void)maxlen;
static inline void _out_char(char character, void *buffer, size_t idx, size_t maxlen) {
(void)buffer;
(void)idx;
(void)maxlen;
if (character) {
_putchar(character);
}
@ -156,9 +158,9 @@ static inline void _out_char(char character, void* buffer, size_t idx, size_t ma
// internal output function wrapper
static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen)
{
(void)idx; (void)maxlen;
static inline void _out_fct(char character, void *buffer, size_t idx, size_t maxlen) {
(void)idx;
(void)maxlen;
if (character) {
// buffer is the output fct pointer
((out_fct_wrap_type *)buffer)->fct(character, ((out_fct_wrap_type *)buffer)->arg);
@ -168,8 +170,7 @@ static inline void _out_fct(char character, void* buffer, size_t idx, size_t max
// internal secure strlen
// \return The length of the string (excluding the terminating 0) limited by 'maxsize'
static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
{
static inline unsigned int _strnlen_s(const char *str, size_t maxsize) {
const char *s;
for (s = str; *s && maxsize--; ++s);
return (unsigned int)(s - str);
@ -178,15 +179,13 @@ static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
// internal test if char is a digit (0-9)
// \return true if char is a digit
static inline bool _is_digit(char ch)
{
static inline bool _is_digit(char ch) {
return (ch >= '0') && (ch <= '9');
}
// internal ASCII string to unsigned int conversion
static unsigned int _atoi(const char** str)
{
static unsigned int _atoi(const char **str) {
unsigned int i = 0U;
while (_is_digit(**str)) {
i = i * 10U + (unsigned int)(*((*str)++) - '0');
@ -196,8 +195,7 @@ static unsigned int _atoi(const char** str)
// output the specified string in reverse, taking care of any zero-padding
static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen, const char* buf, size_t len, unsigned int width, unsigned int flags)
{
static size_t _out_rev(out_fct_type out, char *buffer, size_t idx, size_t maxlen, const char *buf, size_t len, unsigned int width, unsigned int flags) {
const size_t start_idx = idx;
// pad spaces up to given width
@ -224,8 +222,7 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen
// internal itoa format
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
{
static size_t _ntoa_format(out_fct_type out, char *buffer, size_t idx, size_t maxlen, char *buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) {
// pad leading zeros
if (!(flags & FLAGS_LEFT)) {
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
@ -249,11 +246,9 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
}
if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'x';
}
else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
} else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'X';
}
else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
} else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = 'b';
}
if (len < PRINTF_NTOA_BUFFER_SIZE) {
@ -264,11 +259,9 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
if (len < PRINTF_NTOA_BUFFER_SIZE) {
if (negative) {
buf[len++] = '-';
}
else if (flags & FLAGS_PLUS) {
} else if (flags & FLAGS_PLUS) {
buf[len++] = '+'; // ignore the space if the '+' exists
}
else if (flags & FLAGS_SPACE) {
} else if (flags & FLAGS_SPACE) {
buf[len++] = ' ';
}
}
@ -278,8 +271,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma
// internal itoa for 'long' type
static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
{
static size_t _ntoa_long(out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long value, bool negative, unsigned long base, unsigned int prec, unsigned int width, unsigned int flags) {
char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;
@ -303,8 +295,7 @@ static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxl
// internal itoa for 'long long' type
#if defined(PRINTF_SUPPORT_LONG_LONG)
static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
{
static size_t _ntoa_long_long(out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags) {
char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;
@ -336,8 +327,7 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
// internal ftoa for fixed decimal floating point
static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
static size_t _ftoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) {
char buf[PRINTF_FTOA_BUFFER_SIZE];
size_t len = 0U;
double diff = 0.0;
@ -392,10 +382,8 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
frac = 0;
++whole;
}
}
else if (diff < 0.5) {
}
else if ((frac == 0U) || (frac & 1U)) {
} else if (diff < 0.5) {
} else if ((frac == 0U) || (frac & 1U)) {
// if halfway, round up if odd OR if last digit is 0
++frac;
}
@ -407,8 +395,7 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
// 1.5 -> 2, but 2.5 -> 2
++whole;
}
}
else {
} else {
unsigned int count = prec;
// now do fractional part, as an unsigned number
while (len < PRINTF_FTOA_BUFFER_SIZE) {
@ -449,11 +436,9 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
if (len < PRINTF_FTOA_BUFFER_SIZE) {
if (negative) {
buf[len++] = '-';
}
else if (flags & FLAGS_PLUS) {
} else if (flags & FLAGS_PLUS) {
buf[len++] = '+'; // ignore the space if the '+' exists
}
else if (flags & FLAGS_SPACE) {
} else if (flags & FLAGS_SPACE) {
buf[len++] = ' ';
}
}
@ -464,8 +449,7 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) {
// check for NaN and special values
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
@ -516,16 +500,14 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
if ((value >= 1e-4) && (value < 1e6)) {
if ((int)prec > expval) {
prec = (unsigned)((int)prec - expval - 1);
}
else {
} else {
prec = 0;
}
flags |= FLAGS_PRECISION; // make sure _ftoa respects precision
// no characters in exponent
minwidth = 0U;
expval = 0;
}
else {
} else {
// we use one sigfig for the whole part
if ((prec > 0) && (flags & FLAGS_PRECISION)) {
--prec;
@ -574,8 +556,7 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
// internal vsnprintf
static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
{
static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const char *format, va_list va) {
unsigned int flags, width, precision, n;
size_t idx = 0U;
@ -584,16 +565,14 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
out = _out_null;
}
while (*format)
{
while (*format) {
// format specifier? %[flags][width][.precision][length]
if (*format != '%') {
// no
out(*format, buffer, idx++, maxlen);
format++;
continue;
}
else {
} else {
// yes, evaluate it
format++;
}
@ -602,12 +581,34 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
flags = 0U;
do {
switch (*format) {
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break;
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break;
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break;
case '#': flags |= FLAGS_HASH; format++; n = 1U; break;
default : n = 0U; break;
case '0':
flags |= FLAGS_ZEROPAD;
format++;
n = 1U;
break;
case '-':
flags |= FLAGS_LEFT;
format++;
n = 1U;
break;
case '+':
flags |= FLAGS_PLUS;
format++;
n = 1U;
break;
case ' ':
flags |= FLAGS_SPACE;
format++;
n = 1U;
break;
case '#':
flags |= FLAGS_HASH;
format++;
n = 1U;
break;
default :
n = 0U;
break;
}
} while (n);
@ -615,14 +616,12 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
width = 0U;
if (_is_digit(*format)) {
width = _atoi(&format);
}
else if (*format == '*') {
} else if (*format == '*') {
const int w = va_arg(va, int);
if (w < 0) {
flags |= FLAGS_LEFT; // reverse padding
width = (unsigned int) - w;
}
else {
} else {
width = (unsigned int)w;
}
format++;
@ -635,8 +634,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
format++;
if (_is_digit(*format)) {
precision = _atoi(&format);
}
else if (*format == '*') {
} else if (*format == '*') {
const int prec = (int)va_arg(va, int);
precision = prec > 0 ? (unsigned int)prec : 0U;
format++;
@ -692,14 +690,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
unsigned int base;
if (*format == 'x' || *format == 'X') {
base = 16U;
}
else if (*format == 'o') {
} else if (*format == 'o') {
base = 8U;
}
else if (*format == 'b') {
} else if (*format == 'b') {
base = 2U;
}
else {
} else {
base = 10U;
flags &= ~FLAGS_HASH; // no hash for dec format
}
@ -726,27 +721,22 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
const long long value = va_arg(va, long long);
idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
#endif
}
else if (flags & FLAGS_LONG) {
} else if (flags & FLAGS_LONG) {
const long value = va_arg(va, long);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
}
else {
} else {
const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int);
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags);
}
}
else {
} else {
// unsigned
if (flags & FLAGS_LONG_LONG) {
#if defined(PRINTF_SUPPORT_LONG_LONG)
idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
#endif
}
else if (flags & FLAGS_LONG) {
} else if (flags & FLAGS_LONG) {
idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
}
else {
} else {
const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);
idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
}
@ -826,8 +816,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) {
idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void *), false, 16U, precision, width, flags);
}
else {
} else {
#endif
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void *)), false, 16U, precision, width, flags);
#if defined(PRINTF_SUPPORT_LONG_LONG)
@ -859,8 +848,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
///////////////////////////////////////////////////////////////////////////////
int printf_(const char* format, ...)
{
int printf_(const char *format, ...) {
va_list va;
va_start(va, format);
char buffer[1];
@ -870,8 +858,7 @@ int printf_(const char* format, ...)
}
int sprintf_(char* buffer, const char* format, ...)
{
int sprintf_(char *buffer, const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = _vsnprintf(_out_buffer, buffer, (size_t) -1, format, va);
@ -880,8 +867,7 @@ int sprintf_(char* buffer, const char* format, ...)
}
int snprintf_(char* buffer, size_t count, const char* format, ...)
{
int snprintf_(char *buffer, size_t count, const char *format, ...) {
va_list va;
va_start(va, format);
const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
@ -890,21 +876,18 @@ int snprintf_(char* buffer, size_t count, const char* format, ...)
}
int vprintf_(const char* format, va_list va)
{
int vprintf_(const char *format, va_list va) {
char buffer[1];
return _vsnprintf(_out_char, buffer, (size_t) -1, format, va);
}
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)
{
int vsnprintf_(char *buffer, size_t count, const char *format, va_list va) {
return _vsnprintf(_out_buffer, buffer, count, format, va);
}
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...)
{
int fctprintf(void (*out)(char character, void *arg), void *arg, const char *format, ...) {
va_list va;
va_start(va, format);
const out_fct_wrap_type out_fct_wrap = { out, arg };

View file

@ -197,24 +197,21 @@ char *strtok(char *s, const char *delim) {
}
char *strchr(const char *s, int c)
{
char *strchr(const char *s, int c) {
while (*s != (char)c)
if (!*s++)
return 0;
return (char *)s;
}
size_t strspn(const char *s1, const char *s2)
{
size_t strspn(const char *s1, const char *s2) {
size_t ret = 0;
while (*s1 && strchr(s2, *s1++))
ret++;
return ret;
}
char *strrchr(const char *s, int c)
{
char *strrchr(const char *s, int c) {
const char *ret = 0;
do {
if (*s == (char)c)
@ -223,8 +220,7 @@ char *strrchr(const char *s, int c)
return (char *)ret;
}
size_t strcspn(const char *s1, const char *s2)
{
size_t strcspn(const char *s1, const char *s2) {
size_t ret = 0;
while (*s1)
if (strchr(s2, *s1))
@ -234,16 +230,14 @@ size_t strcspn(const char *s1, const char *s2)
return ret;
}
char *strpbrk(const char *s1, const char *s2)
{
char *strpbrk(const char *s1, const char *s2) {
while (*s1)
if (strchr(s2, *s1++))
return (char *)--s1;
return 0;
}
int strncmp(const char* s1, const char* s2, size_t n)
{
int strncmp(const char *s1, const char *s2, size_t n) {
while (n--)
if (*s1++ != *s2++)
return *(unsigned char *)(s1 - 1) - *(unsigned char *)(s2 - 1);
@ -255,27 +249,23 @@ int strncmp(const char* s1, const char* s2, size_t n)
#define isspace(a) __extension__ ({ unsigned char bb__isspace = (a) - 9; bb__isspace == (' ' - 9) || bb__isspace <= (13 - 9); })
unsigned long strtoul(const char *p, char **out_p, int base)
{
unsigned long strtoul(const char *p, char **out_p, int base) {
unsigned long v = 0;
while (isspace(*p))
p++;
if (((base == 16) || (base == 0)) &&
((*p == '0') && ((p[1] == 'x') || (p[1] == 'X'))))
{
((*p == '0') && ((p[1] == 'x') || (p[1] == 'X')))) {
p += 2;
base = 16;
}
if (base == 0)
{
if (base == 0) {
if (*p == '0')
base = 8;
else
base = 10;
}
while (1)
{
while (1) {
char c = *p;
if ((c >= '0') && (c <= '9') && (c - '0' < base))
v = (v * base) + (c - '0');
@ -292,8 +282,7 @@ unsigned long strtoul(const char *p, char **out_p, int base)
return v;
}
long strtol(const char *p, char **out_p, int base)
{
long strtol(const char *p, char **out_p, int base) {
long v = 0;
int is_neg = 0;
@ -304,20 +293,17 @@ long strtol(const char *p, char **out_p, int base)
else if (*p == '+')
is_neg = 0;
if (((base == 16) || (base == 0)) &&
((*p == '0') && ((p[1] == 'x') || (p[1] == 'X'))))
{
((*p == '0') && ((p[1] == 'x') || (p[1] == 'X')))) {
p += 2;
base = 16;
}
if (base == 0)
{
if (base == 0) {
if (*p == '0')
base = 8;
else
base = 10;
}
while (1)
{
while (1) {
char c = *p;
if ((c >= '0') && (c <= '9') && (c - '0' < base))
v = (v * base) + (c - '0');
@ -335,16 +321,14 @@ long strtol(const char *p, char **out_p, int base)
return v;
}
char c_tolower(int c)
{
char c_tolower(int c) {
// (int)a = 97, (int)A = 65
// (a)97 - (A)65 = 32
// therefore 32 + 65 = a
return c > 64 && c < 91 ? c + 32 : c;
}
char c_isprint (unsigned char c)
{
char c_isprint(unsigned char c) {
if (c >= 0x20 && c <= 0x7e)
return 1;
return 0;