/* vifm
* Copyright (C) 2001 Ken Steen.
* Copyright (C) 2011 xaizek.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "str.h"
#include <ctype.h> /* tolower() isspace() */
#include <limits.h> /* INT_MAX INT_MIN LONG_MAX LONG_MIN */
#include <stdarg.h> /* va_list va_start() va_copy() va_end() */
#include <stddef.h> /* NULL size_t wchar_t */
#include <stdio.h> /* snprintf() */
#include <stdlib.h> /* free() malloc() mbstowcs() memmove() memset() realloc()
strtol() wcstombs() */
#include <string.h> /* memcpy() strdup() strncmp() strlen() strcmp() strchr()
strrchr() strncpy() strcspn() strspn() */
#include <wchar.h> /* wint_t vswprintf() */
#include <wctype.h> /* iswprint() iswupper() towlower() towupper() */
#include "../compat/reallocarray.h"
#include "macros.h"
#include "test_helpers.h"
#include "utf8.h"
#include "utils.h"
/* Where to place ellipsis. */
typedef enum
{
EK_LEFT, /* On the far left. */
EK_RIGHT, /* On the far right. */
EK_MIDDLE, /* In the middle. */
}
EllipsisKind;
static int transform_ascii_str(const char str[], int (*f)(int), char buf[],
size_t buf_len);
static int transform_wide_str(const char str[], wint_t (*f)(wint_t), char buf[],
size_t buf_len);
TSTATIC void squash_double_commas(char str[]);
static char * ellipsis(const char str[], size_t max_width, const char ell[],
EllipsisKind kind);
static size_t copy_substr(char dst[], size_t dst_len, const char src[],
char terminator);
void
chomp(char str[])
{
if(str[0] != '\0')
{
const size_t last_char_pos = strlen(str) - 1;
if(str[last_char_pos] == '\n')
{
str[last_char_pos] = '\0';
}
}
}
wchar_t *
to_wide(const char s[])
{
#ifndef _WIN32
wchar_t *result = NULL;
size_t len;
len = mbstowcs(NULL, s, 0);
if(len != (size_t)-1)
{
result = reallocarray(NULL, len + 1, sizeof(wchar_t));
if(result != NULL)
{
(void)mbstowcs(result, s, len + 1);
}
}
return result;
#else
return utf8_to_utf16(s);
#endif
}
wchar_t *
to_wide_force(const char s[])
{
wchar_t *w = to_wide(s);
wchar_t *p;
if(w