Skip to content

Add ability to skip comma flag even if no comma output implemented #73

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
#define FLAGS_LONG_LONG (1U << 9U)
#define FLAGS_PRECISION (1U << 10U)
#define FLAGS_ADAPT_EXP (1U << 11U)
#define FLAGS_COMMA (1U << 12U)


// import float.h for DBL_MAX
Expand Down Expand Up @@ -602,12 +603,13 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
flags = 0U;
do {
switch (*format) {
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break;
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break;
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break;
case '#': flags |= FLAGS_HASH; format++; n = 1U; break;
default : n = 0U; break;
case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break;
case '-': flags |= FLAGS_LEFT; format++; n = 1U; break;
case '+': flags |= FLAGS_PLUS; format++; n = 1U; break;
case ' ': flags |= FLAGS_SPACE; format++; n = 1U; break;
case '#': flags |= FLAGS_HASH; format++; n = 1U; break;
case '\'': flags |= FLAGS_COMMA; format++; n = 1U; break;
default : n = 0U; break;
}
} while (n);

Expand Down
74 changes: 72 additions & 2 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -276,6 +276,76 @@ TEST_CASE("+ flag", "[]" ) {
}


// Note: copy and pasted from "+ flag".
// Todo: Implement output of comma e.g. if environment variable is set due to lack of locale?
TEST_CASE("' flag", "[]" ) {
char buffer[100];

test::sprintf(buffer, "%'+d", 42);
REQUIRE(!strcmp(buffer, "+42"));

test::sprintf(buffer, "%'+d", -42);
REQUIRE(!strcmp(buffer, "-42"));

test::sprintf(buffer, "%'+5d", 42);
REQUIRE(!strcmp(buffer, " +42"));

test::sprintf(buffer, "%'+5d", -42);
REQUIRE(!strcmp(buffer, " -42"));

test::sprintf(buffer, "%'+15d", 42);
REQUIRE(!strcmp(buffer, " +42"));

test::sprintf(buffer, "%'+15d", -42);
REQUIRE(!strcmp(buffer, " -42"));

test::sprintf(buffer, "%'+s", "Hello testing");
REQUIRE(!strcmp(buffer, "Hello testing"));

test::sprintf(buffer, "%'+d", 1024);
REQUIRE(!strcmp(buffer, "+1024"));

test::sprintf(buffer, "%'+d", -1024);
REQUIRE(!strcmp(buffer, "-1024"));

test::sprintf(buffer, "%'+i", 1024);
REQUIRE(!strcmp(buffer, "+1024"));

test::sprintf(buffer, "%'+i", -1024);
REQUIRE(!strcmp(buffer, "-1024"));

test::sprintf(buffer, "%'+u", 1024);
REQUIRE(!strcmp(buffer, "1024"));

test::sprintf(buffer, "%'+u", 4294966272U);
REQUIRE(!strcmp(buffer, "4294966272"));

test::sprintf(buffer, "%'+o", 511);
REQUIRE(!strcmp(buffer, "777"));

test::sprintf(buffer, "%'+o", 4294966785U);
REQUIRE(!strcmp(buffer, "37777777001"));

test::sprintf(buffer, "%'+x", 305441741);
REQUIRE(!strcmp(buffer, "1234abcd"));

test::sprintf(buffer, "%'+x", 3989525555U);
REQUIRE(!strcmp(buffer, "edcb5433"));

test::sprintf(buffer, "%'+X", 305441741);
REQUIRE(!strcmp(buffer, "1234ABCD"));

test::sprintf(buffer, "%'+X", 3989525555U);
REQUIRE(!strcmp(buffer, "EDCB5433"));

test::sprintf(buffer, "%'+c", 'x');
REQUIRE(!strcmp(buffer, "x"));

test::sprintf(buffer, "%'+.0d", 0);
REQUIRE(!strcmp(buffer, "+"));
}


TEST_CASE("0 flag", "[]" ) {
char buffer[100];

Expand Down