[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language)
- From: Sean Conner <sean@...>
- Date: Mon, 19 Aug 2013 05:46:29 -0400
It was thus said that the Great Coda Highland once stated:
>
> C++'s << is nice, but doesn't address reordering parameters for localization.
I attempted to solve this problem (some fifteen years ago) by writing my
own printf() replacment in C. A typical call would look like [1]:
PrintFormat(
"i $ $",
"Hi, my first name is %b and my last name is %c and I am %a years old\n",
44,
"Sean",
"Conner"
);
The first parameter is the "little language"---each space separated group
had type information ("i" for integer, "$" for string [3], "f" for float,
etc) and was used to collect the arguments after the format string. Of
course, being a custom format string and making no assumptions on the C
compiler, type checking was lacking, to say the least. You could also
specify field widths, padding, etc, but I never did find a satisfactory
method to do this, and consequently, I *always* had to look up how to
specify widths, padding, etc.
The second parameter is the format string. As you can see, you reference
variables per argument position ("%a" being the first variable argument,
"%b" being the second one, etc) and they could be reused in a string. This
provided a way to recode the format string without having to recode the
entire call site:
"Subject: %c, %b Age: %a\n"
Furthermore, I could include comments:
"Subject: %(lastname)c, %(firstname)b Age: %a\n"
which I used occasionally, but not enough to justify adding the feature [4].
I only had a few small projects use my printf() replacement, and after a few
years of never bothing to use it for everything I did [5] I eventually
removed the code and went back to using printf().
-spc (It's not an easy problem)
[1] Excuse the CamelCaps---it was a phase I was going through at the
time [2].
[2] Having come off quite a bit of development for AmigaOS, which made
extensive use of CamelCapFunctionNames.
[3] Shades of BASIC.
[4] This was part of an idea I had on supporting localization of
programs, but the idea involved changes to how the compiler
generates strings, and how the linker dealt with those changes. I
still think it's a good idea but I don't have the gumption to apply
the ideas to the GNU C development chain and trying to get upstream
to accept the changes.
[5] Partly because I wrote my own file handling routines, which,
frankly, were a nightmare to use.
- References:
- Re: [OT] Lua community and Go language, steve donovan
- printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), Jay Carlson
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), William Ahern
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), Jay Carlson
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), Andres Perera
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), Jay Carlson
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), Andres Perera
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), Tim Hill
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), steve donovan
- Re: printf is a crappy language in a crappy language (was Re: [OT] Lua community and Go language), Coda Highland