utf8: intruduce utf8_escape_non_printable

This commit is contained in:
WaLyong Cho 2014-11-12 19:49:31 +09:00 committed by Lennart Poettering
parent da64a1fc41
commit fec8457652
3 changed files with 65 additions and 0 deletions

View file

@ -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;

View file

@ -30,6 +30,7 @@
const char *utf8_is_valid(const char *s) _pure_;
char *ascii_is_valid(const char *s) _pure_;
char *utf8_escape_invalid(const char *s);
char *utf8_escape_non_printable(const char *str);
bool utf8_is_printable_newline(const char* str, size_t length, bool newline) _pure_;
_pure_ static inline bool utf8_is_printable(const char* str, size_t length) {

View file

@ -66,12 +66,37 @@ static void test_utf8_escaping(void) {
assert_se(utf8_is_valid(p3));
}
static void test_utf8_escaping_printable(void) {
_cleanup_free_ char *p1, *p2, *p3, *p4, *p5;
p1 = utf8_escape_non_printable("goo goo goo");
puts(p1);
assert_se(utf8_is_valid(p1));
p2 = utf8_escape_non_printable("\341\204\341\204");
puts(p2);
assert_se(utf8_is_valid(p2));
p3 = utf8_escape_non_printable("\341\204");
puts(p3);
assert_se(utf8_is_valid(p3));
p4 = utf8_escape_non_printable("ąę\n가너도루\n1234\n\341\204\341\204\n\001 \019\20\a");
puts(p4);
assert_se(utf8_is_valid(p4));
p5 = utf8_escape_non_printable("\001 \019\20\a");
puts(p5);
assert_se(utf8_is_valid(p5));
}
int main(int argc, char *argv[]) {
test_utf8_is_valid();
test_utf8_is_printable();
test_ascii_is_valid();
test_utf8_encoded_valid_unichar();
test_utf8_escaping();
test_utf8_escaping_printable();
return 0;
}