]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
tuklib_mbstr_width: Add tuklib_mbstr_width_mem()
authorLasse Collin <lasse.collin@tukaani.org>
Mon, 21 Oct 2024 15:51:24 +0000 (18:51 +0300)
committerLasse Collin <lasse.collin@tukaani.org>
Wed, 18 Dec 2024 15:09:30 +0000 (17:09 +0200)
It's a new function split from tuklib_mbstr_width().
It's useful with partial strings that aren't terminated with \0.

src/common/tuklib_mbstr.h
src/common/tuklib_mbstr_width.c

index 3ab0a82aad7dca6b8834375eca1870bec430e426..5ac06eb35e88ed1259d37ab834358bc0eb3d89cc 100644 (file)
@@ -37,6 +37,23 @@ extern size_t tuklib_mbstr_width(const char *str, size_t *bytes);
 ///             (size_t)-1 is returned. Possible errors include invalid,
 ///             partial, or non-printable multibyte character in str.
 
+#define tuklib_mbstr_width_mem TUKLIB_SYMBOL(tuklib_mbstr_width_mem)
+extern size_t tuklib_mbstr_width_mem(const char *str, size_t len);
+///<
+/// \brief      Get the number of columns needed for the multibyte buffer
+///
+/// This is like tuklib_mbstr_width() except that this takes the buffer
+/// length in bytes as the second argument. This allows using the function
+/// for buffers that aren't terminated with '\0'.
+///
+/// \param      str         String whose width is to be calculated.
+/// \param      len         Number of bytes to read from str.
+///
+/// \return     On success, the number of columns needed to display the
+///             string e.g. in a terminal emulator is returned. On error,
+///             (size_t)-1 is returned. Possible errors include invalid,
+///             partial, or non-printable multibyte character in str.
+
 #define tuklib_mbstr_fw TUKLIB_SYMBOL(tuklib_mbstr_fw)
 extern int tuklib_mbstr_fw(const char *str, int columns_min);
 ///<
index f003aed11a3b594398918d1ccdd6b7ff45c03330..98c611d8f38d02bbeb0f66c73b62156533d05b9c 100644 (file)
@@ -24,9 +24,17 @@ tuklib_mbstr_width(const char *str, size_t *bytes)
        if (bytes != NULL)
                *bytes = len;
 
+       return tuklib_mbstr_width_mem(str, len);
+}
+
+
+extern size_t
+tuklib_mbstr_width_mem(const char *str, size_t len)
+{
 #ifndef HAVE_MBRTOWC
        // In single-byte mode, the width of the string is the same
        // as its length.
+       (void)str;
        return len;
 
 #else