(str_utf8_normalize): ret rid of extra memory allocation.

(str_utf8_casefold_normalize): likewise.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2012-08-31 13:28:10 +04:00
parent f9aacdaf05
commit 149cd8ea96

View File

@ -984,12 +984,14 @@ str_utf8_search_last (const char *text, const char *search, int case_sen)
static char * static char *
str_utf8_normalize (const char *text) str_utf8_normalize (const char *text)
{ {
GString *fixed = g_string_new (""); GString *fixed;
char *tmp; char *tmp;
char *result; char *result;
const char *start; const char *start;
const char *end; const char *end;
fixed = g_string_sized_new (4);
start = text; start = text;
while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') while (!g_utf8_validate (start, -1, &end) && start[0] != '\0')
{ {
@ -1006,6 +1008,7 @@ str_utf8_normalize (const char *text)
if (start == text) if (start == text)
{ {
result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL); result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
g_string_free (fixed, TRUE);
} }
else else
{ {
@ -1015,9 +1018,8 @@ str_utf8_normalize (const char *text)
g_string_append (fixed, tmp); g_string_append (fixed, tmp);
g_free (tmp); g_free (tmp);
} }
result = g_strdup (fixed->str); result = g_string_free (fixed, FALSE);
} }
g_string_free (fixed, TRUE);
return result; return result;
} }
@ -1025,12 +1027,14 @@ str_utf8_normalize (const char *text)
static char * static char *
str_utf8_casefold_normalize (const char *text) str_utf8_casefold_normalize (const char *text)
{ {
GString *fixed = g_string_new (""); GString *fixed;
char *tmp, *fold; char *tmp, *fold;
char *result; char *result;
const char *start; const char *start;
const char *end; const char *end;
fixed = g_string_sized_new (4);
start = text; start = text;
while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') while (!g_utf8_validate (start, -1, &end) && start[0] != '\0')
{ {
@ -1051,6 +1055,7 @@ str_utf8_casefold_normalize (const char *text)
fold = g_utf8_casefold (text, -1); fold = g_utf8_casefold (text, -1);
result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL);
g_free (fold); g_free (fold);
g_string_free (fixed, TRUE);
} }
else else
{ {
@ -1062,9 +1067,8 @@ str_utf8_casefold_normalize (const char *text)
g_free (tmp); g_free (tmp);
g_free (fold); g_free (fold);
} }
result = g_strdup (fixed->str); result = g_string_free (fixed, FALSE);
} }
g_string_free (fixed, TRUE);
return result; return result;
} }