Skip to content
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

Bad handling of "-0" flags. #39

Open
SandorDobos opened this issue Feb 7, 2021 · 0 comments
Open

Bad handling of "-0" flags. #39

SandorDobos opened this issue Feb 7, 2021 · 0 comments

Comments

@SandorDobos
Copy link

PrintEx doesn't handle format strings like "%-05d" according to the printf(3) manual page:

If the 0 and - flags both appear, the 0 flag is ignored. 
...  
A - overrides a 0 if both are given.

This results in very bad outputs:

#include <PrintEx.h>

void setup() {
  Serial.begin(115200);
  PrintEx serial = Serial;

  // according the printf(3) manual on handling '-' and '0' flags 
  // the outputs below are bad:
  
  // outputs: "[-3000]", expected: "[-3   ]"
  serial.printf("[%-05d]\n", -3);

  // outputs: [30000], expected: "[3    ]"
  serial.printf("[%-05d]\n", 3);
}

void loop() {
  // no op
}

The fix is as simple as modifying 181. line of PrintExtension.cpp
from:

if( formatTest( format, CHAR_ZERO ) )  pad |= PAD_ZERO;

to:

// formatTest should been always called to step over ignored 0s
if( formatTest( format, CHAR_ZERO ) && pad == 0)  pad |= PAD_ZERO;

(I am sorry but have no time to create a pull request.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant