The code below does not work as I expected it to.
//-----------------
import std.stdio;
void main()
{
enum string foo = "qwerty";
std.stdio.writefln("'%s'", foo);
foreach(int i, char c; foo)
writefln("[%s]%s", i, cast(int)c);
enum fob = "qwerty";
std.stdio.writefln("'%s'", fob);
foreach(int i, char c; fob)
writefln("[%s]%s", i, cast(int)c);
const bar = "qwerty";
std.stdio.writefln("'%s'", bar);
foreach(int i, char c; bar)
writefln("[%s]%s", i, cast(int)c);
}
//-----------------
I was expecting three identical strings to printout plus each character to be correct. Instead I got this ...
'qwerty'
[0]176
[1]236
[2]65
' ÿý'
[0]0
[1]0
[2]0
[3]0
[4]152
[5]236
'qwerty'
[0]113
[1]119
[2]101
[3]114
[4]116
[5]121
Which seems to me that only the form "const x " works.
Comment #1 by ddparnell — 2008-02-29T20:46:08Z
And if you think that was stringe, try this example ...
//-------------
import std.stdio;
void main()
{
enum string foo = "qwerty";
std.stdio.writefln("'%s'", foo);
foreach(int i, char c; foo)
writefln("[%s]%s", i, cast(int)c);
enum fob = "qwerty";
std.stdio.writefln("'%s'", fob);
foreach(int i, char c; fob)
writefln("[%s]%s", i, cast(int)c);
const bar = "qwerty";
std.stdio.writefln("'%s'", bar);
foreach(int i, char c; bar)
writefln("[%s]%s", i, cast(int)c);
std.stdio.writefln("'%s'", "qwerty");
foreach(int i, char c; "qwerty")
writefln("[%s]%s", i, cast(int)c);
}
//---------
Comment #4 by sardonicpresence — 2008-03-26T08:35:17Z
What I assume is the same issue still occurs within structs as of 2.012 e.g.
//-----------------
import std.stdio;
struct Test
{
public string Text;
}
enum Test test = { Text: "test" };
void main()
{
std.stdio.writefln(test);
}
//-----------------
This results in the following output:
' âA ÓâA `äA ►ëA └ëA øA á£A ►×A ǃA ñA ñA ªA '
Comment #5 by sardonicpresence — 2008-03-26T08:37:12Z
Sorry that should have been "std.stdio.writefln("'%s'", test.Text);".
Comment #6 by clugdbug — 2009-04-18T01:36:48Z
Neil's bug is actually totally different, and not related to strings in any way.--> Bug #2850.
The original bug is fixed.