Skip to content

Commit

Permalink
added support for %zd and %jd; added padding and minimum length speci…
Browse files Browse the repository at this point in the history
…fication for all integer types; changed version to 5.5.0
  • Loading branch information
nickeldan committed Jul 20, 2022
1 parent fd54211 commit 47812d2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
31 changes: 20 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Vanilla Squad
=============

:Author: Daniel Walker
:Version: 5.4.0
:Date: 2022-07-18
:Version: 5.5.0
:Date: 2022-07-19

Overview
========
Expand Down Expand Up @@ -40,20 +40,29 @@ The % tokens recognized by these functions are
* %lli
* %lld
* %llu
* %zu
* %zi
* %ju
* %zd
* %zu
* %ji
* %jd
* %ju
* %x
* %X
* %lx
* %lX
* %llx
* %llX
* %p
* %s
* %n
* %.*s
* %x
* %X
* %<N>[l/ll]x (e.g., %4x) (N must be a single digit)
* %<N>[l/ll]X
* %0<N>[l/ll]x (e.g., %04x)
* %0<N>[l/ll]X
* %n

In addition, zero-padding and minimum-length specification can be added to any integer type. E.g.,

.. code-block:: c
vasqSafeSnprintf(buffer, size, "%04i", 5); // "0005"
vasqSafeSnprintf(buffer, size, "%2x", 10); // " a"
Logging
=======
Expand Down
5 changes: 5 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
5.5.0:
- Added padding and minimum length specification for all integer types.
- Added support for %zd and %jd.
- Changed the handling of the %h token in logger formats to use the local time.

5.4.0:
- Added the ability to print long and long long hex values.

Expand Down
2 changes: 1 addition & 1 deletion include/vasq/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @brief Current version of the library.
*/
#define VASQ_VERSION "5.4.0"
#define VASQ_VERSION "5.5.0"

#ifndef NO_OP
#define NO_OP ((void)0)
Expand Down
45 changes: 15 additions & 30 deletions source/safe_snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ vasqSafeVsnprintf(char *buffer, size_t size, const char *format, va_list args)
unsigned int index = sizeof(subbuffer), min_length = 0;
char padding = ' ';

if (c == '0') {
padding = '0';
c = *(++format);
}
if (safeIsDigit(c)) {
if (c == '0') {
return -1;
}
min_length = c - '0';
c = *(++format);
}

if (c == 'l') {
c = *(++format);
if (c == 'l') {
Expand Down Expand Up @@ -158,7 +170,7 @@ vasqSafeVsnprintf(char *buffer, size_t size, const char *format, va_list args)
if (c == 'u') {
value = va_arg(args, size_t);
}
else if (c == 'i') {
else if (c == 'i' || c == 'd') {
is_signed = true;
signed_value = va_arg(args, ssize_t);
}
Expand All @@ -175,7 +187,7 @@ vasqSafeVsnprintf(char *buffer, size_t size, const char *format, va_list args)
if (c == 'u') {
value = va_arg(args, uintmax_t);
}
else if (c == 'i') {
else if (c == 'i' || c == 'd') {
is_signed = true;
signed_value = va_arg(args, intmax_t);
}
Expand All @@ -201,39 +213,12 @@ vasqSafeVsnprintf(char *buffer, size_t size, const char *format, va_list args)
show_hex = true;
value = (uintptr_t)va_arg(args, void *);
}
else if (safeIsDigit(c) || c == 'x' || c == 'X') {
else if (c == 'x' || c == 'X') {
show_hex = true;

if (c == '0') {
padding = '0';
c = *(++format);
}

if (safeIsDigit(c)) {
min_length = c - '0';
c = *(++format);

if (c == 'l') {
c = *(++format);
if (c == 'l') {
is_long_long = true;
c = *(++format);
}
else {
is_long = true;
}
}
}
else {
min_length = 0;
}

if (c == 'X') {
capitalize_hex = true;
}
else if (c != 'x') {
return -1;
}

value = is_long_long ? va_arg(args, unsigned long long) :
is_long ? va_arg(args, unsigned long) :
Expand Down

0 comments on commit 47812d2

Please sign in to comment.