3 ms·
Libxo is used in FreeBSD. I can't say I'm a fan of the approach though. Typical printf usage is imperative and additive: if (enter) printf("Hello "); else
by yyyk 3y ago
Libxo is used in FreeBSD. I can't say I'm a fan of the approach though.
Typical printf usage is imperative and additive:
if (enter)
printf("Hello ");
else
printf("Goodbye ");
printf("World!\n");
Using the format string forces the programmer to keep implicit state (the document format) all over the place or get an inconsistent document. For example, imagine the first printf calls the column 'Text' and the others call it 'Output'. We can easily do this for a single format, but the complexity will get higher the more we add.
If you do this properly (emit to an object and render from that), the result is trivially consistent. The difficulty here is to get streaming, this is however not always required and can be achieved with a little effort.
- mpweiher 3y ago> The difficulty here is to get streaming Polymorphic Write Streams do this. ACM DL: https://dl.acm.org/doi/10.1145/3359619.3359748 https://dl.acm.org/doi/10.1145/3359619.3359748 pdf: http://www.hirschfeld.org/writings/media/WeiherHirschfeld_2019_StandardObjectOutStreamingObjectsWithPolymorphicWriteStreams_AcmDL_Preprint.pdf http://www.hirschfeld.org/writings/media/WeiherHirschfeld_20... Code: https://github.com/mpw/MPWFoundation/tree/master/Streams.subproj https://github.com/mpw/MPWFoundation/tree/master/Streams.sub... Fast JSON parsing using this approach: https://blog.metaobject.com/2020/04/somewhat-less-lethargic-json-support.html https://blog.metaobject.com/2020/04/somewhat-less-lethargic-... Presentation (DLS '19): https://www.youtube.com/watch?v=DG5MtsMojgI https://www.youtube.com/watch?v=DG5MtsMojgI
- 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.
- mananaysiempre 3y ago> Typical printf usage is imperative and additive: > if (enter) printf("Hello "); else printf("Goodbye "); printf("World!\n"); And unless you want your translator to hate your guts, you really, really mustn’t do this in user-facing output. (OK, you can if you really want to and if you’re ready to give them the same tools[1], but it won’t be simple. Although I’m unaware of any professional translators supporting this either—most use a CAT, and this approach is deeply incompatible with all existing ones.) [1] https://projectfluent.org/ https://projectfluent.org/
- yyyk 3y agoWell, the issue still exists if we used regular sentences ending with a newline, allowing nice stuff for translators like printf positional arguments. The problem is that you have implicit state - the layout of the console or the columns of a json/xml - and no decent way to maintain it in a format string (because C tooling is poor, because this interface isn't entirely typesafe, etc.).