Skip to content

Commit

Permalink
fixup! fixup! sys/string_utils: add string_writer helper
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Dec 16, 2024
1 parent 397bca5 commit 1d2bdde
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
13 changes: 11 additions & 2 deletions sys/include/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ static inline const char *string_writer_str(const string_writer_t *sw)
return sw->start;
}

#if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
#define __swprintf flash_swprintf
#else
#define __swprintf swprintf

Check failure on line 93 in sys/include/string_utils.h

View workflow job for this annotation

GitHub Actions / static-tests

Member __swprintf (macro definition) of group sys_string_utils is not documented.
#endif

/**
* @brief Write a formatted string to a buffer
* The string will be truncated if there is not enough space left in
Expand All @@ -99,8 +105,11 @@ static inline const char *string_writer_str(const string_writer_t *sw)
* @return number of bytes written on success
* -E2BIG if the string was truncated
*/
__attribute__ ((format (printf, 2, 3)))
int swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);
int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...);

#if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
#define swprintf(sw, fmt, ...) flash_swprintf(sw, TO_FLASH(fmt), ## __VA_ARGS__)
#endif

/* explicit_bzero is provided if:
* - glibc is used as C lib (only with board natvie)
Expand Down
16 changes: 9 additions & 7 deletions sys/libc/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,20 @@ const void *memchk(const void *data, uint8_t c, size_t len)
return NULL;
}

int swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...)
int __swprintf(string_writer_t *sw, FLASH_ATTR const char *restrict format, ...)
{
va_list args;
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);
#ifdef __clang__
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wformat-nonliteral\"") \
res = flash_vsnprintf(sw->position, sw->capacity, format, args);
_Pragma("clang diagnostic pop")
#else
res = flash_vsnprintf(sw->position, sw->capacity, format, args);
#endif
va_end(args);

if (res < (int)sw->capacity) {
Expand Down

0 comments on commit 1d2bdde

Please sign in to comment.