chars: make the UTF-8 case ever so slightly faster by eliding an 'if'
And in the bargain get rid of some duplicate code. This makes a binary without UTF-8 support slightly slower, but that's not important -- it is more than fast enough anyway. Important is that the most used and longest code path, the UTF-8 case, becomes faster. Note that 'is_cntrl_mbchar()' will fall back to 'is_cntrl_char()' for a non-UTF-8 build, so the deleted piece of code really was equivalent with the remaining piece for that case.master
parent
f456794045
commit
035b91cb15
30
src/chars.c
30
src/chars.c
|
@ -292,10 +292,9 @@ char *make_mbchar(long chr, int *chr_mb_len)
|
|||
* col isn't NULL, add the character's width (in columns) to it. */
|
||||
int parse_mbchar(const char *buf, char *chr, size_t *col)
|
||||
{
|
||||
#ifdef ENABLE_UTF8
|
||||
if (use_utf8) {
|
||||
int length;
|
||||
|
||||
#ifdef ENABLE_UTF8
|
||||
/* If this is a UTF-8 starter byte, get the number of bytes of the character. */
|
||||
if ((signed char)*buf < 0) {
|
||||
length = mblen(buf, MAXCHARLEN);
|
||||
|
@ -304,6 +303,7 @@ int parse_mbchar(const char *buf, char *chr, size_t *col)
|
|||
if (length <= 0)
|
||||
length = 1;
|
||||
} else
|
||||
#endif
|
||||
length = 1;
|
||||
|
||||
/* When requested, store the multibyte character in chr. */
|
||||
|
@ -327,35 +327,13 @@ int parse_mbchar(const char *buf, char *chr, size_t *col)
|
|||
/* If we have a normal character, get its width normally. */
|
||||
} else if (length == 1)
|
||||
*col += 1;
|
||||
#ifdef ENABLE_UTF8
|
||||
else
|
||||
*col += mbwidth(buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
return length;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
/* When requested, store the byte character in chr. */
|
||||
if (chr != NULL)
|
||||
*chr = *buf;
|
||||
|
||||
/* When requested, add the width of the character to col. */
|
||||
if (col != NULL) {
|
||||
/* If we have a tab, compute its width in columns using the
|
||||
* current value of col. */
|
||||
if (*buf == '\t')
|
||||
*col += tabsize - *col % tabsize;
|
||||
/* If we have a control character, it's two columns wide: one
|
||||
* column for the "^", and one for the visible character. */
|
||||
else if (is_cntrl_char((unsigned char)*buf))
|
||||
*col += 2;
|
||||
/* If we have a normal character, it's one column wide. */
|
||||
else
|
||||
(*col)++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the index in buf of the beginning of the multibyte character
|
||||
|
|
Loading…
Reference in New Issue