Skip to content

Commit

Permalink
Remove mbstowcs/wcstombs from g_strtrim()
Browse files Browse the repository at this point in the history
Because of the way UTF-8 encoding works, there is no need to
use mbstowcs/wcstombs in teh implementationof this function.
  • Loading branch information
matt335672 committed Sep 20, 2023
1 parent 71fa683 commit f649f69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 108 deletions.
132 changes: 24 additions & 108 deletions common/string_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
case 3: /* trim both */
rv = g_strtrim(str, 1) || g_strtrim(str, 2);
break;

/* trim right */
for (index = len - 1; index >= 0; index--)
case 2: /* trim right */
index = strlen(str);
while (index > 0 && str[index - 1] <= ' ')
{
if (text1[index] > 32)
{
break;
}
--index;
}

text1_index = index + 1;
text1[text1_index] = 0;
str[index] = '\0';
break;
case 2: /* trim right */

/* copy it */
for (index = 0; index < len; index++)
case 1: /* trim left */
index = 0;
while (str[index] != '\0' && str[index] <= ' ')
{
text1[text1_index] = text[index];
text1_index++;
++index;
}

/* trim right */
for (index = len - 1; index >= 0; index--)
if (index > 0)
{
if (text1[index] > 32)
{
break;
}
memmove(str, str + index, strlen(str) + 1 - index);
}

text1_index = index + 1;
text1[text1_index] = 0;
break;
case 1: /* trim left */
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;
break;
default:
rv = 1;
}

wcstombs(str, text1, text1_index + 1);
free(text);
free(text1);
return 0;
return rv;
}

/*****************************************************************************/
Expand Down
7 changes: 7 additions & 0 deletions common/string_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,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);

/**
Expand Down

0 comments on commit f649f69

Please sign in to comment.