Split RichString_(append|appendn|write) into wide and ascii

RichString_writeFrom takes a top spot during performance analysis due to the
calls to mbstowcs() and iswprint().

Most of the time we know in advance that we are only going to print regular
ASCII characters.
This commit is contained in:
Christian Göttsche
2020-12-04 14:44:57 +01:00
committed by BenBE
parent 5506925b34
commit 157086e750
26 changed files with 225 additions and 194 deletions

View File

@ -7,6 +7,7 @@ in the source distribution for its full text.
#include "RichString.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
@ -47,7 +48,7 @@ static void RichString_setLen(RichString* this, int len) {
#ifdef HAVE_LIBNCURSESW
static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) {
static inline void RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) {
wchar_t data[len + 1];
len = mbstowcs(data, data_c, len);
if (len < 0)
@ -60,6 +61,14 @@ static inline void RichString_writeFrom(RichString* this, int attrs, const char*
}
}
static inline void RichString_writeFromAscii(RichString* this, int attrs, const char* data, int from, int len) {
int newLen = from + len;
RichString_setLen(this, newLen);
for (int i = from, j = 0; i < newLen; i++, j++) {
this->chptr[i] = (CharType) { .attr = attrs & 0xffffff, .chars = { (isprint(data[j]) ? data[j] : '?') } };
}
}
inline void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
cchar_t* ch = this->chptr + start;
finish = CLAMP(finish, 0, this->chlen - 1);
@ -82,7 +91,7 @@ int RichString_findChar(RichString* this, char c, int start) {
#else /* HAVE_LIBNCURSESW */
static inline void RichString_writeFrom(RichString* this, int attrs, const char* data_c, int from, int len) {
static inline void RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) {
int newLen = from + len;
RichString_setLen(this, newLen);
for (int i = from, j = 0; i < newLen; i++, j++) {
@ -91,6 +100,10 @@ static inline void RichString_writeFrom(RichString* this, int attrs, const char*
this->chptr[newLen] = 0;
}
static inline void RichString_writeFromAscii(RichString* this, int attrs, const char* data_c, int from, int len) {
RichString_writeFromWide(this, attrs, data_c, from, len);
}
void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
chtype* ch = this->chptr + start;
finish = CLAMP(finish, 0, this->chlen - 1);
@ -132,14 +145,26 @@ void RichString_setAttr(RichString* this, int attrs) {
RichString_setAttrn(this, attrs, 0, this->chlen - 1);
}
void RichString_append(RichString* this, int attrs, const char* data) {
RichString_writeFrom(this, attrs, data, this->chlen, strlen(data));
void RichString_appendWide(RichString* this, int attrs, const char* data) {
RichString_writeFromWide(this, attrs, data, this->chlen, strlen(data));
}
void RichString_appendn(RichString* this, int attrs, const char* data, int len) {
RichString_writeFrom(this, attrs, data, this->chlen, len);
void RichString_appendnWide(RichString* this, int attrs, const char* data, int len) {
RichString_writeFromWide(this, attrs, data, this->chlen, len);
}
void RichString_write(RichString* this, int attrs, const char* data) {
RichString_writeFrom(this, attrs, data, 0, strlen(data));
void RichString_writeWide(RichString* this, int attrs, const char* data) {
RichString_writeFromWide(this, attrs, data, 0, strlen(data));
}
void RichString_appendAscii(RichString* this, int attrs, const char* data) {
RichString_writeFromAscii(this, attrs, data, this->chlen, strlen(data));
}
void RichString_appendnAscii(RichString* this, int attrs, const char* data, int len) {
RichString_writeFromAscii(this, attrs, data, this->chlen, len);
}
void RichString_writeAscii(RichString* this, int attrs, const char* data) {
RichString_writeFromAscii(this, attrs, data, 0, strlen(data));
}