From c355f792fce200367a7c802edf466a02134bad87 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 27 Oct 2022 13:22:19 +0200 Subject: Added nxt_usts2str() to make C strings from nxt_str_t. This function is identical to nxt_ustr2str(), except that it takes a nxt_str_t structure as input, instead of a 'u_char *' and a size. The documentation of the function: /* * SYNOPSIS * void nxt_usts2str(char dst[restrict .src->length+1], * const nxt_str_t *restrict src); * * ARGUMENTS * dst Pointer to the first byte of the destination buffer. * src Pointer to the source Unterminated STring Structure. * * DESCRIPTION * Copy a string from the source nxt_str_t, which may be * not-NUL-terminated, into a NUL-terminated string in the * destination buffer. * * CAVEATS * If the destination buffer is not wider than the source buffer * at least by 1 byte, the behavior is undefined. * * EXAMPLES * nxt_str_t src = nxt_string("0123456789"); * char dst[src.length + 1]; * * nxt_usts2str(dst, &src); * * SEE ALSO * ustr2str(3), strlcpy(3), strscpy(9) */ Suggested-by: Andrew Clayton Signed-off-by: Alejandro Colomar --- src/nxt_string.c | 2 ++ src/nxt_string.h | 9 +++++++++ 2 files changed, 11 insertions(+) (limited to 'src') diff --git a/src/nxt_string.c b/src/nxt_string.c index 39f472e9..8c86f3b0 100644 --- a/src/nxt_string.c +++ b/src/nxt_string.c @@ -9,6 +9,8 @@ extern inline void nxt_ustr2str(char *restrict dst, const u_char *restrict src, size_t length); +extern inline void nxt_usts2str(char *restrict dst, + const nxt_str_t *restrict src); nxt_str_t * diff --git a/src/nxt_string.h b/src/nxt_string.h index 275a62ee..c6d6798e 100644 --- a/src/nxt_string.h +++ b/src/nxt_string.h @@ -60,6 +60,8 @@ struct nxt_str_s { NXT_EXPORT inline void nxt_ustr2str(char *restrict dst, const u_char *restrict src, size_t length); +NXT_EXPORT inline void nxt_usts2str(char *restrict dst, + const nxt_str_t *restrict src); NXT_EXPORT void nxt_memcpy_lowcase(u_char *dst, const u_char *src, size_t length); @@ -182,4 +184,11 @@ nxt_ustr2str(char *restrict dst, const u_char *restrict src, size_t length) } +inline void +nxt_usts2str(char *restrict dst, const nxt_str_t *restrict src) +{ + nxt_ustr2str(dst, src->start, src->length); +} + + #endif /* _NXT_STRING_H_INCLUDED_ */ -- cgit