3 ms·
> Using the format string forces the programmer to keep implicit state (the document format) all over the place or get an inconsistent document. I'm not unders
by lelanthran 3y ago
> Using the format string forces the programmer to keep implicit state (the document format) all over the place or get an inconsistent document.
I'm not understanding your objection[1]; surely you would only define the libxo format string once, and then reuse it everywhere? Without libxo you'd need to duplicate your code everywhere for every output format you want to support.
IOW, you'd have to construct your libxo format string using a string concatenation library, something like this:
const char *s1 = "{:Text%7ju}";
const char *s2 = "{:Output%7ju}";
char *final = NULL;
if (enter) {
final = strdup (s1);
} else {
final = strdup (s2);
}
final = strconcat (final, s2);
Isn't that a better mechanism than printf?
[1] It's late, I've the flu and feeling a little stupid right now. Also, this is the first I've seen this project.
- yyyk 3y ago>surely you would only define the libxo format string once, and then reuse it everywhere I would be rather scared of using a variable for a format string. IMHO, these types of format strings are an antipattern for a different reason - we don't see the format at point of use, if we make some too easy mistakes, we have a crash or CVE*. I think nowaways there are tools to do some verification**, and I guess we could use an IDE (but most C programmers don't?), but I am unfamiliar with any such tool which supports libxo style format strings. * https://en.wikipedia.org/wiki/Format_string_attack https://en.wikipedia.org/wiki/Format_string_attack ** IIRC, GCC/Clang eventually added a verifier? But it doesn't apply to all cases or scanf? I don't recall.