}
int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
- char *r, *t;
+ _cleanup_free_ char *ans = NULL;
+ char *t;
const char *f;
size_t pl;
+ int r;
assert(s);
assert(ret);
pl = strlen_ptr(prefix);
- r = new(char, pl+length+1);
- if (!r)
+ ans = new(char, pl+length+1);
+ if (!ans)
return -ENOMEM;
if (prefix)
- memcpy(r, prefix, pl);
+ memcpy(ans, prefix, pl);
- for (f = s, t = r + pl; f < s + length; f++) {
+ for (f = s, t = ans + pl; f < s + length; f++) {
size_t remaining;
bool eight_bit = false;
char32_t u;
- int k;
remaining = s + length - f;
assert(remaining > 0);
continue;
}
- free(r);
return -EINVAL;
}
- k = cunescape_one(f + 1, remaining - 1, &u, &eight_bit, flags & UNESCAPE_ACCEPT_NUL);
- if (k < 0) {
+ r = cunescape_one(f + 1, remaining - 1, &u, &eight_bit, flags & UNESCAPE_ACCEPT_NUL);
+ if (r < 0) {
if (flags & UNESCAPE_RELAX) {
/* Invalid escape code, let's take it literal then */
*(t++) = '\\';
continue;
}
- free(r);
- return k;
+ return r;
}
- f += k;
+ f += r;
if (eight_bit)
/* One byte? Set directly as specified */
*(t++) = u;
*t = 0;
- *ret = r;
- return t - r;
+ *ret = TAKE_PTR(ans);
+ return t - *ret;
}
char* xescape_full(const char *s, const char *bad, size_t console_width, XEscapeFlags flags) {