X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/b2cc954248dff07883ef749c1c2851dd07e0d400..df646add9dde90130753c06938e425c8f8f39c0f:/sbr/charstring.c diff --git a/sbr/charstring.c b/sbr/charstring.c index d007e7b5..589b000e 100644 --- a/sbr/charstring.c +++ b/sbr/charstring.c @@ -1,5 +1,4 @@ -/* - * charstring -- dynamically-sized char array that can report size +/* charstring.c -- dynamically-sized char array that can report size * in both characters and bytes * * This code is Copyright (c) 2014, by the authors of nmh. See the @@ -42,8 +41,9 @@ charstring_reserve (charstring_t s, size_t need) { */ charstring_t charstring_create (size_t max) { - charstring_t s = mh_xmalloc (sizeof *s); + charstring_t s; + NEW(s); s->max = NMH_MAX_CHARWIDTH * (max ? max : CHARSTRING_DEFAULT_SIZE); s->cur = s->buffer = mh_xmalloc (s->max); s->chars = 0; @@ -54,8 +54,9 @@ charstring_create (size_t max) { charstring_t charstring_copy (const charstring_t src) { const size_t num = src->cur - src->buffer; - charstring_t s = mh_xmalloc (sizeof *s); + charstring_t s; + NEW(s); s->max = src->max; s->buffer = mh_xmalloc (s->max); memcpy (s->buffer, src->buffer, num); @@ -102,13 +103,25 @@ charstring_append (charstring_t dest, const charstring_t src) { const size_t num = src->cur - src->buffer; if (num > 0) { - charstring_reserve (dest, num + (dest->cur - dest->buffer)); + charstring_reserve (dest, dest->cur - dest->buffer + num); memcpy (dest->cur, src->buffer, num); dest->cur += num; dest->chars += src->chars; } } +void +charstring_append_cstring (charstring_t dest, const char src[]) { + const size_t num = strlen (src); + + if (num > 0) { + charstring_reserve (dest, dest->cur - dest->buffer + num); + memcpy (dest->cur, src, num); /* Exclude src's trailing newline. */ + dest->cur += num; + dest->chars += num; + } +} + void charstring_clear (charstring_t s) { s->cur = s->buffer; @@ -134,14 +147,12 @@ charstring_buffer (const charstring_t s) { char * charstring_buffer_copy (const charstring_t s) { - char *copy = strdup (charstring_buffer (s)); + char *copy = mh_xmalloc (s->cur - s->buffer + 1); - if (copy) { - return copy; - } else { - advise ("strdup", "unable to copy charstring buffer"); - return NULL; - } + /* Use charstring_buffer() to null terminate the buffer. */ + memcpy (copy, charstring_buffer (s), s->cur - s->buffer + 1); + + return copy; } size_t @@ -169,8 +180,8 @@ charstring_last_char_len (const charstring_t s) { len = mbtowc (&wide_char, sp, (size_t) MB_CUR_MAX < remaining ? (size_t) MB_CUR_MAX : remaining); - sp += len > 0 ? len : 1; - remaining -= len > 0 ? len : 1; + sp += max(len, 1); + remaining -= max(len, 1); } #else /* ! MULTIBYTE_SUPPORT */ if (charstring_bytes (s) > 0) { len = 1; }