liblzma: CRC CLMUL: Omit is_arch_extension_supported() when not needed
[xz/debian.git] / src / common / tuklib_mbstr_width.c
blob7a8bf0707518c792ad336a0a41a58ba253c2c5f4
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file tuklib_mbstr_width.c
6 /// \brief Calculate width of a multibyte string
7 //
8 // Author: Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "tuklib_mbstr.h"
13 #include <string.h>
15 #if defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
16 # include <wchar.h>
17 #endif
20 extern size_t
21 tuklib_mbstr_width(const char *str, size_t *bytes)
23 const size_t len = strlen(str);
24 if (bytes != NULL)
25 *bytes = len;
27 #if !(defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH))
28 // In single-byte mode, the width of the string is the same
29 // as its length.
30 return len;
32 #else
33 mbstate_t state;
34 memset(&state, 0, sizeof(state));
36 size_t width = 0;
37 size_t i = 0;
39 // Convert one multibyte character at a time to wchar_t
40 // and get its width using wcwidth().
41 while (i < len) {
42 wchar_t wc;
43 const size_t ret = mbrtowc(&wc, str + i, len - i, &state);
44 if (ret < 1 || ret > len)
45 return (size_t)-1;
47 i += ret;
49 const int wc_width = wcwidth(wc);
50 if (wc_width < 0)
51 return (size_t)-1;
53 width += (size_t)wc_width;
56 // Require that the string ends in the initial shift state.
57 // This way the caller can be combine the string with other
58 // strings without needing to worry about the shift states.
59 if (!mbsinit(&state))
60 return (size_t)-1;
62 return width;
63 #endif