mirror of
https://github.com/neovim/neovim.git
synced 2025-09-16 08:18:17 +00:00
Merge branch 'master' into 1476-changes
This commit is contained in:
@@ -291,14 +291,15 @@ void vim_strup(char_u *p)
|
||||
}
|
||||
}
|
||||
|
||||
/// Make given string all upper-case
|
||||
/// Make given string all upper-case or all lower-case
|
||||
///
|
||||
/// Handels multi-byte characters as good as possible.
|
||||
/// Handles multi-byte characters as good as possible.
|
||||
///
|
||||
/// @param[in] orig Input string.
|
||||
/// @param[in] upper If true make uppercase, otherwise lowercase
|
||||
///
|
||||
/// @return [allocated] upper-cased string.
|
||||
char *strup_save(const char *const orig)
|
||||
char *strcase_save(const char *const orig, bool upper)
|
||||
FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
|
||||
{
|
||||
char *res = xstrdup(orig);
|
||||
@@ -307,33 +308,25 @@ char *strup_save(const char *const orig)
|
||||
while (*p != NUL) {
|
||||
int l;
|
||||
|
||||
if (enc_utf8) {
|
||||
int c = utf_ptr2char((const char_u *)p);
|
||||
int uc = utf_toupper(c);
|
||||
int c = utf_ptr2char((const char_u *)p);
|
||||
int uc = upper ? mb_toupper(c) : mb_tolower(c);
|
||||
|
||||
// Reallocate string when byte count changes. This is rare,
|
||||
// thus it's OK to do another malloc()/free().
|
||||
l = utf_ptr2len((const char_u *)p);
|
||||
int newl = utf_char2len(uc);
|
||||
if (newl != l) {
|
||||
// TODO(philix): use xrealloc() in strup_save()
|
||||
char *s = xmalloc(STRLEN(res) + (size_t)(1 + newl - l));
|
||||
memcpy(s, res, (size_t)(p - res));
|
||||
STRCPY(s + (p - res) + newl, p + l);
|
||||
p = s + (p - res);
|
||||
xfree(res);
|
||||
res = s;
|
||||
}
|
||||
|
||||
utf_char2bytes(uc, (char_u *)p);
|
||||
p += newl;
|
||||
} else if (has_mbyte && (l = (*mb_ptr2len)((const char_u *)p)) > 1) {
|
||||
p += l; // Skip multi-byte character.
|
||||
} else {
|
||||
// note that toupper() can be a macro
|
||||
*p = (char)(uint8_t)TOUPPER_LOC(*p);
|
||||
p++;
|
||||
// Reallocate string when byte count changes. This is rare,
|
||||
// thus it's OK to do another malloc()/free().
|
||||
l = utf_ptr2len((const char_u *)p);
|
||||
int newl = utf_char2len(uc);
|
||||
if (newl != l) {
|
||||
// TODO(philix): use xrealloc() in strup_save()
|
||||
char *s = xmalloc(STRLEN(res) + (size_t)(1 + newl - l));
|
||||
memcpy(s, res, (size_t)(p - res));
|
||||
STRCPY(s + (p - res) + newl, p + l);
|
||||
p = s + (p - res);
|
||||
xfree(res);
|
||||
res = s;
|
||||
}
|
||||
|
||||
utf_char2bytes(uc, (char_u *)p);
|
||||
p += newl;
|
||||
}
|
||||
|
||||
return res;
|
||||
|
Reference in New Issue
Block a user