From 418625eaaacd43b84736788d6fe40c30a2a0a3d5 Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Sun, 10 Jun 2018 12:52:52 +0200 Subject: [PATCH] tests: add print_quoted_stringn to print string with a size limit This is similar to print_quoted_cstring, but not quite the same. * tests/print_quoted_string.c (print_quoted_stringn): New function. * tests/tests.h (print_quoted_stringn): New declaration. --- tests/print_quoted_string.c | 12 ++++++++++++ tests/tests.h | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/tests/print_quoted_string.c b/tests/print_quoted_string.c index 3ca56318..a58b69ac 100644 --- a/tests/print_quoted_string.c +++ b/tests/print_quoted_string.c @@ -33,6 +33,18 @@ print_quoted_cstring(const char *instr, const size_t size) } } +void +print_quoted_stringn(const char *instr, const size_t size) +{ + const size_t len = strnlen(instr, size); + if (len < size) { + print_quoted_memory(instr, len); + } else { + print_quoted_memory(instr, size); + printf("..."); + } +} + static void print_octal(unsigned char c, char next) { diff --git a/tests/tests.h b/tests/tests.h index 9b829ea3..248e341c 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -174,6 +174,12 @@ void print_quoted_string(const char *); */ void print_quoted_cstring(const char *str, size_t size); +/* + * Print a NUL-terminated string `str' of length up to `size' + * in a quoted form. + */ +void print_quoted_stringn(const char *str, size_t size); + /* Print memory in a quoted form with optional escape characters. */ void print_quoted_memory_ex(const void *, size_t, bool quote, const char *escape_chars);