Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
August 09, 2005 2 bugs: format / printf | ||||
---|---|---|---|---|
| ||||
I suppose format and printf accept the same format string. If you comment out the line marked "here", there's a runtime error with format. $ cat fmt.d ----------------------------- import std.string; int main(char[][] args) { byte c = -1; char ch; printf( "0x%02hhX\n", c ); // OK printf(format("0x%02hhX\n", c)); // runtime Error: std.format formatArg printf("here"~ch); // compiler crash return 0; } ----------------------------- $ dmd fmt.d gdc -c fmt.d -o fmt.o fmt.d: In function `main': fmt.d:9: internal compiler error: in rawArray, at d/d-codegen.cc:981 Please submit a full bug report, with preprocessed source if appropriate. See <URL:http://gcc.gnu.org/bugs.html> for instructions. |
August 09, 2005 Re: 2 bugs: format / printf | ||||
---|---|---|---|---|
| ||||
Posted in reply to bug | bug@d.com wrote: > I suppose format and printf accept the same format string. Not exactly, actually. > $ cat fmt.d > ----------------------------- > import std.string; > > int main(char[][] args) > { > byte c = -1; > char ch; > printf( "0x%02hhX\n", c ); // OK > printf(format("0x%02hhX\n", c)); // runtime Error: std.format formatArg > printf("here"~ch); // compiler crash > return 0; > } > ----------------------------- I'm not surprised you got errors... you are passing D-style strings and slices to printf, which expects C-style strings. For instance you could change this line: # printf(format("0x%02hhX\n", c)); to this one: # printf(toStringz(format("0x%02hhX\n", c))); And change this line: # printf("here"~ch); to this one: # printf("%.*s", "here"~ch); //.*s is compatable with D-style strings/slices Or, better yet, just import 'std.stdio' and use writef/writefln in place of printf. Then you have no problems, and can even scrap your call to format in this case. So your code becomes: # import std.stdio ; # # int main (in char[][] args) { # byte c = -1 ; # char ch ; # # writefln("0x%02hhX", c); # writefln("here" ~ ch); # return 0; # } -- Chris Sauls |
August 09, 2005 3 bugs Re: 2 bugs: format | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Maybe my choose of printf is misleading. I only want to write a small example to show the bug. For the 2nd bug: at any rate the compile can report errors, but should not crash. Let's see the new example: $ cat fmt.d ----------------------------- import std.string; void doNothing(char[] s) {} int main(char[][] args) { int i; byte c = -1; char ch; doNothing(format("0x%02hhX\n", c)); // Error: std.format formatArg doNothing("here"~ch); // compiler crash fmt.d:13: internal compiler error: in rawArray, at d/d-codegen.cc:981 doNothing(format("// %d: %.*s\n", i, "there")); //Error: std.format int argument expected return 0; } ----------------------------- In article <ddb00l$9u0$1@digitaldaemon.com>, Chris Sauls says... > >bug@d.com wrote: >> I suppose format and printf accept the same format string. >Not exactly, actually. > > >> $ cat fmt.d >> ----------------------------- >> import std.string; >> >> int main(char[][] args) >> { >> byte c = -1; >> char ch; >> printf( "0x%02hhX\n", c ); // OK >> printf(format("0x%02hhX\n", c)); // runtime Error: std.format formatArg >> printf("here"~ch); // compiler crash >> return 0; >> } >> ----------------------------- > >I'm not surprised you got errors... you are passing D-style strings and slices to printf, >which expects C-style strings. For instance you could change this line: ># printf(format("0x%02hhX\n", c)); > >to this one: ># printf(toStringz(format("0x%02hhX\n", c))); > >And change this line: ># printf("here"~ch); > >to this one: ># printf("%.*s", "here"~ch); //.*s is compatable with D-style strings/slices > >Or, better yet, just import 'std.stdio' and use writef/writefln in place of printf. Then you have no problems, and can even scrap your call to format in this case. So your code becomes: > ># import std.stdio ; ># ># int main (in char[][] args) { ># byte c = -1 ; ># char ch ; ># ># writefln("0x%02hhX", c); ># writefln("here" ~ ch); ># return 0; ># } > >-- Chris Sauls |
August 09, 2005 Re: 3 bugs Re: 2 bugs: format | ||||
---|---|---|---|---|
| ||||
Posted in reply to bug | Sorry, looks like format(...) do not accept format string e.g. "%d..." at all. So the 1st and 3rd are not bug. Only the 2nd compiler crash is a real bug. In article <ddb81r$ihe$1@digitaldaemon.com>, bug@d.com says... > >Maybe my choose of printf is misleading. I only want to write a small example to show the bug. > >For the 2nd bug: at any rate the compile can report errors, but should not crash. > >Let's see the new example: > >$ cat fmt.d >----------------------------- >import std.string; > >void doNothing(char[] s) {} > >int main(char[][] args) >{ >int i; >byte c = -1; >char ch; > >doNothing(format("0x%02hhX\n", c)); // Error: std.format formatArg >doNothing("here"~ch); // compiler crash fmt.d:13: internal compiler error: in >rawArray, at d/d-codegen.cc:981 >doNothing(format("// %d: %.*s\n", i, "there")); //Error: std.format int argument >expected > >return 0; >} >----------------------------- > > >In article <ddb00l$9u0$1@digitaldaemon.com>, Chris Sauls says... >> >>bug@d.com wrote: >>> I suppose format and printf accept the same format string. >>Not exactly, actually. >> >> >>> $ cat fmt.d >>> ----------------------------- >>> import std.string; >>> >>> int main(char[][] args) >>> { >>> byte c = -1; >>> char ch; >>> printf( "0x%02hhX\n", c ); // OK >>> printf(format("0x%02hhX\n", c)); // runtime Error: std.format formatArg >>> printf("here"~ch); // compiler crash >>> return 0; >>> } >>> ----------------------------- >> >>I'm not surprised you got errors... you are passing D-style strings and slices to printf, >>which expects C-style strings. For instance you could change this line: >># printf(format("0x%02hhX\n", c)); >> >>to this one: >># printf(toStringz(format("0x%02hhX\n", c))); >> >>And change this line: >># printf("here"~ch); >> >>to this one: >># printf("%.*s", "here"~ch); //.*s is compatable with D-style strings/slices >> >>Or, better yet, just import 'std.stdio' and use writef/writefln in place of printf. Then you have no problems, and can even scrap your call to format in this case. So your code becomes: >> >># import std.stdio ; >># >># int main (in char[][] args) { >># byte c = -1 ; >># char ch ; >># >># writefln("0x%02hhX", c); >># writefln("here" ~ ch); >># return 0; >># } >> >>-- Chris Sauls > > |
August 10, 2005 Re: 3 bugs Re: 2 bugs: format | ||||
---|---|---|---|---|
| ||||
Posted in reply to bug | bug@d.com wrote:
> Only the 2nd compiler crash is a real bug.
Both the snippets you posted compile fine with DMD 0.129 under Windows.
|
August 10, 2005 Re: 2 bugs: format / printf | ||||
---|---|---|---|---|
| ||||
Posted in reply to bug | bug@d.com wrote: > I suppose format and printf accept the same format string. If you comment out > the line marked "here", there's a runtime error with format. <snip> No, format and writef accept the same format string. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on on the 'group where everyone may benefit. |
Copyright © 1999-2021 by the D Language Foundation