core-util: Avoid usage of pa_strbuf in pa_escape()

The current code uses a pa_strbuf to construct the escaped string. This
will generate a linked list member for each character which may be very
inefficient.
This patch avoids the use of pa_strbuf by allocating a sufficiently large
string which can be filled with the output data.
This commit is contained in:
Georg Chini 2019-03-26 10:39:21 +01:00 committed by Tanu Kaskinen
parent f34ea0f0c3
commit ab9fed9523

View file

@ -3093,23 +3093,39 @@ char *pa_replace(const char*s, const char*a, const char *b) {
char *pa_escape(const char *p, const char *chars) {
const char *s;
const char *c;
pa_strbuf *buf = pa_strbuf_new();
char *out_string, *output;
int char_count = strlen(p);
/* Maximum number of characters in output string
* including trailing 0. */
char_count = 2 * char_count + 1;
/* allocate output string */
out_string = pa_xmalloc(char_count);
output = out_string;
/* write output string */
for (s = p; *s; ++s) {
if (*s == '\\')
pa_strbuf_putc(buf, '\\');
*output++ = '\\';
else if (chars) {
for (c = chars; *c; ++c) {
if (*s == *c) {
pa_strbuf_putc(buf, '\\');
*output++ = '\\';
break;
}
}
}
pa_strbuf_putc(buf, *s);
*output++ = *s;
}
return pa_strbuf_to_string_free(buf);
*output = 0;
/* Remove trailing garbage */
output = pa_xstrdup(out_string);
pa_xfree(out_string);
return output;
}
char *pa_unescape(char *p) {