aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/strings.c
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2017-04-09 09:29:00 +0200
committerBjörn Linse <bjorn.linse@gmail.com>2017-04-10 12:02:26 +0200
commit4c857dae1169f3b8c7b6a9740f50acfd3c16858d (patch)
tree2368cea5211cd31269b2cc3c5b7e31147e9f268e /src/nvim/strings.c
parenta3a06d02489a7a43cdd7909c92c79ef7971c8663 (diff)
downloadrneovim-4c857dae1169f3b8c7b6a9740f50acfd3c16858d.tar.gz
rneovim-4c857dae1169f3b8c7b6a9740f50acfd3c16858d.tar.bz2
rneovim-4c857dae1169f3b8c7b6a9740f50acfd3c16858d.zip
vim-patch:8.0.0243
Problem: When making a character lower case with tolower() changes the byte cound, it is not made lower case. Solution: Add strlow_save(). (Dominique Pelle, closes vim/vim#1406) https://github.com/vim/vim/commit/cc5b22b3bfdc0e9e835cf7871166badda31447bd Join almost identical strup_save and strlow_save functions to one Function.
Diffstat (limited to 'src/nvim/strings.c')
-rw-r--r--src/nvim/strings.c51
1 files changed, 22 insertions, 29 deletions
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index e7c0fb8a7d..87e066d80a 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -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 = mb_toupper(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++;
+ 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;
}
return res;