Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter.

This commit is contained in:
Christoffer Lerno
2025-01-16 01:06:57 +01:00
parent 15503a9054
commit 721aaa28aa
4 changed files with 62 additions and 2 deletions

View File

@@ -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("<INVALID>")!;
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)
{

View File

@@ -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;

View File

@@ -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

View File

@@ -1,2 +1,2 @@
#define COMPILER_VERSION "0.6.6"
#define PRERELEASE 0
#define COMPILER_VERSION "0.6.7"
#define PRERELEASE 1