From 04eb56f146a6c0395645aa2537d7c29f1e234069 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Mon, 9 Apr 2018 00:41:28 +0200 Subject: add osmo_quote_str(),osmo_quote_str_buf() and test Rationale: with osmo_escape_str(), you get the escaped contents of the string, but not so graceful handling of NULL strings. The caller needs to quote it, and for NULL strings not quote it. osmo_quote_str() is like osmo_escape_str() but always quotes a non-NULL string, and for a NULL string returns a literal NULL, i.e. it should (tm) give the exact C representation of a string. That's useful in testing, to show exactly what char* situation we have, without jumping through hoops like if (str) printf("\"%s\"", osmo_escape_str(str, -1)); else printf("NULL"); Copy the unit test for osmo_escape_str() and adjust. To indicate that the double quotes are returned by osmo_quote_str(), use single quotes in the test printf()s. I considered allowing to pick the quoting characters by further arguments, but that complicates things: we'd need to escape the quoting characters. Just hardcode double quotes like C. Change-Id: I6f1b3709b32c23fc52f70ad9ecc9439c62b02a12 --- include/osmocom/core/utils.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index f1e011fe..8928f686 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -125,5 +125,7 @@ bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars); const char *osmo_escape_str(const char *str, int len); const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize); +const char *osmo_quote_str(const char *str, int in_len); +const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize); /*! @} */ -- cgit v1.2.3