summaryrefslogtreecommitdiff
path: root/src/fe-text/term-terminfo.c
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2015-10-02 15:02:43 +0200
committerLemonBoy <thatlemon@gmail.com>2015-10-02 15:02:43 +0200
commitc351c448b8dd2b9759e46c0c6e73ed5ead936ffc (patch)
treea175bd3de3c6876568545c2c92bd7fe820968eda /src/fe-text/term-terminfo.c
parent2e860abd2b8b3a30f74005450830c34a318b64a3 (diff)
downloadirssi-c351c448b8dd2b9759e46c0c6e73ed5ead936ffc.zip
Rework the logic to avoid allocating memory
Diffstat (limited to 'src/fe-text/term-terminfo.c')
-rw-r--r--src/fe-text/term-terminfo.c52
1 files changed, 19 insertions, 33 deletions
diff --git a/src/fe-text/term-terminfo.c b/src/fe-text/term-terminfo.c
index 8c95bc0d..2ee69a1c 100644
--- a/src/fe-text/term-terminfo.c
+++ b/src/fe-text/term-terminfo.c
@@ -524,52 +524,38 @@ void term_add_unichar(TERM_WINDOW *window, unichar chr)
int term_addstr(TERM_WINDOW *window, const char *str)
{
- int i, len, raw_len;
- unichar *tmp;
+ int len, raw_len;
+ unichar tmp;
const char *ptr;
if (vcmove) term_move_real();
+ len = 0;
raw_len = strlen(str);
/* 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;
+ ptr = str;
- switch (term_type) {
- case TERM_TYPE_BIG5:
- big5_to_unichars(str, tmp);
- break;
- case TERM_TYPE_UTF8:
- 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);
}
- break;
- default:
- for (i = 0; i < len; i++)
- tmp[i] = str[i];
+ } else {
+ while (*ptr != '\0') {
+ if (is_big5(ptr[0], ptr[1])) {
+ 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);
/* Use strlen() here since we need the number of raw bytes */