From a5ef363808b100856783784654de9b6bf2110173 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 23 Nov 2015 21:13:59 +0100 Subject: [PATCH] escape: add cescape_length() call as generalization of cescape() --- src/basic/escape.c | 16 +++++++++++----- src/basic/escape.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/basic/escape.c b/src/basic/escape.c index 4815161..42a84c9 100644 --- a/src/basic/escape.c +++ b/src/basic/escape.c @@ -89,20 +89,20 @@ size_t cescape_char(char c, char *buf) { return buf - buf_old; } -char *cescape(const char *s) { - char *r, *t; +char *cescape_length(const char *s, size_t n) { const char *f; + char *r, *t; - assert(s); + assert(s || n == 0); /* Does C style string escaping. May be reversed with * cunescape(). */ - r = new(char, strlen(s)*4 + 1); + r = new(char, n*4 + 1); if (!r) return NULL; - for (f = s, t = r; *f; f++) + for (f = s, t = r; f < s + n; f++) t += cescape_char(*f, t); *t = 0; @@ -110,6 +110,12 @@ char *cescape(const char *s) { return r; } +char *cescape(const char *s) { + assert(s); + + return cescape_length(s, strlen(s)); +} + int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode) { int r = 1; diff --git a/src/basic/escape.h b/src/basic/escape.h index 30604c5..52ebf11 100644 --- a/src/basic/escape.h +++ b/src/basic/escape.h @@ -35,6 +35,7 @@ typedef enum UnescapeFlags { } UnescapeFlags; char *cescape(const char *s); +char *cescape_length(const char *s, size_t n); size_t cescape_char(char c, char *buf); int cunescape(const char *s, UnescapeFlags flags, char **ret); -- 2.7.4