eliminate static buffer with custom resizing code.

This commit is contained in:
christos 2016-05-02 16:48:34 +00:00
parent 9ff2bfe491
commit 300e2ca473
5 changed files with 43 additions and 47 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: chartype.c,v 1.28 2016/04/11 18:56:31 christos Exp $ */
/* $NetBSD: chartype.c,v 1.29 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -31,7 +31,7 @@
*/
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
__RCSID("$NetBSD: chartype.c,v 1.28 2016/04/11 18:56:31 christos Exp $");
__RCSID("$NetBSD: chartype.c,v 1.29 2016/05/02 16:48:34 christos Exp $");
#endif /* not lint && not SCCSID */
#include <ctype.h>
@ -211,51 +211,44 @@ ct_encode_char(char *dst, size_t len, wchar_t c)
}
protected const wchar_t *
ct_visual_string(const wchar_t *s)
ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
{
static wchar_t *buff = NULL;
static size_t buffsize = 0;
void *p;
wchar_t *dst;
ssize_t used = 0;
ssize_t used;
if (!s)
return NULL;
if (!buff) {
buffsize = CT_BUFSIZ;
buff = el_malloc(buffsize * sizeof(*buff));
}
dst = buff;
if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
return NULL;
used = 0;
dst = conv->wbuff;
while (*s) {
used = ct_visual_char(dst, buffsize - (size_t)(dst - buff), *s);
if (used == -1) { /* failed to encode, need more buffer space */
used = dst - buff;
buffsize += CT_BUFSIZ;
p = el_realloc(buff, buffsize * sizeof(*buff));
if (p == NULL)
goto out;
buff = p;
dst = buff + used;
/* don't increment s here - we want to retry it! */
used = ct_visual_char(dst,
conv->wsize - (size_t)(dst - conv->wbuff), *s);
if (used != -1) {
++s;
dst += used;
continue;
}
else
++s;
dst += used;
/* failed to encode, need more buffer space */
used = dst - conv->wbuff;
if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
return NULL;
dst = conv->wbuff + used;
}
if (dst >= (buff + buffsize)) { /* sigh */
buffsize += 1;
p = el_realloc(buff, buffsize * sizeof(*buff));
if (p == NULL)
goto out;
buff = p;
dst = buff + buffsize - 1;
if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */
used = dst - conv->wbuff;
if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
return NULL;
dst = conv->wbuff + used;
}
*dst = 0;
return buff;
out:
el_free(buff);
buffsize = 0;
return NULL;
*dst = L'\0';
return conv->wbuff;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: chartype.h,v 1.32 2016/05/02 16:35:17 christos Exp $ */
/* $NetBSD: chartype.h,v 1.33 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -95,7 +95,7 @@ protected ssize_t ct_visual_char(wchar_t *, size_t, wchar_t);
/* Convert the given string into visual format, using the ct_visual_char()
* function. Uses a static buffer, so not threadsafe. */
protected const wchar_t *ct_visual_string(const wchar_t *);
protected const wchar_t *ct_visual_string(const wchar_t *, ct_buffer_t *);
/* printable character, use ct_visual_width() to find out display width */

View File

@ -1,4 +1,4 @@
/* $NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $ */
/* $NetBSD: el.c,v 1.90 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
#else
__RCSID("$NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $");
__RCSID("$NetBSD: el.c,v 1.90 2016/05/02 16:48:34 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
@ -147,6 +147,8 @@ el_end(EditLine *el)
sig_end(el);
el_free(el->el_prog);
el_free(el->el_visual.cbuff);
el_free(el->el_visual.wbuff);
el_free(el->el_scratch.cbuff);
el_free(el->el_scratch.wbuff);
el_free(el->el_lgcyconv.cbuff);

View File

@ -1,4 +1,4 @@
/* $NetBSD: el.h,v 1.38 2016/04/19 19:50:53 christos Exp $ */
/* $NetBSD: el.h,v 1.39 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -133,6 +133,7 @@ struct editline {
el_search_t el_search; /* Search stuff */
el_signal_t el_signal; /* Signal handling stuff */
struct el_read_t *el_read; /* Character reading stuff */
ct_buffer_t el_visual; /* Buffer for displayable str */
ct_buffer_t el_scratch; /* Scratch conversion buffer */
ct_buffer_t el_lgcyconv; /* Buffer for legacy wrappers */
LineInfo el_lgcylinfo; /* Legacy LineInfo buffer */

View File

@ -1,4 +1,4 @@
/* $NetBSD: terminal.c,v 1.30 2016/04/18 17:01:19 christos Exp $ */
/* $NetBSD: terminal.c,v 1.31 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
#else
__RCSID("$NetBSD: terminal.c,v 1.30 2016/04/18 17:01:19 christos Exp $");
__RCSID("$NetBSD: terminal.c,v 1.31 2016/05/02 16:48:34 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
@ -1316,8 +1316,8 @@ terminal_telltc(EditLine *el, int argc __attribute__((__unused__)),
const char *ub;
if (*ts && **ts) {
ub = ct_encode_string(ct_visual_string(
ct_decode_string(*ts, &el->el_scratch)),
&el->el_scratch);
ct_decode_string(*ts, &el->el_scratch),
&el->el_visual), &el->el_scratch);
} else {
ub = "(empty)";
}