February 28, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | On Tue, 01 Mar 2005 12:10:54 +1300, Regan Heath wrote: > On Tue, 1 Mar 2005 08:25:23 +1100, Derek <derek@psych.ward> wrote: >> Second >> advice, if you are writing out the contents of strings, always use a %s. >> This will help avoid embedded '%' in your input string. >> >> writefln("%s", temp[0]); > > IIRC all parameters of writefln are treated as format strings, meaning, if temp[0] above contains a %s, another parameter is expected. I suspect you recall incorrectly ;-) Try this ... import std.stdio; void main() { writefln("%s", "%d", 1); } The output is ... %d1 and not %d11 if all strings were formatting strings. If writef is not processing a format string, and the next parameter *is* a format string, then is 'eats up' the following parameters according to the format string. So writef sees ("%s", "%d", 1) and assumes the first parameter is a potential format string, until if comes across the %s, when it now knows it is one. So it uses the %s to 'eat up' the next string "%d", until it comes to the end of the format string. The next parameter if looks at is then the next 'non-eaten' one, the integer 1! > The only way around this is to process every string you print with the write functions, changing % to %%. So one does *not* have to convert all embedded % to %%. > On one hand this is bad(tm). You have to process all strings, there are many potential bugs/problems. Which I've just shown is not an issue. > On the other hand this is good(tm). You are able to use a format string > anywhere eg. > writefln("%5s",a,b,i,"%.6f",f); > > if probably more easily understood, and less prone to bugs (wrong format > specifier) than: > writefln("%5s %s %d %.6f",a,b,i,f); That is a personal opinion. I actually prefer the version you do not prefer. > If the write functions could somehow tell a format string apart from a normal string. > > eg. > > char[] a = "%s and %s"; > char[] b = "pass"; > char[] c = "fail" > > writefln(f"%s and %s",b,c); > writefln(format(a),b,c); > > this seems to be a pain to me, someone might forget to use format() on a var, of forget to prefix with f and wonder why it's not working. Good thing we don't need this after all ;-) > Perhaps the opposite is better. > > char[] a = "%s and %s"; > char[] b = "pass"; > char[] c = "fail" > > writefln("%s and %s",noformat(b),noformat(c)); > writefln(a,noformat(b),noformat(c)); > > but then, it's more likely you have less format strings, so neither of these options seem very nice to me. > > Does anyone have any good ideas? Yep, Leave it as it is. -- Derek Melbourne, Australia 1/03/2005 10:16:52 AM |
February 28, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Tue, 01 Mar 2005 00:21:16 +0100, Anders F Björklund wrote: [snip] > > I wrote a patch for a "writeln" function that was the same > as writefln, but without the formats. (i.e. all strings > are normal, no need to double up the percentages anywhere) > > writefln("Hello!"); > writeln("100%"); > > It's working just great. You can still use variadic args: > > writeln(100, "%"); Can you submit to Walter for inclusion into his Phobos, or maybe post it here for us? I suppose I could write one too, but if you've done the hard work already... ;-) -- Derek Melbourne, Australia 1/03/2005 10:26:57 AM |
February 28, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote: > Can you submit to Walter for inclusion into his Phobos, or maybe post it > here for us? I suppose I could write one too, but if you've done the hard > work already... ;-) I did. Haven't heard a thing. http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/15627 As you might see, I took the short and simple road... :-) --anders |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek | Derek wrote: >>so, printf is going to be my last result from now on. >>All of my print-outs will be done with writef. >> >>Please help me understand this. > > I'll give it a go ;-) > > My advice firstly though is to stop using printf, just use writef. Well, just as long as one keeps them apart (C and D), all is OK... > import std.c.stdio; > > int main() > { > char* s = "World"; > printf("Hello, %s!\n", s); > return 0; > } vs. > import std.stdio; > > void main() > { > char[] s = "World"; > writefln("Hello, %s!", s); > } Sometimes it is very useful to be able to write "C programs" in D. If nothing else, it makes porting much easier: http://www.algonet.se/~afb/d/gdtest.d --anders |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | On Tue, 1 Mar 2005 10:23:55 +1100, Derek Parnell <derek@psych.ward> wrote: > On Tue, 01 Mar 2005 12:10:54 +1300, Regan Heath wrote: > >> On Tue, 1 Mar 2005 08:25:23 +1100, Derek <derek@psych.ward> wrote: >>> Second >>> advice, if you are writing out the contents of strings, always use a %s. >>> This will help avoid embedded '%' in your input string. >>> >>> writefln("%s", temp[0]); >> >> IIRC all parameters of writefln are treated as format strings, meaning, if >> temp[0] above contains a %s, another parameter is expected. > > I suspect you recall incorrectly ;-) Try this ... Sorry, I wasn't as clear as I should have been. If you use "%s" then the next argument is not treated as a format string, I know that. However, I still think there is a (perhaps small) problem eg. char[] a = "%s for a while"; writefln(a); sure, the above is engineered to show a problem, sure it's simple to see the problem in the above, but, this could occur in a more complex situation i.e. the string comes from 'elsewhere' You suggested always using "%s", that does work, it's the same thing we have to do with printf.. it's one of those "lessons" we learn, one that bites us on the a#@% once, or more, until we learn it. I was hoping D could improve on it. Anders functions which do not use format strings at all are one way to solve the problem, but it means if you want to format some of your output you must make several function calls, perhaps this is the best we can do? I was hoping we could come up with something better. >> On the other hand this is good(tm). You are able to use a format string anywhere eg. >> writefln("%5s",a,b,i,"%.6f",f); >> if probably more easily understood, and less prone to bugs (wrong format specifier) than: >> writefln("%5s %s %d %.6f",a,b,i,f); >That is a personal opinion. I actually prefer the version you do not > prefer. Perhaps because you are used to it? This isn't really important anyway :) Regan |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Tue, 01 Mar 2005 01:05:55 +0100, Anders F Björklund wrote: > Derek wrote: > >>>so, printf is going to be my last result from now on. >>>All of my print-outs will be done with writef. >>> >>>Please help me understand this. >> >> I'll give it a go ;-) >> >> My advice firstly though is to stop using printf, just use writef. > > Well, just as long as one keeps them apart (C and D), all is OK... As is my intention, :D >> import std.c.stdio; >> >> int main() >> { >> char* s = "World"; >> printf("Hello, %s!\n", s); >> return 0; >> } > > vs. > >> import std.stdio; >> >> void main() >> { >> char[] s = "World"; >> writefln("Hello, %s!", s); >> } > Sometimes it is very useful to be able to write "C programs" in D. Seems to be a good reason to throw C away ;-) > > If nothing else, it makes porting much easier: http://www.algonet.se/~afb/d/gdtest.d > No doubt you are right, but I'm not intending to port any C/C++ code. I'm writing in D and I want to think in D. I'm happy to translate C to D, but not to copy it slavishly. -- Derek Melbourne, Australia 1/03/2005 11:10:36 AM |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote: >>Sometimes it is very useful to be able to write "C programs" in D. > > Seems to be a good reason to throw C away ;-) :-) (but perhaps a good enough reason to drop printf from object ?) One can write "C programs" in C++ too. (and way too many still do) >>If nothing else, it makes porting much easier: >>http://www.algonet.se/~afb/d/gdtest.d > > No doubt you are right, but I'm not intending to port any C/C++ code. I'm > writing in D and I want to think in D. I'm happy to translate C to D, but > not to copy it slavishly. Me neither, I just wanted a "quick start" to see whether my ports of big header files was working OK before I started adding D stuff. You probably still have to *interface* to C code/libraries, though ? --anders |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: > Anders functions which do not use format strings at all are one way to solve the problem, but it means if you want to format some of your output you must make several function calls, perhaps this is the best we can do? Mixing and matching different styles in one call sounds like a good way to get hopelessly confused about which rules apply... Having both in the language is good, but in one function ? Nah. > I was hoping we could come up with something better. > >>> On the other hand this is good(tm). You are able to use a format string anywhere eg. >>> writefln("%5s",a,b,i,"%.6f",f); >>> if probably more easily understood, and less prone to bugs (wrong format specifier) than: >>> writefln("%5s %s %d %.6f",a,b,i,f); >> >> That is a personal opinion. I actually prefer the version you do not >> prefer. > > Perhaps because you are used to it? > This isn't really important anyway :) C has "printf", Java has "println" (but just got printf in JDK 1.5, before that you used methods and hairpulling to format Java output http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html) D could have *both*, and everyone is happy ? :-) Heck, you can even use the C++ bitshift operators if you prefer, since they're in Mango... http://svn.dsource.org/svn/projects/mango/trunk/doc/html/classStdio.html --anders |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | On Tue, 01 Mar 2005 01:32:00 +0100, Anders F Björklund <afb@algonet.se> wrote: > Regan Heath wrote: > >> Anders functions which do not use format strings at all are one way to solve the problem, but it means if you want to format some of your output you must make several function calls, perhaps this is the best we can do? > > Mixing and matching different styles in one call sounds like a > good way to get hopelessly confused about which rules apply... "styles"? I assume you mean, "printing with formatting" and "printing without formatting'? I don't think "printing without formatting" exists. Instead "printing in the default specified format" is what we do. I don't see how it's confusing to want to print 'a' in a specified format and 'b' in the default specified format in one function call. eg. printf("%d %f",a,b); prints a and b in the default format. printf("%05d %f",a,b); prints a in the specified format and b in the default format. You still have to know what the default format is. > Having both in the language is good, but in one function ? Nah. Perhaps. I'm not convinced yet. Regan |
March 01, 2005 Re: to printf or to writef? That is the question... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: >> Mixing and matching different styles in one call sounds like a >> good way to get hopelessly confused about which rules apply... > > "styles"? > I assume you mean, "printing with formatting" and "printing without formatting'? Yes, as in printf(3) vs. java.lang.System.out.println > I don't think "printing without formatting" exists. Instead "printing in the default specified format" is what we do. Which was why I hacked "printing without formatting" in :-) > I don't see how it's confusing to want to print 'a' in a specified format and 'b' in the default specified format in one function call. eg. > > printf("%d %f",a,b); > > prints a and b in the default format. > > printf("%05d %f",a,b); > > prints a in the specified format and b in the default format. > > You still have to know what the default format is. Those are all good... (examples of using C-style printf) What I meant was confusing was when you mix and match, like in: > writefln("%5s",a,b,i,"%.6f",f); But that could just be me not being used to D's writef just yet... (not too strange really, since it hasn't been around all that long?) There's also *another* way (Java) of doing the same thing: System.out.println(a + " " + b); Which in D would become something like: std.stdio.writeln (std.string.toString(a) ~ " " ~ std.string.toString(b)); The formatting options totally sucks here, of course. (It's a "known bug" of Java, before JDK 1.5... :-) ) But it doesn't have any "problems" with percent strings ? >> Having both in the language is good, but in one function ? Nah. > > Perhaps. I'm not convinced yet. It's not like writef is going away, or anything... Full example: > import std.stdio; > import std.string; > > void main() > { > int a = 42; > float b = 3.1415; > > writefln("%d %f",a,b); > writeln(toString(a) ~ " " ~ toString(b)); > } --anders |
Copyright © 1999-2021 by the D Language Foundation