www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Bug 51] New: String cast overrides the char type of decorated string literals.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=51

           Summary: String cast overrides the char type of decorated string
                    literals.
           Product: D
           Version: 0.149
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: daiphoenix lycos.com


Observe the following code:

  char[6] cstr = "123456"c;
  auto str1 = cast(wchar[3])(cstr);
  auto str2 = cast(wchar[3])("123456"c);

  writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
  // prints: str1: 6 : 123456
  writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2)); 
  // prints: str2: 6 : ?1?2?3

One would expect the same behaviour from str1 and str2, however the
cast(wchar[3]) in str2 overrides the string decorator 'c', and makes the string
literal "123456" (i.e. the string "instance" data) be of type wchar[6] instead
of char[6].


-- 
Mar 14 2006
next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail puremagic.com schrieb am 2006-03-14:
 Observe the following code:

   char[6] cstr = "123456"c;
   auto str1 = cast(wchar[3])(cstr);
   auto str2 = cast(wchar[3])("123456"c);

   writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
   // prints: str1: 6 : 123456
   writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2)); 
   // prints: str2: 6 : ?1?2?3

 One would expect the same behaviour from str1 and str2, however the
 cast(wchar[3]) in str2 overrides the string decorator 'c', and makes the string
 literal "123456" (i.e. the string "instance" data) be of type wchar[6] instead
 of char[6].
This might seem confusing, but is the correct behaviour. http://www.digitalmars.com/d/arrays.html Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFEHRi13w+/yD4P9tIRAmgpAJ9F/KNd1JSBTBOp1QME7RA6Lwja9wCfT7Gx SlkT9jiEQ1rxtIl/cc7wT7s= =aqz7 -----END PGP SIGNATURE-----
Mar 18 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=51







 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 This might seem confusing, but is the correct behaviour.
 http://www.digitalmars.com/d/arrays.html







 Thomas
 -----BEGIN PGP SIGNATURE-----
 iD8DBQFEHRi13w+/yD4P9tIRAmgpAJ9F/KNd1JSBTBOp1QME7RA6Lwja9wCfT7Gx
 SlkT9jiEQ1rxtIl/cc7wT7s=
 =aqz7
 -----END PGP SIGNATURE-----
That behaviour is true for undecorated string literals, however for decorated string literals (i.e. those with postfix 'c', 'd', or 'w') it is not so. In http://www.digitalmars.com/d/lex.html#stringliteral it is said: "The optional Postfix character gives a specific type to the string, rather than it being inferred from the context. ..." Thus postfix-decorated string literals should not be vulnerable to type change due to casts or other context influences, no? --
Mar 19 2006
prev sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail puremagic.com schrieb am 2006-03-14:
 http://d.puremagic.com/bugzilla/show_bug.cgi?id=51
 Observe the following code:

   char[6] cstr = "123456"c;
   auto str1 = cast(wchar[3])(cstr);
   auto str2 = cast(wchar[3])("123456"c);

   writefln("str1: ", (cast(char[])str1).length , " : ", (cast(char[])str1));
   // prints: str1: 6 : 123456
   writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2)); 
   // prints: str2: 6 : ?1?2?3

 One would expect the same behaviour from str1 and str2, however the
 cast(wchar[3]) in str2 overrides the string decorator 'c', and makes the string
 literal "123456" (i.e. the string "instance" data) be of type wchar[6] instead
 of char[6].
Added to DStress as http://dstress.kuehne.cn/run/s/string_postfix_07_A.d http://dstress.kuehne.cn/run/s/string_postfix_07_B.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFEtOHcLK5blCcjpWoRAgakAKCOhY7VG1AkTF8IFOz1Ur067Dk+dQCeIlbj h9EvDdFmTjEL3U2/pdpgnqk= =rrCR -----END PGP SIGNATURE-----
Jul 12 2006