diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-23 19:28:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-23 19:28:08 -0400 |
| commit | fcda36561d668f43bac91ea31cd55cbbd605d330 (patch) | |
| tree | eb74c0b17df584af0fd8154422ad924e04c96cc2 /src/stdlib/print.c | |
| parent | 414b0c7472c87c5a013029aefef49e2dbc41e700 (diff) | |
Autoformat everything with clang-format
Diffstat (limited to 'src/stdlib/print.c')
| -rw-r--r-- | src/stdlib/print.c | 181 |
1 files changed, 89 insertions, 92 deletions
diff --git a/src/stdlib/print.c b/src/stdlib/print.c index 8e2dd862..d246c9c3 100644 --- a/src/stdlib/print.c +++ b/src/stdlib/print.c @@ -8,9 +8,9 @@ #include "print.h" #include "util.h" -public int _print_int(FILE *f, int64_t n) -{ - char buf[21] = {[20]=0}; // Big enough for INT64_MIN + '\0' +public +int _print_int(FILE *f, int64_t n) { + char buf[21] = {[20] = 0}; // Big enough for INT64_MIN + '\0' char *p = &buf[19]; bool negative = n < 0; @@ -19,15 +19,14 @@ public int _print_int(FILE *f, int64_t n) n /= 10; } while (n > 0); - if (negative) - *(p--) = '-'; + if (negative) *(p--) = '-'; return fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f); } -public int _print_uint(FILE *f, uint64_t n) -{ - char buf[21] = {[20]=0}; // Big enough for UINT64_MAX + '\0' +public +int _print_uint(FILE *f, uint64_t n) { + char buf[21] = {[20] = 0}; // Big enough for UINT64_MAX + '\0' char *p = &buf[19]; do { @@ -38,8 +37,8 @@ public int _print_uint(FILE *f, uint64_t n) return fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f); } -public int _print_hex(FILE *f, hex_format_t hex) -{ +public +int _print_hex(FILE *f, hex_format_t hex) { int printed = 0; if (!hex.no_prefix) printed += fputs("0x", f); if (hex.digits > 0) { @@ -50,24 +49,21 @@ public int _print_hex(FILE *f, hex_format_t hex) printed += fputc('0', f); } } - char buf[9] = {[8]='\0'}; // Enough space for FFFFFFFF + '\0' + char buf[9] = {[8] = '\0'}; // Enough space for FFFFFFFF + '\0' char *p = &buf[7]; do { uint8_t digit = hex.n % 16; - if (digit <= 9) - *(p--) = '0' + digit; - else if (hex.uppercase) - *(p--) = 'A' + digit - 10; - else - *(p--) = 'a' + digit - 10; + if (digit <= 9) *(p--) = '0' + digit; + else if (hex.uppercase) *(p--) = 'A' + digit - 10; + else *(p--) = 'a' + digit - 10; hex.n /= 16; } while (hex.n > 0); printed += (int)fwrite(p + 1, sizeof(char), (size_t)(&buf[7] - p), f); return printed; } -public int _print_oct(FILE *f, oct_format_t oct) -{ +public +int _print_oct(FILE *f, oct_format_t oct) { int printed = 0; if (!oct.no_prefix) printed += fputs("0o", f); if (oct.digits > 0) { @@ -76,7 +72,7 @@ public int _print_oct(FILE *f, oct_format_t oct) for (; oct.digits > 0; oct.digits -= 1) printed += fputc('0', f); } - char buf[12] = {[11]='\0'}; // Enough space for octal UINT64_MAX + '\0' + char buf[12] = {[11] = '\0'}; // Enough space for octal UINT64_MAX + '\0' char *p = &buf[10]; do { *(p--) = '0' + (oct.n % 8); @@ -86,25 +82,24 @@ public int _print_oct(FILE *f, oct_format_t oct) return printed; } -public int _print_double(FILE *f, double n) -{ +public +int _print_double(FILE *f, double n) { static char buf[24]; int len = fpconv_dtoa(n, buf); return (int)fwrite(buf, sizeof(char), (size_t)len, f); } -public int _print_hex_double(FILE *f, hex_double_t hex) -{ - if (hex.d != hex.d) - return fputs("NAN", f); - else if (hex.d == 1.0/0.0) - return fputs("INF", f); - else if (hex.d == -1.0/0.0) - return fputs("-INF", f); - else if (hex.d == 0.0) - return fputs("0.0", f); +public +int _print_hex_double(FILE *f, hex_double_t hex) { + if (hex.d != hex.d) return fputs("NAN", f); + else if (hex.d == 1.0 / 0.0) return fputs("INF", f); + else if (hex.d == -1.0 / 0.0) return fputs("-INF", f); + else if (hex.d == 0.0) return fputs("0.0", f); - union { double d; uint64_t u; } bits = { .d = hex.d }; + union { + double d; + uint64_t u; + } bits = {.d = hex.d}; int sign = (bits.u >> 63) & 1ull; int exp = (int)((bits.u >> 52) & 0x7FF) - 1023ull; @@ -161,30 +156,27 @@ public int _print_hex_double(FILE *f, hex_double_t hex) return fwrite(buf, sizeof(char), (size_t)(p - buf), f); } -public int _print_char(FILE *f, char c) -{ +public +int _print_char(FILE *f, char c) { #define ESC(e) "'\\" e "'" - const char *named[256] = {['\'']=ESC("'"), ['\\']=ESC("\\"), - ['\n']=ESC("n"), ['\t']=ESC("t"), ['\r']=ESC("r"), - ['\033']=ESC("e"), ['\v']=ESC("v"), ['\a']=ESC("a"), ['\b']=ESC("b")}; + const char *named[256] = { + ['\''] = ESC("'"), ['\\'] = ESC("\\"), ['\n'] = ESC("n"), ['\t'] = ESC("t"), ['\r'] = ESC("r"), + ['\033'] = ESC("e"), ['\v'] = ESC("v"), ['\a'] = ESC("a"), ['\b'] = ESC("b")}; const char *name = named[(uint8_t)c]; - if (name != NULL) - return fputs(name, f); - else if (isprint(c)) - - return fputc('\'', f) + fputc(c, f) + fputc('\'', f); + if (name != NULL) return fputs(name, f); + else if (isprint(c)) return fputc('\'', f) + fputc(c, f) + fputc('\'', f); else - return (fputs("'\\x", f) + _print_hex(f, hex((uint64_t)c, .digits=2, .no_prefix=true, .uppercase=true)) + return (fputs("'\\x", f) + _print_hex(f, hex((uint64_t)c, .digits = 2, .no_prefix = true, .uppercase = true)) + fputs("'", f)); #undef ESC } -public int _print_quoted(FILE *f, quoted_t quoted) -{ +public +int _print_quoted(FILE *f, quoted_t quoted) { #define ESC(e) "\\" e - const char *named[256] = {['"']=ESC("\""), ['\\']=ESC("\\"), - ['\n']=ESC("n"), ['\t']=ESC("t"), ['\r']=ESC("r"), - ['\033']=ESC("e"), ['\v']=ESC("v"), ['\a']=ESC("a"), ['\b']=ESC("b")}; + const char *named[256] = { + ['"'] = ESC("\""), ['\\'] = ESC("\\"), ['\n'] = ESC("n"), ['\t'] = ESC("t"), ['\r'] = ESC("r"), + ['\033'] = ESC("e"), ['\v'] = ESC("v"), ['\a'] = ESC("a"), ['\b'] = ESC("b")}; int printed = fputc('"', f); for (const char *p = quoted.str; *p; p++) { const char *name = named[(uint8_t)*p]; @@ -193,7 +185,8 @@ public int _print_quoted(FILE *f, quoted_t quoted) } else if (isprint(*p) || (uint8_t)*p > 0x7F) { printed += fputc(*p, f); } else { - printed += fputs("\\x", f) + _print_hex(f, hex((uint64_t)*p, .digits=2, .no_prefix=true, .uppercase=true)); + printed += + fputs("\\x", f) + _print_hex(f, hex((uint64_t)*p, .digits = 2, .no_prefix = true, .uppercase = true)); } } printed += fputc('"', f); @@ -202,52 +195,56 @@ public int _print_quoted(FILE *f, quoted_t quoted) } #if defined(__GLIBC__) && defined(_GNU_SOURCE) - // GLIBC has fopencookie() - static ssize_t _gc_stream_write(void *cookie, const char *buf, size_t size) { - gc_stream_t *stream = (gc_stream_t *)cookie; - if (stream->position + size + 1 > *stream->size) - *stream->buffer = GC_REALLOC(*stream->buffer, (*stream->size += MAX(MAX(16UL, *stream->size/2UL), size + 1UL))); - memcpy(&(*stream->buffer)[stream->position], buf, size); - stream->position += size; - (*stream->buffer)[stream->position] = '\0'; - return (ssize_t)size; - } +// GLIBC has fopencookie() +static ssize_t _gc_stream_write(void *cookie, const char *buf, size_t size) { + gc_stream_t *stream = (gc_stream_t *)cookie; + if (stream->position + size + 1 > *stream->size) + *stream->buffer = + GC_REALLOC(*stream->buffer, (*stream->size += MAX(MAX(16UL, *stream->size / 2UL), size + 1UL))); + memcpy(&(*stream->buffer)[stream->position], buf, size); + stream->position += size; + (*stream->buffer)[stream->position] = '\0'; + return (ssize_t)size; +} - public FILE *gc_memory_stream(char **buf, size_t *size) { - gc_stream_t *stream = GC_MALLOC(sizeof(gc_stream_t)); - stream->size = size; - stream->buffer = buf; - *stream->size = 16; - *stream->buffer = GC_MALLOC_ATOMIC(*stream->size); - (*stream->buffer)[0] = '\0'; - stream->position = 0; - cookie_io_functions_t functions = {.write = _gc_stream_write}; - return fopencookie(stream, "w", functions); - } +public +FILE *gc_memory_stream(char **buf, size_t *size) { + gc_stream_t *stream = GC_MALLOC(sizeof(gc_stream_t)); + stream->size = size; + stream->buffer = buf; + *stream->size = 16; + *stream->buffer = GC_MALLOC_ATOMIC(*stream->size); + (*stream->buffer)[0] = '\0'; + stream->position = 0; + cookie_io_functions_t functions = {.write = _gc_stream_write}; + return fopencookie(stream, "w", functions); +} #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) - // BSDs have funopen() and fwopen() - static int _gc_stream_write(void *cookie, const char *buf, int size) { - gc_stream_t *stream = (gc_stream_t *)cookie; - if (stream->position + size + 1 > *stream->size) - *stream->buffer = GC_REALLOC(*stream->buffer, (*stream->size += MAX(MAX(16UL, *stream->size/2UL), size + 1UL))); - memcpy(&(*stream->buffer)[stream->position], buf, size); - stream->position += size; - (*stream->buffer)[stream->position] = '\0'; - return size; - } +// BSDs have funopen() and fwopen() +static int _gc_stream_write(void *cookie, const char *buf, int size) { + gc_stream_t *stream = (gc_stream_t *)cookie; + if (stream->position + size + 1 > *stream->size) + *stream->buffer = + GC_REALLOC(*stream->buffer, (*stream->size += MAX(MAX(16UL, *stream->size / 2UL), size + 1UL))); + memcpy(&(*stream->buffer)[stream->position], buf, size); + stream->position += size; + (*stream->buffer)[stream->position] = '\0'; + return size; +} - public FILE *gc_memory_stream(char **buf, size_t *size) { - gc_stream_t *stream = GC_MALLOC(sizeof(gc_stream_t)); - stream->size = size; - stream->buffer = buf; - *stream->size = 16; - *stream->buffer = GC_MALLOC_ATOMIC(*stream->size); - (*stream->buffer)[0] = '\0'; - stream->position = 0; - return fwopen(stream, _gc_stream_write); - } +public +FILE *gc_memory_stream(char **buf, size_t *size) { + gc_stream_t *stream = GC_MALLOC(sizeof(gc_stream_t)); + stream->size = size; + stream->buffer = buf; + *stream->size = 16; + *stream->buffer = GC_MALLOC_ATOMIC(*stream->size); + (*stream->buffer)[0] = '\0'; + stream->position = 0; + return fwopen(stream, _gc_stream_write); +} #else -# error "This platform doesn't support fopencookie() or funopen()!" +#error "This platform doesn't support fopencookie() or funopen()!" #endif // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
