mirror of https://gerrit.osmocom.org/libosmocore
add osmo_quote_str_buf3, osmo_escape_str_buf3
There already are osmo_quote_str_buf() and osmo_quote_str_buf2(), same for _escape_, but none of them return the snprintf() like string length. A private function does, publish this in the API. The returned chars_needed is required to accurately allocate sufficient size in string functions that call osmo_quote_str/osmo_escape_str. I am adding such in osmo-upf.git. Related: SYS#5599 Change-Id: I05d75a40599e3133da099a11e8babaaad0e9493a
This commit is contained in:
parent
f3270f246f
commit
1633735e9b
|
@ -157,10 +157,12 @@ size_t osmo_quote_cstr_buf(char *buf, size_t bufsize, const char *str, int in_le
|
|||
char *osmo_quote_cstr_c(void *ctx, const char *str, int in_len);
|
||||
|
||||
const char *osmo_escape_str(const char *str, int len);
|
||||
int osmo_escape_str_buf3(char *buf, size_t bufsize, const char *str, int in_len);
|
||||
char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len);
|
||||
const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
|
||||
char *osmo_escape_str_c(const void *ctx, const char *str, int in_len);
|
||||
const char *osmo_quote_str(const char *str, int in_len);
|
||||
int osmo_quote_str_buf3(char *buf, size_t bufsize, const char *str, int in_len);
|
||||
char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len);
|
||||
const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
|
||||
char *osmo_quote_str_c(const void *ctx, const char *str, int in_len);
|
||||
|
|
28
src/utils.c
28
src/utils.c
|
@ -754,7 +754,7 @@ int osmo_print_n(char *buf, size_t bufsize, const char *str, size_t n)
|
|||
* the non-legacy format also escapes those as "\xNN" sequences.
|
||||
* \return Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).
|
||||
*/
|
||||
static size_t _osmo_escape_str_buf(char *buf, size_t bufsize, const char *str, int in_len, bool legacy_format)
|
||||
static int _osmo_escape_str_buf(char *buf, size_t bufsize, const char *str, int in_len, bool legacy_format)
|
||||
{
|
||||
struct osmo_strbuf sb = { .buf = buf, .len = bufsize };
|
||||
int in_pos = 0;
|
||||
|
@ -821,6 +821,18 @@ done:
|
|||
return sb.chars_needed;
|
||||
}
|
||||
|
||||
/*! Return the string with all non-printable characters escaped.
|
||||
* \param[out] buf string buffer to write escaped characters to.
|
||||
* \param[in] bufsize sizeof(buf).
|
||||
* \param[in] str A string that may contain any characters.
|
||||
* \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).
|
||||
* \return Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).
|
||||
*/
|
||||
int osmo_escape_str_buf3(char *buf, size_t bufsize, const char *str, int in_len)
|
||||
{
|
||||
return _osmo_escape_str_buf(buf, bufsize, str, in_len, false);
|
||||
}
|
||||
|
||||
/*! Return the string with all non-printable characters escaped.
|
||||
* \param[out] buf string buffer to write escaped characters to.
|
||||
* \param[in] bufsize sizeof(buf).
|
||||
|
@ -883,6 +895,20 @@ static size_t _osmo_quote_str_buf(char *buf, size_t bufsize, const char *str, in
|
|||
return sb.chars_needed;
|
||||
}
|
||||
|
||||
/*! Like osmo_escape_str_buf3(), but returns double-quotes around a string, or "NULL" for a NULL string.
|
||||
* This allows passing any char* value and get its C representation as string.
|
||||
* The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN().
|
||||
* \param[out] buf string buffer to write escaped characters to.
|
||||
* \param[in] bufsize sizeof(buf).
|
||||
* \param[in] str A string that may contain any characters.
|
||||
* \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length.
|
||||
* \return Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).
|
||||
*/
|
||||
int osmo_quote_str_buf3(char *buf, size_t bufsize, const char *str, int in_len)
|
||||
{
|
||||
return _osmo_quote_str_buf(buf, bufsize, str, in_len, false);
|
||||
}
|
||||
|
||||
/*! Like osmo_escape_str_buf2(), but returns double-quotes around a string, or "NULL" for a NULL string.
|
||||
* This allows passing any char* value and get its C representation as string.
|
||||
* The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN().
|
||||
|
|
Loading…
Reference in New Issue