digitalmars.D.learn - wchar[] literals
- Andrew Fedoniouk (9/9) May 06 2005 Is there any way to define wchar[] literals?
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (11/12) May 07 2005 Yes: cast(wchar[]) "string"
- Derek Parnell (22/23) May 07 2005 Yes. But it's not neat or intuitive given that syntax is available for
- =?UTF-8?B?QW5kZXJzIEYgQmrDtnJrbHVuZA==?= (17/31) May 07 2005 You can also do this: (but only with literals)
- Andrew Fedoniouk (5/16) May 07 2005 Aha, I suspected trap like this, but hope dies last...
Is there any way to define wchar[] literals? I need this: wchar[] format(wchar[] fmt, ...); togeteher with standard: char[] format(char[] fmt, ...); Also following produces compilation error "cannot cast..." bool convertTo(int i, inout wchar[] ws) { ws = i? "true":"false"; return true; } Thanks.
May 06 2005
Andrew Fedoniouk wrote:Is there any way to define wchar[] literals?Yes: cast(wchar[]) "string" I know, I know... :-) There has been a suggestion to bring back the C syntax for declaring wide strings: L"string" (would be the same as: cast(wchar[]) "string") Or maybe another letter, like w"string" perhaps? But so far it hasn't been popular... (with W) BTW; wchar[] s = "string"; // works without cast() --anders
May 07 2005
On Fri, 6 May 2005 23:36:13 -0700, Andrew Fedoniouk wrote:Is there any way to define wchar[] literals?Yes. But it's not neat or intuitive given that syntax is available for writing 'wide' integers and floating point values. cast(wchar[])"This is a Wide String Literal"; But please beware of a syntactical anomaly; you cannot do this to char[] variables and get a wchar[]. char[] A = "An ASCII string"; wchar[] W; W = cast(wchar[])A; // WRONG! This time the cast makes 'W' point to A's data and has D pretending it is a wide string, but no data conversion has actually happened. W = std.utf.toUTF16(A); // CORRECT! This is the correct way to convert narrow string variables to wide strings. So to summarize, use 'cast(wchar[])' on narrow string literals and 'std.utf.toUTF16()' on narrow string variables. Why Walters isn't interested in adding a qualifier on string literals such as he has for numbers is hard for me to understand. -- Derek Parnell Melbourne, Australia http://www.dsource.org/projects/build v2.06 is now available. 04/May/2005 7/05/2005 7:55:33 PM
May 07 2005
Derek Parnell wrote:But please beware of a syntactical anomaly; you cannot do this to char[] variables and get a wchar[]. char[] A = "An ASCII string"; wchar[] W; W = cast(wchar[])A; // WRONG! This time the cast makes 'W' point to A's data and has D pretending it is a wide string, but no data conversion has actually happened. W = std.utf.toUTF16(A); // CORRECT! This is the correct way to convert narrow string variables to wide strings.You can also do this: (but only with literals) char[] A = "An ASCII string"; wchar[] W = "An ASCII string"; char[] A = "A non-ASCII string (€)"; wchar[] W = "A non-ASCII string (€)"; And the same goes for non-literal conversions: A = cast(char[]) W; // WRONG A = std.utf.toUTF8(W); // CORRECT Note: Sometimes you have to cast "" into char[], like if you have a function overloaded with both char[] and wchar[] ? (Phobos uses a "W" prefix/suffix on wchar[] functions...) The string literals in D are of an *unnamed* string type. They are not of the type "char[]", like one might expect... (since e.g. the integer and floating literals are typed ?) --anders
May 07 2005
Thanks, guys,cast(wchar[]) "string"Aha, I suspected trap like this, but hope dies last... Andrew. "Andrew Fedoniouk" <news terrainformatica.com> wrote in message news:d5hnl6$24gn$1 digitaldaemon.com...Is there any way to define wchar[] literals? I need this: wchar[] format(wchar[] fmt, ...); togeteher with standard: char[] format(char[] fmt, ...); Also following produces compilation error "cannot cast..." bool convertTo(int i, inout wchar[] ws) { ws = i? "true":"false"; return true; } Thanks.
May 07 2005