From f649f69effb3653e618f487943dfe12f7ed809b1 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:04:41 +0100 Subject: [PATCH] Remove mbstowcs/wcstombs from g_strtrim() Because of the way UTF-8 encoding works, there is no need to use mbstowcs/wcstombs in teh implementationof this function. --- common/string_calls.c | 132 ++++++++---------------------------------- common/string_calls.h | 7 +++ 2 files changed, 31 insertions(+), 108 deletions(-) diff --git a/common/string_calls.c b/common/string_calls.c index 22a04a0a45..112da0fbfe 100644 --- a/common/string_calls.c +++ b/common/string_calls.c @@ -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; } /*****************************************************************************/ diff --git a/common/string_calls.h b/common/string_calls.h index 46f3bbfc49..352ebc6b99 100644 --- a/common/string_calls.h +++ b/common/string_calls.h @@ -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); /**