diff options
author | WaLyong Cho <walyong.cho@samsung.com> | 2014-11-12 19:49:31 +0900 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-12-04 02:09:44 +0100 |
commit | fec8457652002fd5bff923042a7407b3810f5a7f (patch) | |
tree | 94f038e2c98fb54c228b82d32fe72c61ab47bee5 /src/shared/utf8.c | |
parent | da64a1fc41e84bdb67c56693fff685b5eff2a2c7 (diff) |
utf8: intruduce utf8_escape_non_printable
Diffstat (limited to 'src/shared/utf8.c')
-rw-r--r-- | src/shared/utf8.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/shared/utf8.c b/src/shared/utf8.c index 8702ceb1b2..0b6c38ebbf 100644 --- a/src/shared/utf8.c +++ b/src/shared/utf8.c @@ -212,6 +212,45 @@ char *utf8_escape_invalid(const char *str) { return p; } +char *utf8_escape_non_printable(const char *str) { + char *p, *s; + + assert(str); + + p = s = malloc(strlen(str) * 4 + 1); + if (!p) + return NULL; + + while (*str) { + int len; + + len = utf8_encoded_valid_unichar(str); + if (len > 0) { + if (utf8_is_printable(str, len)) { + s = mempcpy(s, str, len); + str += len; + } else { + if ((*str < ' ') || (*str >= 127)) { + *(s++) = '\\'; + *(s++) = 'x'; + *(s++) = hexchar((int) *str >> 4); + *(s++) = hexchar((int) *str); + } else + *(s++) = *str; + + str += 1; + } + } else { + s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER)); + str += 1; + } + } + + *s = '\0'; + + return p; +} + char *ascii_is_valid(const char *str) { const char *p; |