From 701177ef5b4005d8f03c7f9f47081bb4e7c46517 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Thu, 3 Jul 2008 16:33:30 +0200 Subject: piano: Replace XmlEncodeString with more efficient version --- libpiano/src/xml.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) (limited to 'libpiano/src/xml.c') diff --git a/libpiano/src/xml.c b/libpiano/src/xml.c index a9be0a8..377affb 100644 --- a/libpiano/src/xml.c +++ b/libpiano/src/xml.c @@ -548,33 +548,27 @@ PianoReturn_t PianoXmlParseSearch (char *searchXml, * @return encoded string */ char *PianoXmlEncodeString (const char *s) { - char *replacements[] = {"&&", "''", "\""", "<<", ">>"}; - char *s_new = NULL; - unsigned int s_new_n = 0; - unsigned int i; - char found = 0; - - s_new_n = strlen (s) + 1; - s_new = calloc (s_new_n, 1); - - while (*s) { + char *replacements[] = {"&&", "''", "\""", "<<", + ">>", NULL}; + char **r; + char *sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut)); + char found; + + while (*s != '\0') { + r = replacements; found = 0; - for (i = 0; i < sizeof (replacements) / sizeof (*replacements); i++) { - if (*s == replacements[i][0]) { - /* replacements[i]+1 is our replacement, and -1 'cause we - * "overwrite" one byte */ - s_new_n += strlen (replacements[i]+1)-1; - s_new = realloc (s_new, s_new_n); - strncat (s_new, replacements[i]+1, strlen (replacements[i]+1)); + while (*r != NULL) { + if (*s == *r[0]) { found = 1; - /* no need to look for other entities */ + strcat (sOut, (*r) + 1); break; } + r++; } if (!found) { - strncat (s_new, s, 1); + strncat (sOut, s, 1); } s++; } - return s_new; + return sOut; } -- cgit v1.2.3