4 ms·
> C's stdlib, `math.h`, `string.h` are good but don't have string padding functions Of course they do; how would you align printouts otherwise? $ cat main
by rdancer 11y ago
> C's stdlib, `math.h`, `string.h` are good but don't have string padding functions
Of course they do; how would you align printouts otherwise?
$ cat main.c
#include <stdio.h>
int main()
{
(void) printf ("%42s\n", "hello"); // right-align, 42 chars
(void) printf ("%-42s\n", "hello"); // left-align
}
$ gcc main.c
$ ./a.out | cat -A
hello$
hello $
$ ./a.out | wc -L
42
- afandian 11y agoOf course you can use `asprintf` to pad a string like you can use a for loop to pad a string in JS. But it's not a dedicated named function to pad strings like the `leftpad` we're discussing. Kind of missing the point of the argument. Given how simple and popular `left-pad` module is (indicating the principle the community uses to determine granularity), I'd guess that if `String.format` was available in JavaScript there would still be a library to provide `leftpad`.
- fyolnish 11y agoIt's a memset followed by a strncpy, hardly worth linking to a library for. You'd be done writing the function before you finish googling for an existing one.
- afandian 11y agoThat's the whole point of the discussion.
- rdancer 11y agoI think the difference is that when a C programmer hits an undefined behaviour, they either blames the spec, the compiler, or themselves. A JavaScript programmer doesn't need a prompt to blame the spec proliferation and dilution, the necessity to write cross-platform by default, and their lack of foresight in not including a 3rd-party library that would have provided some vestiges of a targetable platform. The C code that started as a nice asprintf(3) for GNU/Linux will sprout a few if-defs once it has to run on some embedded PoS without a sane malloc().
- rdancer 11y agoA function is dedicated if it is well-suited for a use case. It does not require it to be ill-suited for other use cases. Either you arguing that `left-pad` is not a "dedicated" function for padding by 42 characters, because it can also pad for 43 or 41, or I don't understand you at all?
- afandian 11y agoThe point is that it's easy to pad a string in C and easy to pad a string in JS but there's no specific function called 'padstring' in either. The examples were really secondary to the point of my argument, which was about packaging functions into modules, not about the C language.