March 21, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | Except for the fact that va_lists are not type aware.Thus the whole format string telling print what the arguements types are. I don't know of anything else in D that changes this. Or am i confused? In article <b5fm9d$u9e$1@digitaldaemon.com>, Sean L. Palmer says... > >This is why putting the printed values after the format string isn't a good idea. > >It should be: > >int n=5; >char[] s = "wow"; >char c = '.'; >char[] fmt = format("I am printing a number here ", n, " and then a string >", s, " and finally a period", c); > >But apparently printf rules, so why try anything else? > >Sean |
March 22, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | Format string are not that bad. Consider: we are living in an international world. Your format string needs not be stored in the program, it may be in the database. Different languages may have different order of words.
The solution would be a formatting operator which would take a format string on the left side, and an associative array of strings at the right side. The format string would then contain parameter names, which would be then have to be contained as indizes in an assoziative array. I'm not exactly sure whether there's a convinient way to define an associative array literal in D in-line, IMO there should. If there isn't, a multiarg function can be created to take pairs of string literals and make an assoziative aray out of them. Obviously, this is even more time-consuming than simple format-string procesing. But where "time matters", Sean's format function serves much better than printf.
Compile-time typechecking can not be expessed in terms of the D language and would thus requiere to be "special"...
-i.
Sean L. Palmer wrote:
> This is why putting the printed values after the format string isn't a good
> idea.
>
> It should be:
>
> int n=5;
> char[] s = "wow";
> char c = '.';
> char[] fmt = format("I am printing a number here ", n, " and then a string
> ", s, " and finally a period", c);
>
> But apparently printf rules, so why try anything else?
>
> Sean
|
March 23, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | Ilya Minkov wrote: > Format string are not that bad. Consider: we are living in an international world. Your format string needs not be stored in the program, it may be in the database. Different languages may have different order of words. > > The solution would be a formatting operator which would take a format string on the left side, and an associative array of strings at the right side. The format string would then contain parameter names, which would be then have to be contained as indizes in an assoziative array. I'm not exactly sure whether there's a convinient way to define an associative array literal in D in-line, IMO there should. If there isn't, a multiarg function can be created to take pairs of string literals and make an assoziative aray out of them. Obviously, this is even more time-consuming than simple format-string procesing. But where "time matters", Sean's format function serves much better than printf. > > Compile-time typechecking can not be expessed in terms of the D language and would thus requiere to be "special"... > > -i. I was thinking about allowing some type information in the insertion string, and then having the formatter throw an exception if the expected type is not given. Something like: format("Name: {%s}\tAge: {%i}\tpi: {%f3.8}") % name % age % pi; Where the %f bit is for precision. The alternative is to just copy printf's syntax, which, come to think of it, is probably better because of the familiarity factor. Compile-time checking could be done, but I doubt it's worth it. You'd have to chain methods. format("blah {%} {%}").insertString("blah").insertFloat(3.14); // ew By the way, I coded that, not Sean. :) (he added the console methods for repositioning the cursor, and colouring things) --andy |
April 01, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | Andy Friesen wrote: > I got this idea from boost. Seems to work pretty nicely. Comments/etc welcome. > > Console.io.write(format("String: '{%}' {%} + {%} = {%}") % "A string!" % 5 % 3 % (5+3)); Updated. http://www.ikagames.com/andy/d/formatter.zip char[] isFalse = "IS FALSE WHEN PRECEDED BY ITS QUOTATION"; char[] str = new Formatter("\"{0}\" {0}. {1} + {2} = {3}") % isFalse % 5 % 3 % (5+3) % Formatter.end; As always, comments welcome. |
April 03, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "Sean L. Palmer" <seanpalmer@directvinternet.com> wrote in message news:b5fm9d$u9e$1@digitaldaemon.com... > This is why putting the printed values after the format string isn't a good > idea. > > It should be: > > int n=5; > char[] s = "wow"; > char c = '.'; > char[] fmt = format("I am printing a number here ", n, " and then a string > ", s, " and finally a period", c); > > But apparently printf rules, so why try anything else? Why, indeed <g>. Note that printf also results in the smallest code generated. |
April 03, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to eluusive | <eluusive@NOMAPSsbclgobal.net> wrote in message news:b5ftto$14es$1@digitaldaemon.com... > Except for the fact that va_lists are not type aware.Thus the whole format string telling print what the arguements types are. I don't know of anything > else in D that changes this. Or am i confused? No, you're not confused. Variable argument lists in D work just like they do in C/C++. |
April 04, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | I'm not so sure about that. A) printf has to parse all those format specifiers. That slows printing down. B) printf pulls in all the formatting and conversion functions even if you just want to do this: printf("Hello world"); Maybe in certain cases it results in smaller code, but not in every case. Sean "Walter" <walter@digitalmars.com> wrote in message news:b6ieen$udg$3@digitaldaemon.com... > > But apparently printf rules, so why try anything else? > > Why, indeed <g>. Note that printf also results in the smallest code generated. |
April 15, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | "Andy Friesen" <andy@ikagames.com> wrote in message news:b6d82j$2qg$1@digitaldaemon.com... > Andy Friesen wrote: > > I got this idea from boost. Seems to work pretty nicely. Comments/etc welcome. > > > > Console.io.write(format("String: '{%}' {%} + {%} = {%}") % "A string!" % > > 5 % 3 % (5+3)); > > Updated. http://www.ikagames.com/andy/d/formatter.zip > > char[] isFalse = "IS FALSE WHEN PRECEDED BY ITS QUOTATION"; > char[] str = new Formatter("\"{0}\" {0}. {1} + {2} = {3}") % > isFalse % 5 % 3 % (5+3) % Formatter.end; > > As always, comments welcome. > I had a ZX Spectrum back in 1985 and I could do a (in its embedded Basic): print 1, "aaaa", 2, " The quick brown fox is ", var1 which will print: 1aaaa2 The quick brown fox is 3.5 assuming of course var1 is of type real. Why can't it be so simple ? |
May 04, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "Sean L. Palmer" <palmer.sean@verizon.net> wrote in message news:b6jf3b$1m3r$1@digitaldaemon.com... > I'm not so sure about that. > > A) printf has to parse all those format specifiers. That slows printing > down. > B) printf pulls in all the formatting and conversion functions even if you > just want to do this: > > printf("Hello world"); > > Maybe in certain cases it results in smaller code, but not in every case. printf is the smallest code to call, although the function itself is substantial. Hence, if you have a lot of calls to printf, the savings are substantial. Me, I always thought printf should just be built in to the operating system, so every program shares one copy. |
May 04, 2003 Re: String formatting stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Achilleas Margaritis | "Achilleas Margaritis" <axilmar@in.gr> wrote in message news:b7h964$ub5$1@digitaldaemon.com... > > "Andy Friesen" <andy@ikagames.com> wrote in message news:b6d82j$2qg$1@digitaldaemon.com... > > Andy Friesen wrote: > > > I got this idea from boost. Seems to work pretty nicely. Comments/etc > > > welcome. > > > > > > Console.io.write(format("String: '{%}' {%} + {%} = {%}") % "A string!" % > > > 5 % 3 % (5+3)); > > > > Updated. http://www.ikagames.com/andy/d/formatter.zip > > > > char[] isFalse = "IS FALSE WHEN PRECEDED BY ITS QUOTATION"; > > char[] str = new Formatter("\"{0}\" {0}. {1} + {2} = {3}") % > > isFalse % 5 % 3 % (5+3) % Formatter.end; > > > > As always, comments welcome. > > > > I had a ZX Spectrum back in 1985 and I could do a (in its embedded Basic): > > print 1, "aaaa", 2, " The quick brown fox is ", var1 > > which will print: > > 1aaaa2 The quick brown fox is 3.5 > > assuming of course var1 is of type real. > > Why can't it be so simple ? The trouble comes from if you want a %2d format, or %x format, etc., i.e. something other than the default. |
Copyright © 1999-2021 by the D Language Foundation