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

sprintf '#' flag with %o does not conform to POSIX #353

Closed
dmaestro opened this issue Apr 16, 2017 · 0 comments
Closed

sprintf '#' flag with %o does not conform to POSIX #353

dmaestro opened this issue Apr 16, 2017 · 0 comments

Comments

@dmaestro
Copy link

Assuming we don't want to depart from POSIX behavior in our sprintf function.

The # flag with %o and precision is described in Perl 6 Documentation to follow POSIX by conditionally padding beyond the given precision, only if necessary to preserve the c-style 0-prefix on the octal output.
My linux stdio lib follows this behavior:

doug@ender:~/study/Perl6$ echo -e '#include <stdio.h>\n#include <iostream>\nusing namespace std;\n
int main() { char* s = new char[10]; sprintf(s, "%#.5o\\n", 012345); cout<<s; }' | gcc -x c++ -lstdc++ - ; ./a.out
012345
doug@ender:~/study/Perl6$ echo -e '#include <stdio.h>\n#include <iostream>\nusing namespace std;\n
int main() { char* s = new char[10]; sprintf(s, "%.5o\\n", 012345); cout<<s; }' | gcc -x c++ -lstdc++ - ; ./a.out
12345
doug@ender:~/study/Perl6$ echo -e '#include <stdio.h>\n#include <iostream>\nusing namespace std;\n
int main() { char* s = new char[10]; sprintf(s, "%#.5o\\n", 0123); cout<<s; }' | gcc -x c++ -lstdc++ - ; ./a.out
00123
doug@ender:~/study/Perl6$ echo -e '#include <stdio.h>\n#include <iostream>\nusing namespace std;\n
int main() { char* s = new char[10]; sprintf(s, "%.5o\\n", 0123); cout<<s; }' | gcc -x c++ -lstdc++ - ; ./a.out
00123

However, Rakudo Perl6 departs from this behavior:

[13:11] <dmaestro> m: say sprintf "%.5o", 0o123
[13:11] <camelia> rakudo-moar c95c4a: OUTPUT: «00123␤»
[13:11] <dmaestro> m: say sprintf "%#.5o", 0o123
[13:11] <camelia> rakudo-moar c95c4a: OUTPUT: «000123␤»   # uh-uh
[13:11] <dmaestro> m: say sprintf "%.5o", 0o12345
[13:11] <camelia> rakudo-moar c95c4a: OUTPUT: «12345␤»
[13:12] <dmaestro> m: say sprintf "%#.5o", 0o12345
[13:12] <camelia> rakudo-moar c95c4a: OUTPUT: «012345␤»
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

2 participants