From 9faf4938b8195a0fb5d62cf1702f9f5c07523031 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 19 Nov 2014 23:17:52 -0200 Subject: Refactored key handlers. Made the logic more modular, hopefully easier to follow, and removed repeated code. Plus, some optimization in RichString code. --- RichString.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'RichString.c') diff --git a/RichString.c b/RichString.c index 34720c1b..fb2f2578 100644 --- a/RichString.c +++ b/RichString.c @@ -10,7 +10,7 @@ in the source distribution for its full text. #include #include -#define RICHSTRING_MAXLEN 300 +#define RICHSTRING_MAXLEN 350 /*{ #include "config.h" @@ -52,8 +52,8 @@ in the source distribution for its full text. typedef struct RichString_ { int chlen; - CharType chstr[RICHSTRING_MAXLEN+1]; CharType* chptr; + CharType chstr[RICHSTRING_MAXLEN+1]; } RichString; }*/ @@ -64,7 +64,7 @@ typedef struct RichString_ { #define charBytes(n) (sizeof(CharType) * (n)) -static inline void RichString_setLen(RichString* this, int len) { +static void RichString_extendLen(RichString* this, int len) { if (this->chlen <= RICHSTRING_MAXLEN) { if (len > RICHSTRING_MAXLEN) { this->chptr = malloc(charBytes(len+1)); @@ -83,6 +83,8 @@ static inline void RichString_setLen(RichString* this, int len) { this->chlen = len; } +#define RichString_setLen(this, len) do{ if(len < RICHSTRING_MAXLEN) { RichString_setChar(this,len,0); this->chlen=len; } else RichString_extendLen(this,len); }while(0) + #ifdef HAVE_LIBNCURSESW static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) { @@ -92,10 +94,11 @@ static inline void RichString_writeFrom(RichString* this, int attrs, const char* return; int newLen = from + len; RichString_setLen(this, newLen); - memset(&this->chptr[from], 0, sizeof(CharType) * (newLen - from)); for (int i = from, j = 0; i < newLen; i++, j++) { - this->chptr[i].chars[0] = data[j]; - this->chptr[i].attr = attrs; + CharType* c = &(this->chptr[i]); + c->attr = attrs; + c->chars[0] = data[j]; + c->chars[1] = 0; } this->chptr[newLen].chars[0] = 0; } @@ -171,4 +174,4 @@ void RichString_appendn(RichString* this, int attrs, const char* data, int len) void RichString_write(RichString* this, int attrs, const char* data) { RichString_writeFrom(this, attrs, data, 0, strlen(data)); -} +} \ No newline at end of file -- cgit v1.2.3