-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sys/string_utils: add string_writer helper #20843
Open
benpicco
wants to merge
2
commits into
RIOT-OS:master
Choose a base branch
from
benpicco:string_writer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
* @author Marian Buschsieweke <[email protected]> | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <stdint.h> | ||
/* if explicit_bzero() is provided by standard C lib, it may be defined in | ||
|
@@ -38,6 +39,67 @@ | |
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief String Writer structure. | ||
* Helper for writing multiple formatted strings to a buffer | ||
*/ | ||
typedef struct { | ||
const char *start; /**< start of the target buffer */ | ||
char *position; /**< current write pointer */ | ||
size_t capacity; /**< remaining capacity of the buffer */ | ||
} string_writer_t; | ||
|
||
/** | ||
* @brief Initialize a string writer structure | ||
* | ||
* @param[out] sw String Writer object to initialize | ||
* @param[in] buffer destination buffer | ||
* @param[in] len size of the destination buffer | ||
*/ | ||
static inline void string_writer_init(string_writer_t *sw, void *buffer, size_t len) | ||
{ | ||
assert(buffer && len); | ||
|
||
sw->start = buffer; | ||
sw->position = buffer; | ||
sw->capacity = len; | ||
sw->position[0] = 0; | ||
} | ||
|
||
/** | ||
* @brief Get the size of the string contained by the string writer | ||
* @param[in] sw String Writer to query | ||
* @return size of the string | ||
*/ | ||
static inline size_t string_writer_len(const string_writer_t *sw) | ||
{ | ||
return sw->position - sw->start; | ||
} | ||
|
||
/** | ||
* @brief Get the string contained by the string writer | ||
* @param[in] sw String Writer to query | ||
* @return the string assembled by string writer | ||
*/ | ||
static inline const char *string_writer_str(const string_writer_t *sw) | ||
{ | ||
return sw->start; | ||
} | ||
|
||
/** | ||
* @brief Write a formatted string to a buffer | ||
* The string will be truncated if there is not enough space left in | ||
* the destination buffer. | ||
* A terminating `\0` character is always included. | ||
* | ||
* @param[in] sw String Writer to write to | ||
* @param[in] format format string to write | ||
* | ||
* @return number of bytes written on success | ||
* -E2BIG if the string was truncated | ||
*/ | ||
int swprintf(string_writer_t *sw, const char *restrict format, ...); | ||
|
||
/* explicit_bzero is provided if: | ||
* - glibc is used as C lib (only with board natvie) | ||
* - newlib is used and __BSD_VISIBILE is set | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
* @author Benjamin Valentin <[email protected]> | ||
*/ | ||
|
||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <errno.h> | ||
#include "string_utils.h" | ||
|
||
|
@@ -50,4 +52,30 @@ const void *memchk(const void *data, uint8_t c, size_t len) | |
return NULL; | ||
} | ||
|
||
int swprintf(string_writer_t *sw, const char *restrict format, ...) | ||
{ | ||
va_list args; | ||
Teufelchen1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int res; | ||
|
||
/* make sure flash_vsnprintf() is not used */ | ||
#if HAS_FLASH_UTILS_ARCH | ||
#undef vsnprintf | ||
#endif | ||
|
||
va_start(args, format); | ||
res = vsnprintf(sw->position, sw->capacity, format, args); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clang should complain here, you can: #ifdef __clang__
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wformat-nonliteral\"") \
res = vsnprintf(sw->position, sw->capacity, format, args);
_Pragma("clang diagnostic pop")
#else
res = vsnprintf(sw->position, sw->capacity, format, args);
#endif |
||
va_end(args); | ||
|
||
if (res <= (int)sw->capacity) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if |
||
sw->capacity -= res; | ||
sw->position += res; | ||
} else { | ||
sw->position += sw->capacity; | ||
sw->capacity = 0; | ||
res = -E2BIG; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can add
to enable static printf-like compiler checks for the format string and arguments, see https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Function-Attributes.html.