mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-12-23 08:56:40 -05:00
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:
parent
f34ea0f0c3
commit
ab9fed9523
1 changed files with 21 additions and 5 deletions
|
|
@ -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) {
|
char *pa_escape(const char *p, const char *chars) {
|
||||||
const char *s;
|
const char *s;
|
||||||
const char *c;
|
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) {
|
for (s = p; *s; ++s) {
|
||||||
if (*s == '\\')
|
if (*s == '\\')
|
||||||
pa_strbuf_putc(buf, '\\');
|
*output++ = '\\';
|
||||||
else if (chars) {
|
else if (chars) {
|
||||||
for (c = chars; *c; ++c) {
|
for (c = chars; *c; ++c) {
|
||||||
if (*s == *c) {
|
if (*s == *c) {
|
||||||
pa_strbuf_putc(buf, '\\');
|
*output++ = '\\';
|
||||||
break;
|
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) {
|
char *pa_unescape(char *p) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue