Rework the logic to avoid allocating memory

This commit is contained in:
LemonBoy 2015-10-02 15:02:43 +02:00
parent 2e860abd2b
commit c351c448b8

View File

@ -524,52 +524,38 @@ 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)); ptr = str;
if (tmp == NULL)
return 0;
switch (term_type) { if (term_type != TERM_TYPE_BIG5) {
case TERM_TYPE_BIG5: while (*ptr != '\0') {
big5_to_unichars(str, tmp); tmp = g_utf8_get_char(ptr);
break; len += unichar_isprint(tmp) ? mk_wcwidth(tmp) : 1;
case TERM_TYPE_UTF8:
ptr = str;
for (i = 0; i < len; i++) {
tmp[i] = g_utf8_get_char(ptr);
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);
/* Use strlen() here since we need the number of raw bytes */ /* Use strlen() here since we need the number of raw bytes */