|
|
Subscribe / Log in / New account

Vetting the cargo

Vetting the cargo

Posted Jun 13, 2022 9:09 UTC (Mon) by larkey (guest, #104463)
In reply to: Vetting the cargo by wtarreau
Parent article: Vetting the cargo

The linked reference aside...:


$ cat test.c
#include <stdio.h>

int main(void)
{
	printf("<%*s>\n", 3, "");
	printf("<%*s>\n", 3, "̀a");
	printf("<%*s>\n", 3, "á");
	printf("<%*s>\n", 3, "a");
}
$ make test
cc -o test test.c
$ ./test
<   >
<̀a>
< á>
<  a>

Uh, oh, damn. Seems like your code doesn't work. Too bad. So much bad behavior in just *one* line of code! You really should question whether *you* should call yourself developer, to use your own words! Seriously: *Please* be a bit more humble and less snarky before questioning other people's creds. Thanks.


to post comments

Vetting the cargo

Posted Jun 13, 2022 11:59 UTC (Mon) by ballombe (subscriber, #9523) [Link] (1 responses)

You are making the mistake to assume that leftpad works better.
What you are trying to do is not even well defined.

Vetting the cargo

Posted Jun 13, 2022 12:59 UTC (Mon) by larkey (guest, #104463) [Link]

No, I did not assume it, in fact I assumed the opposite and checked this assumption beforehand. They're not better.

However, just as the original comment disregarded how well or badly npm-leftpad works, I chose to do so as well.

Which I demonstrated by plugging in example strings into the code given. So if there is something not-well-defined, then it's the behavior of the original comments code in the quite common case of feeding something outside of the C locale.

Regardless, although I'm not an expert in this area, I'd say even this is well-defined albeit not what's wanted. The result depends on the compilation environment, that is, how the Compiler translates the source from the respective character set into byte arrays. From there on, everything works "as intended", the first string is 3 bytes "wide, the second 2, the last just 1. Hence a padding of 0, 1 and 2 bytes conversely.

I'm obviously aware of how this is not how things work, I'm merely demonstrating how lacking the above code is and that there is a merit in using a library for this task, and that this doesn't discredit the engineer. Don't use npm-leftpad though.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds