Remove mbstowcs/wcstombs from g_strtrim()

Because of the way UTF-8 encoding works, there is no need to
use mbstowcs/wcstombs in the implementation of this function.
This commit is contained in:
matt335672 2023-09-20 15:04:41 +01:00
parent 0758fe03a6
commit 36ea4a3f86
2 changed files with 33 additions and 110 deletions

View File

@ -736,143 +736,59 @@ g_wcstombs(char *dest, const twchar *src, int n)
/*****************************************************************************/
/* returns error */
/* trim spaces and tabs, anything <= space */
/* trim_flags 1 trim left, 2 trim right, 3 trim both, 4 trim through */
/* this will always shorten the string or not change it */
int
g_strtrim(char *str, int trim_flags)
{
int rv = 0;
int index;
int len;
int text1_index;
int got_char;
wchar_t *text;
wchar_t *text1;
len = mbstowcs(0, str, 0);
if (len < 1)
{
return 0;
}
if ((trim_flags < 1) || (trim_flags > 4))
{
return 1;
}
text = (wchar_t *)malloc(len * sizeof(wchar_t) + 8);
text1 = (wchar_t *)malloc(len * sizeof(wchar_t) + 8);
if (text == NULL || text1 == NULL)
{
free(text);
free(text1);
return 1;
}
text1_index = 0;
mbstowcs(text, str, len + 1);
int j;
switch (trim_flags)
{
case 4: /* trim through */
for (index = 0; index < len; index++)
j = 0;
for (index = 0; str[index] != '\0'; index++)
{
if (text[index] > 32)
if (str[index] > ' ')
{
text1[text1_index] = text[index];
text1_index++;
str[j++] = str[index];
}
}
text1[text1_index] = 0;
str[j] = '\0';
break;
case 3: /* trim both */
got_char = 0;
for (index = 0; index < len; index++)
{
if (got_char)
{
text1[text1_index] = text[index];
text1_index++;
}
else
{
if (text[index] > 32)
{
text1[text1_index] = text[index];
text1_index++;
got_char = 1;
}
}
}
text1[text1_index] = 0;
len = text1_index;
/* trim right */
for (index = len - 1; index >= 0; index--)
{
if (text1[index] > 32)
{
break;
}
}
text1_index = index + 1;
text1[text1_index] = 0;
rv = g_strtrim(str, 1) || g_strtrim(str, 2);
break;
case 2: /* trim right */
/* copy it */
for (index = 0; index < len; index++)
index = strlen(str);
while (index > 0 && str[index - 1] <= ' ')
{
text1[text1_index] = text[index];
text1_index++;
--index;
}
/* trim right */
for (index = len - 1; index >= 0; index--)
{
if (text1[index] > 32)
{
break;
}
}
text1_index = index + 1;
text1[text1_index] = 0;
str[index] = '\0';
break;
case 1: /* trim left */
got_char = 0;
for (index = 0; index < len; index++)
index = 0;
while (str[index] != '\0' && str[index] <= ' ')
{
if (got_char)
{
text1[text1_index] = text[index];
text1_index++;
}
else
{
if (text[index] > 32)
{
text1[text1_index] = text[index];
text1_index++;
got_char = 1;
}
}
++index;
}
if (index > 0)
{
memmove(str, str + index, strlen(str) + 1 - index);
}
text1[text1_index] = 0;
break;
default:
rv = 1;
}
wcstombs(str, text1, text1_index + 1);
free(text);
free(text1);
return 0;
return rv;
}
/*****************************************************************************/

View File

@ -312,6 +312,13 @@ int g_pos(const char *str, const char *to_find);
char *g_strstr(const char *haystack, const char *needle);
int g_mbstowcs(twchar *dest, const char *src, int n);
int g_wcstombs(char *dest, const twchar *src, int n);
/** trim spaces and tabs, anything <= space
*
* @param str (assumed to be UTF-8)
* @param trim_flags 1 trim left, 2 trim right, 3 trim both, 4 trim through
* @return != 0 - trim_flags not recognised
* this will always shorten the string or not change it */
int g_strtrim(char *str, int trim_flags);
/**