refactor: move reverse_text to strings.c as it's a string operation

Also add tests for reverse_text.

Co-authored-by: Kalle Ranki <kalle.ranki@gmail.com>
This commit is contained in:
Dundar Goc
2022-05-20 11:54:39 +02:00
parent 1d160a76ec
commit 1a0de90068
4 changed files with 80 additions and 25 deletions

View File

@@ -1507,3 +1507,24 @@ int kv_do_printf(StringBuilder *str, const char *fmt, ...)
str->size += (size_t)printed;
return printed;
}
/// Reverse text into allocated memory.
///
/// @return the allocated string.
char_u *reverse_text(char_u *s)
FUNC_ATTR_NONNULL_RET
{
// Reverse the pattern.
size_t len = STRLEN(s);
char_u *rev = xmalloc(len + 1);
size_t rev_i = len;
for (size_t s_i = 0; s_i < len; s_i++) {
const int mb_len = utfc_ptr2len((char *)s + s_i);
rev_i -= (size_t)mb_len;
memmove(rev + rev_i, s + s_i, (size_t)mb_len);
s_i += (size_t)mb_len - 1;
}
rev[len] = NUL;
return rev;
}