mbyte: Refactor mb_unescape

Does not alter its usages.
This commit is contained in:
ZyX 2017-07-02 18:05:54 +03:00
parent ca4633bfe4
commit ac086d8ce2

View File

@ -1739,52 +1739,55 @@ int mb_charlen_len(char_u *str, int len)
return count; return count;
} }
/* /// Try to unescape a multibyte character
* Try to un-escape a multi-byte character. ///
* Used for the "to" and "from" part of a mapping. /// Used for the rhs and lhs of the mappings.
* Return the un-escaped string if it is a multi-byte character, and advance ///
* "pp" to just after the bytes that formed it. /// @param[in,out] pp String to unescape. Is advanced to just after the bytes
* Return NULL if no multi-byte char was found. /// that form a multibyte character.
*/ ///
char_u * mb_unescape(char_u **pp) /// @return Unescaped string if it is a multibyte character, NULL if no
/// multibyte character was found. Returns a static buffer, always one
/// and the same.
const char *mb_unescape(const char **const pp)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{ {
static char_u buf[6]; static char buf[6];
int n; size_t buf_idx = 0;
int m = 0; uint8_t *str = (uint8_t *)(*pp);
char_u *str = *pp;
/* Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI // Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI
* KS_EXTRA KE_CSI to CSI. // KS_EXTRA KE_CSI to CSI.
* Maximum length of a utf-8 character is 4 bytes. */ // Maximum length of a utf-8 character is 4 bytes.
for (n = 0; str[n] != NUL && m < 4; ++n) { for (size_t str_idx = 0; str[str_idx] != NUL && buf_idx < 4; str_idx++) {
if (str[n] == K_SPECIAL if (str[str_idx] == K_SPECIAL
&& str[n + 1] == KS_SPECIAL && str[str_idx + 1] == KS_SPECIAL
&& str[n + 2] == KE_FILLER) { && str[str_idx + 2] == KE_FILLER) {
buf[m++] = K_SPECIAL; buf[buf_idx++] = (char)K_SPECIAL;
n += 2; str_idx += 2;
} else if ((str[n] == K_SPECIAL } else if ((str[str_idx] == K_SPECIAL)
) && str[str_idx + 1] == KS_EXTRA
&& str[n + 1] == KS_EXTRA && str[str_idx + 2] == KE_CSI) {
&& str[n + 2] == (int)KE_CSI) { buf[buf_idx++] = (char)CSI;
buf[m++] = CSI; str_idx += 2;
n += 2; } else if (str[str_idx] == K_SPECIAL) {
} else if (str[n] == K_SPECIAL break; // A special key can't be a multibyte char.
) } else {
break; /* a special key can't be a multibyte char */ buf[buf_idx++] = (char)str[str_idx];
else }
buf[m++] = str[n]; buf[buf_idx] = NUL;
buf[m] = NUL;
/* Return a multi-byte character if it's found. An illegal sequence // Return a multi-byte character if it's found. An illegal sequence
* will result in a 1 here. */ // will result in a 1 here.
if ((*mb_ptr2len)(buf) > 1) { if (utf_ptr2len((const char_u *)buf) > 1) {
*pp = str + n + 1; *pp = (const char *)str + buf_idx + 1;
return buf; return buf;
} }
/* Bail out quickly for ASCII. */ // Bail out quickly for ASCII.
if (buf[0] < 128) if ((uint8_t)buf[0] < 128) {
break; break;
}
} }
return NULL; return NULL;
} }