diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2021-05-04 07:19:15 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2021-05-04 07:19:15 -0700 |
commit | f32073001a616fad05b04dd8451292350d572352 (patch) | |
tree | 8da6c8bbf5b131b5c815248cdb1ecafdf80c2fbb /buf.c | |
parent | 0c07bd4a499379c47916ce7cbf8d03af79e3aa98 (diff) | |
download | txr-f32073001a616fad05b04dd8451292350d572352.tar.gz txr-f32073001a616fad05b04dd8451292350d572352.tar.bz2 txr-f32073001a616fad05b04dd8451292350d572352.zip |
format: ~x/~X specifiers support buffers.
* buf.c (buf_hex): New function.
* buf.h (buf_hex): Declared.
* stream.c (formatv): Support printing of buffers in hex
via temporary buffer containing hex characters, similarly
to how bignums are handled.
* tests/018/format.tl: New file, providing some coverage over
new and affected code.
Diffstat (limited to 'buf.c')
-rw-r--r-- | buf.c | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -834,6 +834,20 @@ val buf_pprint(val buf, val stream_in) return t; } +void buf_hex(val buf, char *hex, size_t sz, int caps) +{ + val self = lit("buf-hex"); + struct buf *b = buf_handle(buf, self); + size_t i; + unsigned char *data = b->data; + const char *fmt = caps ? "%02X" : "%02x"; + + *hex = 0; + + for (i = 0; i < sz - 1; i += 2) + hex += sprintf(hex, fmt, *data++); +} + struct buf_strm { struct strm_base a; utf8_decoder_t ud; |