tweaks: move a function to before its callers and next to its kind

Also, improve the indentation of two random lines.
This commit is contained in:
Benno Schulenberg 2019-10-03 11:20:48 +02:00
parent 5398d986ef
commit 31ff7ead73
3 changed files with 24 additions and 24 deletions

View File

@ -265,6 +265,27 @@ int char_length(const char *pointer)
return 1; return 1;
} }
/* Return the number of (multibyte) characters in the given string. */
size_t mbstrlen(const char *pointer)
{
size_t count = 0;
while (*pointer != '\0') {
#ifdef ENABLE_UTF8
if ((signed char)*pointer < 0) {
int length = mblen(pointer, MAXCHARLEN);
pointer += (length < 0 ? 1 : length);
} else
#endif
pointer++;
count++;
}
return count;
}
/* 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, add the character's width (in columns) to it. */ * col isn't NULL, add the character's width (in columns) to it. */
@ -347,7 +368,7 @@ size_t step_left(const char *buf, size_t pos)
return before - charlen; return before - charlen;
} else } else
#endif #endif
return (pos == 0 ? 0 : pos - 1); return (pos == 0 ? 0 : pos - 1);
} }
/* Return the index in buf of the beginning of the multibyte character /* Return the index in buf of the beginning of the multibyte character
@ -488,27 +509,6 @@ char *mbrevstrcasestr(const char *haystack, const char *needle,
return revstrcasestr(haystack, needle, pointer); return revstrcasestr(haystack, needle, pointer);
} }
/* Count the number of (multibyte) characters in the given string. */
size_t mbstrlen(const char *pointer)
{
size_t count = 0;
while (*pointer != '\0') {
#ifdef ENABLE_UTF8
if ((signed char)*pointer < 0) {
int length = mblen(pointer, MAXCHARLEN);
pointer += (length < 0 ? 1 : length);
} else
#endif
pointer++;
count++;
}
return count;
}
#if !defined(NANO_TINY) || defined(ENABLE_JUSTIFY) #if !defined(NANO_TINY) || defined(ENABLE_JUSTIFY)
/* This function is equivalent to strchr() for multibyte strings. */ /* This function is equivalent to strchr() for multibyte strings. */
char *mbstrchr(const char *string, const char *chr) char *mbstrchr(const char *string, const char *chr)

View File

@ -218,6 +218,7 @@ int mbwidth(const char *c);
#endif #endif
char *make_mbchar(long chr, int *chr_mb_len); char *make_mbchar(long chr, int *chr_mb_len);
int char_length(const char *pointer); int char_length(const char *pointer);
size_t mbstrlen(const char *s);
int parse_mbchar(const char *buf, char *chr, size_t *col); int parse_mbchar(const char *buf, char *chr, size_t *col);
size_t step_left(const char *buf, size_t pos); size_t step_left(const char *buf, size_t pos);
size_t step_right(const char *buf, size_t pos); size_t step_right(const char *buf, size_t pos);
@ -226,7 +227,6 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n);
char *mbstrcasestr(const char *haystack, const char *needle); char *mbstrcasestr(const char *haystack, const char *needle);
char *revstrstr(const char *haystack, const char *needle, const char *index); char *revstrstr(const char *haystack, const char *needle, const char *index);
char *mbrevstrcasestr(const char *haystack, const char *needle, const char *index); char *mbrevstrcasestr(const char *haystack, const char *needle, const char *index);
size_t mbstrlen(const char *s);
#if !defined(NANO_TINY) || defined(ENABLE_JUSTIFY) #if !defined(NANO_TINY) || defined(ENABLE_JUSTIFY)
char *mbstrchr(const char *s, const char *c); char *mbstrchr(const char *s, const char *c);
#endif #endif

View File

@ -1984,7 +1984,7 @@ char *display_string(const char *buf, size_t column, size_t span,
/* If there is more text than can be shown, make room for the ">". */ /* If there is more text than can be shown, make room for the ">". */
if (column > beyond || (*buf != '\0' && (isprompt || if (column > beyond || (*buf != '\0' && (isprompt ||
(isdata && !ISSET(SOFTWRAP))))) { (isdata && !ISSET(SOFTWRAP))))) {
#ifdef ENABLE_UTF8 #ifdef ENABLE_UTF8
do { do {
index = step_left(converted, index); index = step_left(converted, index);