GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / tools / misc / xz / src / common / tuklib_mbstr_width.c
blob3c38990f460827fc08b479c7fc84c7de7ec1ff95
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file tuklib_mstr_width.c
4 /// \brief Calculate width of a multibyte string
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tuklib_mbstr.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 += 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