Skip to content

Commit

Permalink
Merge branch 'size_t_specifier_support'
Browse files Browse the repository at this point in the history
  • Loading branch information
mpaland committed Apr 17, 2018
2 parents 896b2ce + 281e44d commit ca6e7e5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ The length sub-specifier modifies the length of the data type.
| (none) | int | unsigned int |
| l | long int | unsigned long int |
| ll | long long int | unsigned long long int |
| z | size_t int | unsigned size_t int |


## Compiler switches/defines
Expand Down
4 changes: 4 additions & 0 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ static size_t _vsnprintf(char* buffer, size_t buffer_len, const char* format, va
flags |= FLAGS_LONG_LONG;
format++;
}
if (*format == 'z') {
flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++;
}

// evaluate specifier
switch (*format) {
Expand Down
17 changes: 16 additions & 1 deletion test/test_suite.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
// \author (c) Marco Paland ([email protected])
// 2017, PALANDesign Hannover, Germany
// 2017-2018, PALANDesign Hannover, Germany
//
// \license The MIT License (MIT)
//
Expand Down Expand Up @@ -923,6 +923,21 @@ TEST_CASE("types", "[]" ) {
test::sprintf(buffer, "%llu", 18446744073709551615LLU);
REQUIRE(!strcmp(buffer, "18446744073709551615"));

test::sprintf(buffer, "%zu", 2147483647UL);
REQUIRE(!strcmp(buffer, "2147483647"));

test::sprintf(buffer, "%zd", 2147483647UL);
REQUIRE(!strcmp(buffer, "2147483647"));

if (sizeof(size_t) == sizeof(long)) {
test::sprintf(buffer, "%zi", -2147483647L);
REQUIRE(!strcmp(buffer, "-2147483647"));
}
else {
test::sprintf(buffer, "%zi", -2147483647LL);
REQUIRE(!strcmp(buffer, "-2147483647"));
}

test::sprintf(buffer, "%b", 60000);
REQUIRE(!strcmp(buffer, "1110101001100000"));

Expand Down

0 comments on commit ca6e7e5

Please sign in to comment.