mirror of
https://github.com/irssi/irssi.git
synced 2025-05-03 08:51:19 -05:00
Rework the logic to avoid allocating memory
This commit is contained in:
parent
2e860abd2b
commit
c351c448b8
@ -524,51 +524,37 @@ void term_add_unichar(TERM_WINDOW *window, unichar chr)
|
|||||||
|
|
||||||
int term_addstr(TERM_WINDOW *window, const char *str)
|
int term_addstr(TERM_WINDOW *window, const char *str)
|
||||||
{
|
{
|
||||||
int i, len, raw_len;
|
int len, raw_len;
|
||||||
unichar *tmp;
|
unichar tmp;
|
||||||
const char *ptr;
|
const char *ptr;
|
||||||
|
|
||||||
if (vcmove) term_move_real();
|
if (vcmove) term_move_real();
|
||||||
|
|
||||||
|
len = 0;
|
||||||
raw_len = strlen(str);
|
raw_len = strlen(str);
|
||||||
|
|
||||||
/* The string length depends on the terminal encoding */
|
/* The string length depends on the terminal encoding */
|
||||||
switch (term_type) {
|
|
||||||
case TERM_TYPE_BIG5:
|
|
||||||
len = strlen_big5((const unsigned char *)str);
|
|
||||||
break;
|
|
||||||
case TERM_TYPE_UTF8:
|
|
||||||
len = g_utf8_strlen(str, -1);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
len = strlen(str);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = calloc(len, sizeof(unichar));
|
|
||||||
if (tmp == NULL)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
switch (term_type) {
|
|
||||||
case TERM_TYPE_BIG5:
|
|
||||||
big5_to_unichars(str, tmp);
|
|
||||||
break;
|
|
||||||
case TERM_TYPE_UTF8:
|
|
||||||
ptr = str;
|
ptr = str;
|
||||||
for (i = 0; i < len; i++) {
|
|
||||||
tmp[i] = g_utf8_get_char(ptr);
|
if (term_type != TERM_TYPE_BIG5) {
|
||||||
|
while (*ptr != '\0') {
|
||||||
|
tmp = g_utf8_get_char(ptr);
|
||||||
|
len += unichar_isprint(tmp) ? mk_wcwidth(tmp) : 1;
|
||||||
ptr = g_utf8_next_char(ptr);
|
ptr = g_utf8_next_char(ptr);
|
||||||
}
|
}
|
||||||
break;
|
} else {
|
||||||
default:
|
while (*ptr != '\0') {
|
||||||
for (i = 0; i < len; i++)
|
if (is_big5(ptr[0], ptr[1])) {
|
||||||
tmp[i] = str[i];
|
tmp = ptr[0] << 8 | ptr[1];
|
||||||
|
ptr += 2;
|
||||||
|
} else {
|
||||||
|
tmp = *ptr;
|
||||||
|
ptr += 1;
|
||||||
|
}
|
||||||
|
len += (tmp > 0xff) ? 2 : 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (len = i = 0; i < len; i++)
|
|
||||||
len += unichar_isprint(tmp[i]) ? mk_wcwidth(tmp[i]) : 1;
|
|
||||||
|
|
||||||
free(tmp);
|
|
||||||
|
|
||||||
term_printed_text(len);
|
term_printed_text(len);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user