From 2be33c7cbc32a245d360be404a5cc190f4ee0029 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sat, 22 Jun 2013 14:47:31 +0100 Subject: [PATCH] struct/dstr.[ch3] (dstr_putc): Accept an `int' argument. This is a rather annoying problem. It's useful to store binary data in `dstr' objects, and, indeed, characters encoded (as does) as `unsigned char' values in an `int' object; but the `char' type of the argument requires an implicit conversion, which may cause undefined behaviour, and raises warnings if called with an out-of-range constant value. --- struct/dstr.3 | 2 +- struct/dstr.c | 4 ++-- struct/dstr.h | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/struct/dstr.3 b/struct/dstr.3 index 28acb04..1cc866d 100644 --- a/struct/dstr.3 +++ b/struct/dstr.3 @@ -60,7 +60,7 @@ dstr \- a simple dynamic string type .BI "void dstr_ensure(dstr *" d ", size_t " sz ); .BI "void dstr_tidy(dstr *" d ); -.BI "void dstr_putc(dstr *" d ", char " ch ); +.BI "void dstr_putc(dstr *" d ", int " ch ); .BI "void dstr_putz(dstr *" d ); .BI "void dstr_puts(dstr *" d ", const char *" s ); .BI "int dstr_vputf(dstr *" d ", va_list *" ap ); diff --git a/struct/dstr.c b/struct/dstr.c index 3072c57..7e154ae 100644 --- a/struct/dstr.c +++ b/struct/dstr.c @@ -118,14 +118,14 @@ void dstr_ensure(dstr *d, size_t sz) /* --- @dstr_putc@ --- * * * Arguments: @dstr *d@ = pointer to a dynamic string block - * @char ch@ = character to append + * @int ch@ = character to append * * Returns: --- * * Use: Appends a character to a string. */ -void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); } +void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); } /* --- @dstr_putz@ --- * * diff --git a/struct/dstr.h b/struct/dstr.h index 3ef3051..e1851a2 100644 --- a/struct/dstr.h +++ b/struct/dstr.h @@ -141,19 +141,19 @@ extern void dstr_ensure(dstr */*d*/, size_t /*sz*/); /* --- @dstr_putc@ --- * * * Arguments: @dstr *d@ = pointer to a dynamic string block - * @char ch@ = character to append + * @int ch@ = character to append * * Returns: --- * * Use: Appends a character to a string. */ -extern void dstr_putc(dstr */*d*/, char /*ch*/); +extern void dstr_putc(dstr */*d*/, int /*ch*/); #define DPUTC(d, ch) do { \ dstr *_d = (d); \ DENSURE(_d, 1); \ - _d->buf[_d->len++] = (ch); \ + *((unsigned char *)_d->buf + _d->len++) = (ch); \ } while (0) /* --- @dstr_putz@ --- * -- 2.11.0