2 * Copyright (C) 2004-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file string.c Memory allocation and string handling functions. */
11 #include <sys/time.h> /* gettimeofday */
13 #include <sys/utsname.h> /* uname() */
27 * Paraslash's version of realloc().
29 * \param p Pointer to the memory block, may be \p NULL.
30 * \param size The desired new size.
32 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
33 * i.e. there is no need to check the return value in the caller.
35 * \return A pointer to the newly allocated memory, which is suitably aligned
36 * for any kind of variable and may be different from \a p.
40 __must_check __malloc void *para_realloc(void *p, size_t size)
43 * No need to check for NULL pointers: If p is NULL, the call
44 * to realloc is equivalent to malloc(size)
47 if (!(p = realloc(p, size))) {
48 PARA_EMERG_LOG("realloc failed (size = %zu), aborting\n",
56 * Paraslash's version of malloc().
58 * \param size The desired new size.
60 * A wrapper for malloc(3) which exits on errors.
62 * \return A pointer to the allocated memory, which is suitably aligned for any
67 __must_check __malloc void *para_malloc(size_t size)
74 PARA_EMERG_LOG("malloc failed (size = %zu), aborting\n",
82 * Paraslash's version of calloc().
84 * \param size The desired new size.
86 * A wrapper for calloc(3) which exits on errors.
88 * \return A pointer to the allocated and zeroed-out memory, which is suitably
89 * aligned for any kind of variable.
93 __must_check __malloc void *para_calloc(size_t size)
95 void *ret = para_malloc(size);
102 * Paraslash's version of strdup().
104 * \param s The string to be duplicated.
106 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
107 * there is no need to check the return value in the caller.
109 * \return A pointer to the duplicated string. If \a s was the \p NULL pointer,
110 * an pointer to an empty string is returned.
114 __must_check __malloc char *para_strdup(const char *s)
118 if ((ret = strdup(s? s: "")))
120 PARA_EMERG_LOG("strdup failed, aborting\n");
125 * Print a formated message to a dynamically allocated string.
127 * \param result The formated string is returned here.
128 * \param fmt The format string.
129 * \param ap Initialized list of arguments.
131 * This function is similar to vasprintf(), a GNU extension which is not in C
132 * or POSIX. It allocates a string large enough to hold the output including
133 * the terminating null byte. The allocated string is returned via the first
134 * argument and must be freed by the caller. However, unlike vasprintf(), this
135 * function calls exit() if insufficient memory is available, while vasprintf()
136 * returns -1 in this case.
138 * \return Number of bytes written, not including the terminating \p NULL
141 * \sa printf(3), vsnprintf(3), va_start(3), vasprintf(3), \ref xasprintf().
143 __printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap)
150 ret = vsnprintf(NULL, 0, fmt, aq);
154 *result = para_malloc(size);
156 ret = vsnprintf(*result, size, fmt, aq);
158 assert(ret >= 0 && ret < size);
163 * Print to a dynamically allocated string, variable number of arguments.
165 * \param result See \ref xvasprintf().
166 * \param fmt Usual format string.
168 * \return The return value of the underlying call to \ref xvasprintf().
170 * \sa \ref xvasprintf() and the references mentioned there.
172 __printf_2_3 unsigned xasprintf(char **result, const char *fmt, ...)
178 ret = xvasprintf(result, fmt, ap);
184 * Allocate a sufficiently large string and print into it.
186 * \param fmt A usual format string.
188 * Produce output according to \p fmt. No artificial bound on the length of the
189 * resulting string is imposed.
191 * \return This function either returns a pointer to a string that must be
192 * freed by the caller or aborts without returning.
194 * \sa printf(3), xasprintf().
196 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
202 xvasprintf(&msg, fmt, ap);
208 * Free the content of a pointer and set it to \p NULL.
210 * This is equivalent to "free(*arg); *arg = NULL;".
212 * \param arg The pointer whose content should be freed.
214 void freep(void *arg)
216 void **ptr = (void **)arg;
222 * Paraslash's version of strcat().
224 * \param a String to be appended to.
225 * \param b String to append.
227 * Append \p b to \p a.
229 * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e.
230 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is \p NULL,
231 * return \a a without making a copy of \a a. Otherwise, construct the
232 * concatenation \a c, free \a a (but not \a b) and return \a c.
236 __must_check __malloc char *para_strcat(char *a, const char *b)
241 return para_strdup(b);
244 tmp = make_message("%s%s", a, b);
250 * Paraslash's version of dirname().
252 * \param name Pointer to the full path.
254 * Compute the directory component of \p name.
256 * \return If \a name is \p NULL or the empty string, return \p NULL.
257 * Otherwise, Make a copy of \a name and return its directory component. Caller
258 * is responsible to free the result.
260 __must_check __malloc char *para_dirname(const char *name)
266 ret = para_strdup(name);
267 p = strrchr(ret, '/');
276 * Paraslash's version of basename().
278 * \param name Pointer to the full path.
280 * Compute the filename component of \a name.
282 * \return \p NULL if (a) \a name is the empty string or \p NULL, or (b) name
283 * ends with a slash. Otherwise, a pointer within \a name is returned. Caller
284 * must not free the result.
286 __must_check char *para_basename(const char *name)
292 ret = strrchr(name, '/');
300 * Cut trailing newline.
302 * \param buf The string to be chopped.
304 * Replace the last character in \p buf by zero if it is equal to
305 * the newline character.
313 if (buf[n - 1] == '\n')
318 * Get the logname of the current user.
320 * \return A dynamically allocated string that must be freed by the caller. On
321 * errors, the string "unknown_user" is returned, i.e. this function never
326 __must_check __malloc char *para_logname(void)
328 struct passwd *pw = getpwuid(getuid());
329 return para_strdup(pw? pw->pw_name : "unknown_user");
333 * Get the home directory of the current user.
335 * \return A dynamically allocated string that must be freed by the caller. If
336 * the home directory could not be found, this function returns "/tmp".
338 __must_check __malloc char *para_homedir(void)
340 struct passwd *pw = getpwuid(getuid());
341 return para_strdup(pw? pw->pw_dir : "/tmp");
345 * Get the own hostname.
347 * \return A dynamically allocated string containing the hostname.
351 __malloc char *para_hostname(void)
356 return para_strdup(u.nodename);
360 * Used to distinguish between read-only and read-write mode.
362 * \sa for_each_line(), for_each_line_ro().
364 enum for_each_line_modes{
365 /** Activate read-only mode. */
367 /** Activate read-write mode. */
371 static int for_each_complete_line(enum for_each_line_modes mode, char *buf,
372 size_t size, line_handler_t *line_handler, void *private_data)
374 char *start = buf, *end;
375 int ret, i, num_lines = 0;
377 // PARA_NOTICE_LOG("buf: %s\n", buf);
378 while (start < buf + size) {
382 next_cr = memchr(start, '\n', buf + size - start);
383 next_null = memchr(start, '\0', buf + size - start);
384 if (!next_cr && !next_null)
386 if (next_cr && next_null) {
387 end = next_cr < next_null? next_cr : next_null;
388 } else if (next_null) {
397 if (mode == LINE_MODE_RO) {
398 size_t s = end - start;
399 char *b = para_malloc(s + 1);
402 // PARA_NOTICE_LOG("b: %s, start: %s\n", b, start);
403 ret = line_handler(b, private_data);
407 ret = line_handler(start, private_data);
413 if (!line_handler || mode == LINE_MODE_RO)
415 i = buf + size - start;
417 memmove(buf, start, i);
422 * Call a custom function for each complete line.
424 * \param buf The buffer containing data separated by newlines.
425 * \param size The number of bytes in \a buf.
426 * \param line_handler The custom function.
427 * \param private_data Pointer passed to \a line_handler.
429 * If \p line_handler is \p NULL, the function returns the number of complete
430 * lines in \p buf. Otherwise, \p line_handler is called for each complete
431 * line in \p buf. The first argument to \p line_handler is the current line,
432 * and \p private_data is passed as the second argument. The function returns
433 * if \p line_handler returns a negative value or no more lines are in the
434 * buffer. The rest of the buffer (last chunk containing an incomplete line)
435 * is moved to the beginning of the buffer.
437 * \return If \p line_handler is not \p NULL, this function returns the number
438 * of bytes not handled to \p line_handler on success, or the negative return
439 * value of the \p line_handler on errors.
441 * \sa for_each_line_ro().
443 int for_each_line(char *buf, size_t size, line_handler_t *line_handler,
446 return for_each_complete_line(LINE_MODE_RW, buf, size, line_handler,
451 * Call a custom function for each complete line.
453 * \param buf Same meaning as in \p for_each_line().
454 * \param size Same meaning as in \p for_each_line().
455 * \param line_handler Same meaning as in \p for_each_line().
456 * \param private_data Same meaning as in \p for_each_line().
458 * This function behaves like \p for_each_line(), but \a buf is left unchanged.
460 * \return On success, the function returns the number of complete lines in \p
461 * buf, otherwise the (negative) return value of \p line_handler is returned.
463 * \sa for_each_line().
465 int for_each_line_ro(char *buf, size_t size, line_handler_t *line_handler,
468 return for_each_complete_line(LINE_MODE_RO, buf, size, line_handler,
472 /** Return the hex characters of the lower 4 bits. */
473 #define hex(a) (hexchar[(a) & 15])
475 static void write_size_header(char *buf, int n)
477 static char hexchar[] = "0123456789abcdef";
479 buf[0] = hex(n >> 12);
480 buf[1] = hex(n >> 8);
481 buf[2] = hex(n >> 4);
487 * Read a four-byte hex-number and return its value.
489 * Each status item sent by para_server is prefixed with such a hex number in
490 * ASCII which describes the size of the status item.
492 * \param buf The buffer which must be at least four bytes long.
494 * \return The value of the hex number on success, \p -E_SIZE_PREFIX if the
495 * buffer did not contain only hex digits.
497 int read_size_header(const char *buf)
501 for (i = 0; i < 4; i++) {
502 unsigned char c = buf[i];
504 if (c >= '0' && c <= '9') {
508 if (c >= 'a' && c <= 'f') {
512 return -E_SIZE_PREFIX;
515 return -E_SIZE_PREFIX;
520 * Safely print into a buffer at a given offset.
522 * \param b Determines the buffer, its size, and the offset.
523 * \param fmt The format string.
525 * This function prints into the buffer given by \a b at the offset which is
526 * also given by \a b. If there is not enough space to hold the result, the
527 * buffer size is doubled until the underlying call to vsnprintf() succeeds
528 * or the size of the buffer exceeds the maximal size specified in \a b.
530 * In the latter case the unmodified \a buf and \a offset values as well as the
531 * private_data pointer of \a b are passed to the \a max_size_handler of \a b.
532 * If this function succeeds, i.e. returns a non-negative value, the offset of
533 * \a b is reset to zero and the given data is written to the beginning of the
534 * buffer. If \a max_size_handler() returns a negative value, this value is
535 * returned by \a para_printf().
537 * Upon return, the offset of \a b is adjusted accordingly so that subsequent
538 * calls to this function append data to what is already contained in the
541 * It's OK to call this function with \p b->buf being \p NULL. In this case, an
542 * initial buffer is allocated.
544 * \return The number of bytes printed into the buffer (not including the
545 * terminating \p NULL byte) on success, negative on errors. If there is no
546 * size-bound on \a b, i.e. if \p b->max_size is zero, this function never
549 * \sa make_message(), vsnprintf(3).
551 __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...)
553 int ret, sz_off = (b->flags & PBF_SIZE_PREFIX)? 5 : 0;
556 b->buf = para_malloc(128);
561 char *p = b->buf + b->offset;
562 size_t size = b->size - b->offset;
567 ret = vsnprintf(p + sz_off, size - sz_off, fmt, ap);
569 if (ret > -1 && ret < size - sz_off) { /* success */
570 b->offset += ret + sz_off;
572 write_size_header(p, ret);
576 /* check if we may grow the buffer */
577 if (!b->max_size || 2 * b->size < b->max_size) { /* yes */
578 /* try again with more space */
580 b->buf = para_realloc(b->buf, b->size);
583 /* can't grow buffer */
584 if (!b->offset || !b->max_size_handler) /* message too large */
585 return -ERRNO_TO_PARA_ERROR(ENOSPC);
586 ret = b->max_size_handler(b->buf, b->offset, b->private_data);
593 /** \cond llong_minmax */
594 /* LLONG_MAX and LLONG_MIN might not be defined. */
596 #define LLONG_MAX 9223372036854775807LL
599 #define LLONG_MIN (-LLONG_MAX - 1LL)
601 /** \endcond llong_minmax */
604 * Convert a string to a 64-bit signed integer value.
606 * \param str The string to be converted.
607 * \param value Result pointer.
611 * \sa para_atoi32(), strtol(3), atoi(3).
613 int para_atoi64(const char *str, int64_t *value)
618 errno = 0; /* To distinguish success/failure after call */
619 tmp = strtoll(str, &endptr, 10);
620 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
621 return -E_ATOI_OVERFLOW;
622 if (errno != 0 && tmp == 0) /* other error */
625 return -E_ATOI_NO_DIGITS;
626 if (*endptr != '\0') /* Further characters after number */
627 return -E_ATOI_JUNK_AT_END;
633 * Convert a string to a 32-bit signed integer value.
635 * \param str The string to be converted.
636 * \param value Result pointer.
642 int para_atoi32(const char *str, int32_t *value)
646 const int32_t max = 2147483647;
648 ret = para_atoi64(str, &tmp);
651 if (tmp > max || tmp < -max - 1)
652 return -E_ATOI_OVERFLOW;
657 static inline int loglevel_equal(const char *arg, const char * const ll)
659 return !strncasecmp(arg, ll, strlen(ll));
663 * Compute the loglevel number from its name.
665 * \param txt The name of the loglevel (debug, info, ...).
667 * \return The numeric representation of the loglevel name.
669 int get_loglevel_by_name(const char *txt)
671 if (loglevel_equal(txt, "debug"))
673 if (loglevel_equal(txt, "info"))
675 if (loglevel_equal(txt, "notice"))
677 if (loglevel_equal(txt, "warning"))
679 if (loglevel_equal(txt, "error"))
681 if (loglevel_equal(txt, "crit"))
683 if (loglevel_equal(txt, "emerg"))
688 static int get_next_word(const char *buf, const char *delim, char **word)
690 enum line_state_flags {LSF_HAVE_WORD = 1, LSF_BACKSLASH = 2,
691 LSF_SINGLE_QUOTE = 4, LSF_DOUBLE_QUOTE = 8};
696 out = para_malloc(strlen(buf) + 1);
699 for (in = buf; *in; in++) {
704 if (state & LSF_BACKSLASH) /* \\ */
706 state |= LSF_BACKSLASH;
707 state |= LSF_HAVE_WORD;
711 if (state & LSF_BACKSLASH) { /* \n or \t */
712 *out++ = (*in == 'n')? '\n' : '\t';
713 state &= ~LSF_BACKSLASH;
718 if (state & LSF_BACKSLASH) /* \" */
720 if (state & LSF_SINGLE_QUOTE) /* '" */
722 if (state & LSF_DOUBLE_QUOTE) {
723 state &= ~LSF_DOUBLE_QUOTE;
726 state |= LSF_HAVE_WORD;
727 state |= LSF_DOUBLE_QUOTE;
730 if (state & LSF_BACKSLASH) /* \' */
732 if (state & LSF_DOUBLE_QUOTE) /* "' */
734 if (state & LSF_SINGLE_QUOTE) {
735 state &= ~LSF_SINGLE_QUOTE;
738 state |= LSF_HAVE_WORD;
739 state |= LSF_SINGLE_QUOTE;
742 for (p = delim; *p; p++) {
745 if (state & LSF_BACKSLASH)
747 if (state & LSF_SINGLE_QUOTE)
749 if (state & LSF_DOUBLE_QUOTE)
751 if (state & LSF_HAVE_WORD)
755 if (*p) /* ignore delimiter at the beginning */
758 state |= LSF_HAVE_WORD;
760 state &= ~LSF_BACKSLASH;
763 if (!(state & LSF_HAVE_WORD))
765 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
766 if (state & LSF_BACKSLASH) {
767 PARA_ERROR_LOG("trailing backslash\n");
770 if ((state & LSF_SINGLE_QUOTE) || (state & LSF_DOUBLE_QUOTE)) {
771 PARA_ERROR_LOG("unmatched quote character\n");
784 * Get the number of the word the cursor is on.
786 * \param buf The zero-terminated line buffer.
787 * \param delim Characters that separate words.
788 * \param point The cursor position.
790 * \return Zero-based word number.
792 int compute_word_num(const char *buf, const char *delim, int point)
798 for (p = buf, num_words = 0; ; p += ret, num_words++) {
799 ret = get_next_word(p, delim, &word);
803 if (p + ret >= buf + point)
810 * Free an array of words created by create_argv() or create_shifted_argv().
812 * \param argv A pointer previously obtained by \ref create_argv().
814 void free_argv(char **argv)
820 for (i = 0; argv[i]; i++)
825 static int create_argv_offset(int offset, const char *buf, const char *delim,
828 char *word, **argv = para_malloc((offset + 1) * sizeof(char *));
832 for (i = 0; i < offset; i++)
834 for (p = buf; p && *p; p += ret, i++) {
835 ret = get_next_word(p, delim, &word);
840 argv = para_realloc(argv, (i + 2) * sizeof(char*));
855 * Split a buffer into words.
857 * This parser honors single and double quotes, backslash-escaped characters
858 * and special characters like \p \\n. The result contains pointers to copies
859 * of the words contained in \a buf and has to be freed by using \ref
862 * \param buf The buffer to be split.
863 * \param delim Each character in this string is treated as a separator.
864 * \param result The array of words is returned here.
866 * \return Number of words in \a buf, negative on errors.
868 int create_argv(const char *buf, const char *delim, char ***result)
870 return create_argv_offset(0, buf, delim, result);
874 * Split a buffer into words, offset one.
876 * This is similar to \ref create_argv() but the returned array is one element
877 * larger, words start at index one and element zero is initialized to \p NULL.
878 * Callers must set element zero to a non-NULL value before calling free_argv()
879 * on the returned array to avoid a memory leak.
881 * \param buf See \ref create_argv().
882 * \param delim See \ref create_argv().
883 * \param result See \ref create_argv().
885 * \return Number of words plus one on success, negative on errors.
887 int create_shifted_argv(const char *buf, const char *delim, char ***result)
889 return create_argv_offset(1, buf, delim, result);
893 * Find out if the given string is contained in the arg vector.
895 * \param arg The string to look for.
896 * \param argv The array to search.
898 * \return The first index whose value equals \a arg, or \p -E_ARG_NOT_FOUND if
899 * arg was not found in \a argv.
901 int find_arg(const char *arg, char **argv)
906 return -E_ARG_NOT_FOUND;
907 for (i = 0; argv[i]; i++)
908 if (strcmp(arg, argv[i]) == 0)
910 return -E_ARG_NOT_FOUND;
914 * Compile a regular expression.
916 * This simple wrapper calls regcomp() and logs a message on errors.
918 * \param preg See regcomp(3).
919 * \param regex See regcomp(3).
920 * \param cflags See regcomp(3).
924 int para_regcomp(regex_t *preg, const char *regex, int cflags)
928 int ret = regcomp(preg, regex, cflags);
932 size = regerror(ret, preg, NULL, 0);
933 buf = para_malloc(size);
934 regerror(ret, preg, buf, size);
935 PARA_ERROR_LOG("%s\n", buf);
941 * strdup() for not necessarily zero-terminated strings.
943 * \param src The source buffer.
944 * \param len The number of bytes to be copied.
946 * \return A 0-terminated buffer of length \a len + 1.
948 * This is similar to strndup(), which is a GNU extension. However, one
949 * difference is that strndup() returns \p NULL if insufficient memory was
950 * available while this function aborts in this case.
952 * \sa strdup(), \ref para_strdup().
954 char *safe_strdup(const char *src, size_t len)
958 assert(len < (size_t)-1);
959 p = para_malloc(len + 1);
967 * Copy the value of a key=value pair.
969 * This checks whether the given buffer starts with "key=", ignoring case. If
970 * yes, a copy of the value is returned. The source buffer may not be
973 * \param src The source buffer.
974 * \param len The number of bytes of the tag.
975 * \param key Only copy if it is the value of this key.
977 * \return A zero-terminated buffer, or \p NULL if the key was
978 * not of the given type.
980 char *key_value_copy(const char *src, size_t len, const char *key)
982 int keylen = strlen(key);
986 if (strncasecmp(src, key, keylen))
988 if (src[keylen] != '=')
990 return safe_strdup(src + keylen + 1, len - keylen - 1);
993 static bool utf8_mode(void)
995 static bool initialized, have_utf8;
998 char *info = nl_langinfo(CODESET);
999 have_utf8 = (info && strcmp(info, "UTF-8") == 0);
1001 PARA_INFO_LOG("%susing UTF-8 character encoding\n",
1002 have_utf8? "" : "not ");
1008 * glibc's wcswidth returns -1 if the string contains a tab character, which
1009 * makes the function next to useless. The two functions below are taken from
1013 #define IsWPrint(wc) (iswprint(wc) || wc >= 0xa0)
1015 static int mutt_wcwidth(wchar_t wc, size_t pos)
1019 if (wc == 0x09) /* tab */
1020 return (pos | 7) + 1 - pos;
1022 if (IsWPrint(wc) && n > 0)
1026 if (!(wc & ~0xffff))
1031 static size_t mutt_wcswidth(const wchar_t *s, size_t n)
1036 w += mutt_wcwidth(*s++, w);
1041 * Skip a given number of cells at the beginning of a string.
1043 * \param s The input string.
1044 * \param cells_to_skip Desired number of cells that should be skipped.
1045 * \param bytes_to_skip Result.
1047 * This function computes how many input bytes must be skipped to advance a
1048 * string by the given width. If the current character encoding is not UTF-8,
1049 * this is simply the given number of cells, i.e. \a cells_to_skip. Otherwise,
1050 * \a s is treated as a multibyte string and on successful return, \a s +
1051 * bytes_to_skip points to the start of a multibyte string such that the total
1052 * width of the multibyte characters that are skipped by advancing \a s that
1053 * many bytes equals at least \a cells_to_skip.
1057 int skip_cells(const char *s, size_t cells_to_skip, size_t *bytes_to_skip)
1061 size_t n, bytes_parsed, cells_skipped;
1064 if (cells_to_skip == 0)
1067 *bytes_to_skip = cells_to_skip;
1070 bytes_parsed = cells_skipped = 0;
1071 memset(&ps, 0, sizeof(ps));
1073 while (cells_to_skip > cells_skipped) {
1076 mbret = mbrtowc(&wc, s + bytes_parsed, n - bytes_parsed, &ps);
1078 if (mbret == (size_t)-1 || mbret == (size_t)-2)
1079 return -ERRNO_TO_PARA_ERROR(EILSEQ);
1080 bytes_parsed += mbret;
1081 cells_skipped += mutt_wcwidth(wc, cells_skipped);
1083 *bytes_to_skip = bytes_parsed;
1088 * Compute the width of an UTF-8 string.
1090 * \param s The string.
1091 * \param result The width of \a s is returned here.
1093 * If not in UTF8-mode. this function is just a wrapper for strlen(3).
1094 * Otherwise \s is treated as an UTF-8 string and its display width is
1095 * computed. Note that this function may fail if the underlying call to
1096 * mbsrtowcs(3) fails, so the caller must check the return value.
1098 * \sa nl_langinfo(3), wcswidth(3).
1102 __must_check int strwidth(const char *s, size_t *result)
1104 const char *src = s;
1106 static wchar_t *dest;
1110 * Never call any log function here. This may result in an endless loop
1111 * as para_gui's para_log() calls this function.
1115 *result = strlen(s);
1118 memset(&state, 0, sizeof(state));
1120 num_wchars = mbsrtowcs(NULL, &src, 0, &state);
1121 if (num_wchars == (size_t)-1)
1122 return -ERRNO_TO_PARA_ERROR(errno);
1123 if (num_wchars == 0)
1125 dest = para_malloc(num_wchars * sizeof(*dest));
1127 memset(&state, 0, sizeof(state));
1128 num_wchars = mbsrtowcs(dest, &src, num_wchars, &state);
1129 assert(num_wchars > 0 && num_wchars != (size_t)-1);
1130 *result = mutt_wcswidth(dest, num_wchars);