diff options
author | ZyX <kp-pav@yandex.ru> | 2017-04-10 22:21:06 +0300 |
---|---|---|
committer | ZyX <kp-pav@yandex.ru> | 2017-04-10 22:21:06 +0300 |
commit | ab4d13e2fa09db538756cc5cfb36f81681e5ff7a (patch) | |
tree | b8bded415655f09bc323a23d21ed8d7e74b1c8da /src/nvim/strings.c | |
parent | a40a969e9a4776f1e274dcf0e59c8f1ec1770ca0 (diff) | |
parent | 9aface8c4d1edd25d4fed3e099e3c2c02b0a282a (diff) | |
download | rneovim-ab4d13e2fa09db538756cc5cfb36f81681e5ff7a.tar.gz rneovim-ab4d13e2fa09db538756cc5cfb36f81681e5ff7a.tar.bz2 rneovim-ab4d13e2fa09db538756cc5cfb36f81681e5ff7a.zip |
Merge branch 'master' into luaviml'/lua
Diffstat (limited to 'src/nvim/strings.c')
-rw-r--r-- | src/nvim/strings.c | 51 |
1 files changed, 22 insertions, 29 deletions
diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 5dcffe00e0..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 = utf_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; |