1 // This file defines some of the helper functions used for printing values
13 int _print_int(FILE *f, int64_t n) {
14 char buf[21] = {[20] = 0}; // Big enough for INT64_MIN + '\0'
16 bool negative = n < 0;
20 *(p--) = '0' + (n % 10);
24 if (negative) *(p--) = '-';
26 return fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f);
30 int _print_uint(FILE *f, uint64_t n) {
31 char buf[21] = {[20] = 0}; // Big enough for UINT64_MAX + '\0'
35 *(p--) = '0' + (n % 10);
39 return fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f);
43 int _print_hex(FILE *f, hex_format_t hex) {
45 if (!hex.no_prefix) printed += fputs("0x", f);
47 hex.digits -= (hex.n == 0); // Don't need a leading zero for a zero
48 for (uint64_t n = hex.n; n > 0 && hex.digits > 0; n /= 16) {
51 for (; hex.digits > 0; hex.digits -= 1) {
52 printed += fputc('0', f);
55 char buf[9] = {[8] = '\0'}; // Enough space for FFFFFFFF + '\0'
58 uint8_t digit = hex.n % 16;
59 if (digit <= 9) *(p--) = '0' + digit;
60 else if (hex.uppercase) *(p--) = 'A' + digit - 10;
61 else *(p--) = 'a' + digit - 10;
64 printed += (int)fwrite(p + 1, sizeof(char), (size_t)(&buf[7] - p), f);
69 int _print_oct(FILE *f, oct_format_t oct) {
71 if (!oct.no_prefix) printed += fputs("0o", f);
73 oct.digits -= (oct.n == 0); // Don't need a leading zero for a zero
74 for (uint64_t n = oct.n; n > 0 && oct.digits > 0; n /= 8)
76 for (; oct.digits > 0; oct.digits -= 1)
77 printed += fputc('0', f);
79 char buf[12] = {[11] = '\0'}; // Enough space for octal UINT64_MAX + '\0'
82 *(p--) = '0' + (oct.n % 8);
85 printed += (int)fwrite(p + 1, sizeof(char), (size_t)(&buf[10] - p), f);
90 int _print_double(FILE *f, double n) {
92 int len = fpconv_dtoa(n, buf);
93 return (int)fwrite(buf, sizeof(char), (size_t)len, f);
97 int _print_hex_double(FILE *f, hex_double_t hex) {
98 if (hex.d != hex.d) return fputs("NAN", f);
99 else if (hex.d == 1.0 / 0.0) return fputs("INF", f);
100 else if (hex.d == -1.0 / 0.0) return fputs("-INF", f);
101 else if (hex.d == 0.0) return fputs("0.0", f);
106 } bits = {.d = hex.d};
108 int sign = (bits.u >> 63) & 1ull;
109 int exp = (int)((bits.u >> 52) & 0x7FF) - 1023ull;
110 uint64_t frac = bits.u & 0xFFFFFFFFFFFFFull;
115 if (sign) *p++ = '-';
119 uint64_t mantissa = (1ull << 52) | frac; // implicit 1
120 int mantissa_shift = 52;
122 while ((mantissa & 0xF) == 0 && mantissa_shift > 0) {
127 uint64_t int_part = mantissa >> mantissa_shift;
128 *p++ = "0123456789abcdef"[int_part];
132 while (mantissa_shift > 0) {
134 uint64_t digit = (mantissa >> mantissa_shift) & 0xF;
135 *p++ = "0123456789abcdef"[digit];
151 expbuf[ei--] = '0' + (exp % 10);
153 } while (exp && ei >= 0);
160 return fwrite(buf, sizeof(char), (size_t)(p - buf), f);
164 int _print_char(FILE *f, char c) {
165 #define ESC(e) "'\\" e "'"
166 const char *named[256] = {
167 ['\''] = ESC("'"), ['\\'] = ESC("\\"), ['\n'] = ESC("n"), ['\t'] = ESC("t"), ['\r'] = ESC("r"),
168 ['\033'] = ESC("e"), ['\v'] = ESC("v"), ['\a'] = ESC("a"), ['\b'] = ESC("b")};
169 const char *name = named[(uint8_t)c];
170 if (name != NULL) return fputs(name, f);
171 else if (isprint(c)) return fputc('\'', f) + fputc(c, f) + fputc('\'', f);
173 return (fputs("'\\x", f) + _print_hex(f, hex((uint64_t)c, .digits = 2, .no_prefix = true, .uppercase = true))
179 int _print_quoted(FILE *f, quoted_t quoted) {
180 #define ESC(e) "\\" e
181 const char *named[256] = {
182 ['"'] = ESC("\""), ['\\'] = ESC("\\"), ['\n'] = ESC("n"), ['\t'] = ESC("t"), ['\r'] = ESC("r"),
183 ['\033'] = ESC("e"), ['\v'] = ESC("v"), ['\a'] = ESC("a"), ['\b'] = ESC("b")};
184 int printed = fputc('"', f);
185 for (const char *p = quoted.str; *p; p++) {
186 const char *name = named[(uint8_t)*p];
188 printed += fputs(name, f);
189 } else if (isprint(*p) || (uint8_t)*p > 0x7F) {
190 printed += fputc(*p, f);
193 fputs("\\x", f) + _print_hex(f, hex((uint64_t)*p, .digits = 2, .no_prefix = true, .uppercase = true));
196 printed += fputc('"', f);
201 #if defined(__GLIBC__) && defined(_GNU_SOURCE)
202 // GLIBC has fopencookie()
203 static ssize_t _gc_stream_write(void *cookie, const char *buf, size_t size) {
204 gc_stream_t *stream = (gc_stream_t *)cookie;
205 if (stream->position + size + 1 > *stream->size)
207 GC_REALLOC(*stream->buffer, (*stream->size += MAX(MAX(16UL, *stream->size / 2UL), size + 1UL)));
208 memcpy(&(*stream->buffer)[stream->position], buf, size);
209 stream->position += size;
210 (*stream->buffer)[stream->position] = '\0';
211 return (ssize_t)size;
215 FILE *gc_memory_stream(char **buf, size_t *size) {
216 gc_stream_t *stream = GC_MALLOC(sizeof(gc_stream_t));
218 stream->buffer = buf;
220 *stream->buffer = GC_MALLOC_ATOMIC(*stream->size);
221 (*stream->buffer)[0] = '\0';
222 stream->position = 0;
223 cookie_io_functions_t functions = {.write = _gc_stream_write};
224 return fopencookie(stream, "w", functions);
226 #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
227 // BSDs have funopen() and fwopen()
228 static int _gc_stream_write(void *cookie, const char *buf, int size) {
229 gc_stream_t *stream = (gc_stream_t *)cookie;
230 if (stream->position + size + 1 > *stream->size)
232 GC_REALLOC(*stream->buffer, (*stream->size += MAX(MAX(16UL, *stream->size / 2UL), size + 1UL)));
233 memcpy(&(*stream->buffer)[stream->position], buf, size);
234 stream->position += size;
235 (*stream->buffer)[stream->position] = '\0';
240 FILE *gc_memory_stream(char **buf, size_t *size) {
241 gc_stream_t *stream = GC_MALLOC(sizeof(gc_stream_t));
243 stream->buffer = buf;
245 *stream->buffer = GC_MALLOC_ATOMIC(*stream->size);
246 (*stream->buffer)[0] = '\0';
247 stream->position = 0;
248 return fwopen(stream, _gc_stream_write);
251 #error "This platform doesn't support fopencookie() or funopen()!"