tweaks: write two pieces of conditionalized code like all others

Also trim or improve a few comments.
This commit is contained in:
Benno Schulenberg 2016-12-15 19:46:16 +01:00
parent 9765c2faa0
commit c5f49167ea

View File

@ -320,21 +320,21 @@ int mbwidth(const char *c)
return 1; return 1;
} }
/* Return the maximum width in bytes of a multibyte character. */ /* Return the maximum length (in bytes) of a character. */
int mb_cur_max(void) int mb_cur_max(void)
{ {
return
#ifdef ENABLE_UTF8 #ifdef ENABLE_UTF8
use_utf8 ? MB_CUR_MAX : if (use_utf8)
return MB_CUR_MAX;
else
#endif #endif
1; return 1;
} }
/* Convert the Unicode value in chr to a multibyte character with the /* Convert the Unicode value in chr to a multibyte character, if possible.
* same wide character value as chr, if possible. If the conversion * If the conversion succeeds, return the (dynamically allocated) multibyte
* succeeds, return the (dynamically allocated) multibyte character and * character and its length. Otherwise, return an undefined (dynamically
* its length. Otherwise, return an undefined (dynamically allocated) * allocated) multibyte character and a length of zero. */
* multibyte character and a length of zero. */
char *make_mbchar(long chr, int *chr_mb_len) char *make_mbchar(long chr, int *chr_mb_len)
{ {
char *chr_mb; char *chr_mb;
@ -363,8 +363,7 @@ char *make_mbchar(long chr, int *chr_mb_len)
/* Parse a multibyte character from buf. Return the number of bytes /* Parse a multibyte character from buf. Return the number of bytes
* used. If chr isn't NULL, store the multibyte character in it. If * used. If chr isn't NULL, store the multibyte character in it. If
* col isn't NULL, store the new display width in it. If *buf is '\t', * col isn't NULL, add the character's width (in columns) to it. */
* we expect col to have the current display width. */
int parse_mbchar(const char *buf, char *chr, size_t *col) int parse_mbchar(const char *buf, char *chr, size_t *col)
{ {
int length; int length;
@ -390,9 +389,9 @@ int parse_mbchar(const char *buf, char *chr, size_t *col)
chr[i] = buf[i]; chr[i] = buf[i];
} }
/* When requested, store the width of the wide character in col. */ /* When requested, add the width of the character to col. */
if (col != NULL) { if (col != NULL) {
/* If we have a tab, get its width in columns using the /* If we have a tab, compute its width in columns based on the
* current value of col. */ * current value of col. */
if (*buf == '\t') if (*buf == '\t')
*col += tabsize - *col % tabsize; *col += tabsize - *col % tabsize;
@ -400,8 +399,7 @@ int parse_mbchar(const char *buf, char *chr, size_t *col)
* column for the "^", and one for the visible character. */ * column for the "^", and one for the visible character. */
else if (is_cntrl_mbchar(buf)) { else if (is_cntrl_mbchar(buf)) {
*col += 2; *col += 2;
/* If we have a normal character, get its width in columns /* If we have a normal character, get its width normally. */
* normally. */
} else } else
*col += mbwidth(buf); *col += mbwidth(buf);
} }
@ -415,9 +413,9 @@ int parse_mbchar(const char *buf, char *chr, size_t *col)
if (chr != NULL) if (chr != NULL)
*chr = *buf; *chr = *buf;
/* When requested, store the width of the wide character in col. */ /* When requested, add the width of the character to col. */
if (col != NULL) { if (col != NULL) {
/* If we have a tab, get its width in columns using the /* If we have a tab, compute its width in columns using the
* current value of col. */ * current value of col. */
if (*buf == '\t') if (*buf == '\t')
*col += tabsize - *col % tabsize; *col += tabsize - *col % tabsize;
@ -908,10 +906,11 @@ bool is_valid_mbstring(const char *s)
{ {
assert(s != NULL); assert(s != NULL);
return
#ifdef ENABLE_UTF8 #ifdef ENABLE_UTF8
use_utf8 ? (mbstowcs(NULL, s, 0) != (size_t)-1) : if (use_utf8)
return (mbstowcs(NULL, s, 0) != (size_t)-1);
else
#endif #endif
TRUE; return TRUE;
} }
#endif /* !DISABLE_NANORC */ #endif /* !DISABLE_NANORC */