From 721aaa28aaf88fd648534e24fd1778ac199ed9ef Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 16 Jan 2025 01:06:57 +0100 Subject: [PATCH] Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. --- lib/std/io/formatter.c3 | 33 +++++++++++++++++++++++++++++++++ lib/std/io/formatter_private.c3 | 16 ++++++++++++++++ releasenotes.md | 11 +++++++++++ src/version.h | 4 ++-- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/std/io/formatter.c3 b/lib/std/io/formatter.c3 index b737cabe2..95f9ce988 100644 --- a/lib/std/io/formatter.c3 +++ b/lib/std/io/formatter.c3 @@ -476,6 +476,39 @@ fn usz! Formatter.vprintf(&self, String format, any[] anys) case 'c': total_len += self.out_char(current)!; continue; + case 'H': + self.flags.uppercase = true; + nextcase; + case 'h': + char[] out @noinit; + switch (current) + { + case char[]: + out = *current; + case ichar[]: + out = *(char[]*)current; + default: + if (current.type.kindof == ARRAY && (current.type.inner == char.typeid || current.type.inner == ichar.typeid)) + { + out = ((char*)current.ptr)[:current.type.sizeof]; + break; + } + total_len += self.out_substr("")!; + continue; + } + if (self.flags.left) + { + usz len = print_hex_chars(self, out, self.flags.uppercase)!; + total_len += len; + total_len += self.pad(' ', self.width, len)!; + continue; + } + if (self.width) + { + total_len += self.pad(' ', self.width, out.len * 2)!; + } + total_len += print_hex_chars(self, out, self.flags.uppercase)!; + continue; case 's': if (self.flags.left) { diff --git a/lib/std/io/formatter_private.c3 b/lib/std/io/formatter_private.c3 index 7d78ef476..503519371 100644 --- a/lib/std/io/formatter_private.c3 +++ b/lib/std/io/formatter_private.c3 @@ -9,6 +9,22 @@ fault FormattingFault BAD_FORMAT } +fn usz! print_hex_chars(Formatter* f, char[] out, bool uppercase) @inline +{ + char past_10 = (uppercase ? 'A' : 'a') - 10; + usz len = 0; + foreach (c : out) + { + char digit = c >> 4; + f.out(digit + (digit < 10 ? '0' : past_10))!; + len++; + digit = c & 0xf; + f.out(digit + (digit < 10 ? '0' : past_10))!; + len++; + } + return len; +} + macro Formatter.first_err(&self, anyfault f) { if (self.first_fault) return self.first_fault; diff --git a/releasenotes.md b/releasenotes.md index 11790d998..9be7e2de8 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -1,5 +1,16 @@ # C3C Release Notes +## 0.6.7 Change list + +### Changes / improvements +- None yet. + +### Fixes +- None yet. + +### Stdlib changes +- Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. + ## 0.6.6 Change list ### Changes / improvements diff --git a/src/version.h b/src/version.h index c3e482706..3debf3987 100644 --- a/src/version.h +++ b/src/version.h @@ -1,2 +1,2 @@ -#define COMPILER_VERSION "0.6.6" -#define PRERELEASE 0 \ No newline at end of file +#define COMPILER_VERSION "0.6.7" +#define PRERELEASE 1 \ No newline at end of file