aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/print.h
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-04-27 16:49:38 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-04-27 16:49:38 -0400
commitbaf990e65c62f42e45fe25ac385db9536d3f1788 (patch)
tree7b89cecde1d6cd3b600b9295e0ad22a702cd1065 /src/stdlib/print.h
parent1a8a8bc3e2ebd9c282db8131902f9bb5de8c03cb (diff)
Update stdlib to use `print` instead of `printf` in all cases. This
means bringing in fpconv to do float-to-string conversion and a few updates to integer and number methods for string formatting.
Diffstat (limited to 'src/stdlib/print.h')
-rw-r--r--src/stdlib/print.h53
1 files changed, 20 insertions, 33 deletions
diff --git a/src/stdlib/print.h b/src/stdlib/print.h
index 243826e0..454ef6b8 100644
--- a/src/stdlib/print.h
+++ b/src/stdlib/print.h
@@ -8,8 +8,6 @@
// print_err(...) - print an error message and exit with EXIT_FAILURE
// String(...) - return an allocated string
//
-// If you put `#define PRINT_COLOR 1` before the import, text will be printed
-// with terminal colors.
#include <assert.h>
#include <ctype.h>
@@ -26,10 +24,6 @@
#include "datatypes.h"
#include "mapmacro.h"
-#ifndef PRINT_COLOR
-#define PRINT_COLOR 0
-#endif
-
// GCC lets you define macro-like functions which are always inlined and never
// compiled using this combination of flags. See: https://gcc.gnu.org/onlinedocs/gcc/Inline.html
#ifndef PRINT_FN
@@ -56,12 +50,6 @@ typedef struct {
#define oct(x, ...) ((oct_format_t){.n=x, __VA_ARGS__})
typedef struct {
- double n;
- int precision;
-} num_format_t;
-#define num_fmt(x, ...) ((num_format_t){.n=x, __VA_ARGS__})
-
-typedef struct {
const char *str;
} quoted_t;
#define quoted(s) ((quoted_t){s})
@@ -72,37 +60,36 @@ typedef struct {
} string_slice_t;
#define string_slice(...) ((string_slice_t){__VA_ARGS__})
+typedef struct {
+ char c;
+ int length;
+} repeated_char_t;
+#define repeated_char(ch, len) ((repeated_char_t){.c=ch, .length=len})
+
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#define FMT64 "ll"
#else
#define FMT64 "l"
#endif
-#if PRINT_COLOR
-#define hl(s) "\033[35m" s "\033[m"
-#else
-#define hl(s) s
-#endif
-PRINT_FN _print_int(FILE *f, int64_t x) { return fprintf(f, hl("%"FMT64"d"), x); }
-PRINT_FN _print_uint(FILE *f, uint64_t x) { return fprintf(f, hl("%"FMT64"u"), x); }
-PRINT_FN _print_float(FILE *f, float x) { return fprintf(f, hl("%g"), (double)x); }
-PRINT_FN _print_double(FILE *f, double x) { return fprintf(f, hl("%g"), x); }
-PRINT_FN _print_pointer(FILE *f, void *p) { return fprintf(f, hl("%p"), p); }
-PRINT_FN _print_bool(FILE *f, bool b) { return fputs(b ? hl("yes") : hl("no"), f); }
+int _print_int(FILE *f, int64_t x);
+int _print_uint(FILE *f, uint64_t x);
+int _print_double(FILE *f, double x);
+int _print_hex(FILE *f, hex_format_t hex);
+int _print_oct(FILE *f, oct_format_t oct);
+PRINT_FN _print_float(FILE *f, float x) { return _print_double(f, (double)x); }
+PRINT_FN _print_pointer(FILE *f, void *p) { return _print_hex(f, hex((uint64_t)p)); }
+PRINT_FN _print_bool(FILE *f, bool b) { return fputs(b ? "yes" : "no", f); }
PRINT_FN _print_str(FILE *f, const char *s) { return fputs(s ? s : "(null)", f); }
int _print_char(FILE *f, char c);
int _print_quoted(FILE *f, quoted_t quoted);
PRINT_FN _print_string_slice(FILE *f, string_slice_t slice) { return slice.str ? fwrite(slice.str, 1, slice.length, f) : (size_t)fputs("(null)", f); }
-PRINT_FN _print_hex(FILE *f, hex_format_t hex) {
- return fprintf(f, hex.no_prefix ? (hex.uppercase ? hl("%0*"FMT64"X") : hl("%0*"FMT64"x")) : (hex.uppercase ? hl("0x%0*"FMT64"X") : hl("%#0*"FMT64"x")), hex.digits, hex.n);
-}
-PRINT_FN _print_oct(FILE *f, oct_format_t oct) {
- return fprintf(f, oct.no_prefix ? hl("%0*"FMT64"o") : hl("%#0*"FMT64"o"), oct.digits, oct.n);
-}
-PRINT_FN _print_num_format(FILE *f, num_format_t num) {
- return fprintf(f, hl("%.*lf"), num.precision, num.n);
+PRINT_FN _print_repeated_char(FILE *f, repeated_char_t repeated) {
+ int len = 0;
+ for (int n = 0; n < repeated.length; n++)
+ len += fputc(repeated.c, f);
+ return len;
}
-#undef hl
extern int Text$print(FILE *stream, Text_t text);
extern int Path$print(FILE *stream, Path_t path);
@@ -125,9 +112,9 @@ extern int Int$print(FILE *f, Int_t i);
double: _print_double, \
hex_format_t: _print_hex, \
oct_format_t: _print_oct, \
- num_format_t: _print_num_format, \
quoted_t: _print_quoted, \
string_slice_t: _print_string_slice, \
+ repeated_char_t: _print_repeated_char, \
Text_t: Text$print, \
Path_t: Path$print, \
Int_t: Int$print, \