Re: String Literals

2022-05-03 Thread user1234 via Digitalmars-d-learn
On Tuesday, 3 May 2022 at 17:21:47 UTC, JG wrote: Hi, The specification of string literals has either some errors or I don't understand what is meant by a Character. [...] Which to me means that e.g. r""" should be a WysiwygString, which the compiler thinks is not (not s

String Literals

2022-05-03 Thread JG via Digitalmars-d-learn
Hi, The specification of string literals has either some errors or I don't understand what is meant by a Character. For instance we have: WysiwygString: r" WysiwygCharacters_opt " StringPostfix_opt WysiwygCharacters: WysiwygCharacter WysiwygCharacter Wy

Re: == comparison of string literals, and their usage

2019-04-07 Thread diniz via Digitalmars-d-learn
Le 07/04/2019 à 14:23, bauss via Digitalmars-d-learn a écrit : On Saturday, 6 April 2019 at 19:47:14 UTC, lithium iodate wrote: On Saturday, 6 April 2019 at 15:35:22 UTC, diniz wrote: So, I still could store and use and compare string pointers myself [1], and get valid results, meaning: pointer

Re: == comparison of string literals, and their usage

2019-04-07 Thread bauss via Digitalmars-d-learn
On Saturday, 6 April 2019 at 19:47:14 UTC, lithium iodate wrote: On Saturday, 6 April 2019 at 15:35:22 UTC, diniz wrote: So, I still could store and use and compare string pointers myself [1], and get valid results, meaning: pointer equality implies (literal) string equality. Or am I wrong? The

Re: == comparison of string literals, and their usage

2019-04-06 Thread diniz via Digitalmars-d-learn
Le 06/04/2019 à 21:47, lithium iodate via Digitalmars-d-learn a écrit : On Saturday, 6 April 2019 at 15:35:22 UTC, diniz wrote: So, I still could store and use and compare string pointers myself [1], and get valid results, meaning: pointer equality implies (literal) string equality. Or am I wro

Re: == comparison of string literals, and their usage

2019-04-06 Thread lithium iodate via Digitalmars-d-learn
On Saturday, 6 April 2019 at 15:35:22 UTC, diniz wrote: So, I still could store and use and compare string pointers myself [1], and get valid results, meaning: pointer equality implies (literal) string equality. Or am I wrong? The point is, the parser, operating on an array of prescanned lexeme

Re: == comparison of string literals, and their usage

2019-04-06 Thread diniz via Digitalmars-d-learn
Le 06/04/2019 à 16:07, AltFunction1 via Digitalmars-d-learn a écrit : On Friday, 5 April 2019 at 14:49:50 UTC, diniz wrote: Hello, Since literal strings are interned (and immutable), can I count on the fact that they are compared (==) by pointer? No. "==" performs a full array comparison and

Re: == comparison of string literals, and their usage

2019-04-06 Thread AltFunction1 via Digitalmars-d-learn
On Friday, 5 April 2019 at 14:49:50 UTC, diniz wrote: Hello, Since literal strings are interned (and immutable), can I count on the fact that they are compared (==) by pointer? No. "==" performs a full array comparison and "is" is apparently simplified at compile time. In the compiler there'

== comparison of string literals, and their usage

2019-04-05 Thread diniz via Digitalmars-d-learn
Hello, Since literal strings are interned (and immutable), can I count on the fact that they are compared (==) by pointer? Context: The use case is a custom lexer for a custom language. I initially wanted to represent lexeme classes by a big enum 'LexClass'. However, this makes me write 3 ti

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-20 Thread Marc Schütz via Digitalmars-d-learn
On Saturday, 19 December 2015 at 14:16:36 UTC, anonymous wrote: On 19.12.2015 14:20, Marc Schütz wrote: As this is going to be passed to a C function, it would need to be zero-terminated. `.dup` doesn't do this, he'd have to use `std.string.toStringz` instead. However, that function returns a

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-20 Thread Marc Schütz via Digitalmars-d-learn
On Saturday, 19 December 2015 at 17:30:02 UTC, Kagamin wrote: On Saturday, 19 December 2015 at 13:20:03 UTC, Marc Schütz wrote: As this is going to be passed to a C function No, ODBC API is designed with multilingual capability in mind, it doesn't rely on null terminated strings heavily: all

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-19 Thread Kagamin via Digitalmars-d-learn
On Saturday, 19 December 2015 at 13:20:03 UTC, Marc Schütz wrote: As this is going to be passed to a C function No, ODBC API is designed with multilingual capability in mind, it doesn't rely on null terminated strings heavily: all string arguments support length specification.

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-19 Thread Kagamin via Digitalmars-d-learn
Well, ISO 9075-3 doesn't use const qualifiers, but uses IN/OUT qualifiers instead, e.g. ExecDirect function is declared as: ExecDirect ( StatementHandle IN INTEGER, StatementText IN CHARACTER(L), TextLength IN INTEGER ) RETURNS SMALLINT And in C header: SQLRETURN SQLExecDire

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-19 Thread anonymous via Digitalmars-d-learn
On 19.12.2015 14:20, Marc Schütz wrote: As this is going to be passed to a C function, it would need to be zero-terminated. `.dup` doesn't do this, he'd have to use `std.string.toStringz` instead. However, that function returns a `immutable(char)*`, which would have to be cast again :-( Ouch, I

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-19 Thread Marc Schütz via Digitalmars-d-learn
On Friday, 18 December 2015 at 22:35:04 UTC, anonymous wrote: If the parameter is really not const, i.e. the function may mutate the argument, then the cast is not ok. You can use `.dup.ptr` instead to get a proper char* from a string. As this is going to be passed to a C function, it would ne

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-18 Thread anonymous via Digitalmars-d-learn
On 19.12.2015 01:06, Fer22f wrote: Documentation on casts say: Casting a pointer type to and from a class type is done as a type paint (i.e. a reinterpret cast). That sentence doesn't apply. string is not a class, it's an alias for immutable(char)[], i.e. it's an array. Reinterpretation i

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-18 Thread Fer22f via Digitalmars-d-learn
On Friday, 18 December 2015 at 22:18:34 UTC, Adam D. Ruppe wrote: That's what the examples on MSDN do too though, a cast. At first I thought the binding was missing a const, but the ODBC docs don't specify it as const either and cast. The ODBC functions also have a size parameter for string p

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-18 Thread Fer22f via Digitalmars-d-learn
y in slices documentation), and not with actual content (though string literals probably act different in this case). Documentation on casts say: Casting a pointer type to and from a class type is done as a type paint (i.e. a reinterpret cast). Reinterpretation is rather dangerous if string

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-18 Thread anonymous via Digitalmars-d-learn
On 18.12.2015 23:14, Fer22f wrote: By the use of this tutorial (http://www.easysoft.com/developer/languages/c/odbc_tutorial.html), I thought it would be very straightforward to use etc.c.odbc.sqlext and etc.c.odbc.sql to create a simple odbc application. But as soon as I started, I noticed a quir

Re: Problems with string literals and etc.c.odbc.sql functions

2015-12-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 18 December 2015 at 22:14:04 UTC, Fer22f wrote: When I remove the string literal and replace it with null, it compiles. .ptr and .toStringz both give immutable char* references, and don't work. A "cast(char *)"DNS=*maydns*;"" works, but it feels a lot like a hack that will not work i

Problems with string literals and etc.c.odbc.sql functions

2015-12-18 Thread Fer22f via Digitalmars-d-learn
By the use of this tutorial (http://www.easysoft.com/developer/languages/c/odbc_tutorial.html), I thought it would be very straightforward to use etc.c.odbc.sqlext and etc.c.odbc.sql to create a simple odbc application. But as soon as I started, I noticed a quirk: SQLRETURN ret; SQLHDBC

Re: Passing string literals to C

2015-01-01 Thread Gary Willoughby via Digitalmars-d-learn
() { writefln("%s",fromStringz(cpling("hello\0"))); } or void callC() { writefln("%s",fromStringz(cpling(toStringz("hello"; } === am I missing a better way to do this? To call a C function you can either use string literals which are always

Re: Passing string literals to C

2014-12-31 Thread Laeeth Isharc via Digitalmars-d-learn
Thanks for the help. Laeeth

Re: Passing string literals to C

2014-12-31 Thread Meta via Digitalmars-d-learn
On Wednesday, 31 December 2014 at 12:25:45 UTC, John Colvin wrote: String literals can implicitly convert to const(char)* or immutable(char)*. Neat. It doesn't appear to apply to array literals in general though... I believe this is a special case specifically for strings added

Re: Passing string literals to C

2014-12-31 Thread John Colvin via Digitalmars-d-learn
extern(C) char* cpling(char* s); void callC() { writefln("%s",fromStringz(cpling("hello\0"))); } or void callC() { writefln("%s",fromStringz(cpling(toStringz("hello"; } === am I missing a better way to do this? String literals are always null-termi

Re: Passing string literals to C

2014-12-31 Thread Mike Parker via Digitalmars-d-learn
",fromStringz(cpling("hello\0"))); } or void callC() { writefln("%s",fromStringz(cpling(toStringz("hello"; } === am I missing a better way to do this? String literals are always null-terminated. You can typically pass them as-is and D will do the

Re: Passing string literals to C

2014-12-31 Thread Daniel Kozák via Digitalmars-d-learn
(toStringz("hello"; > } > > === > > am I missing a better way to do this? First I am not sure, but you do not need to call fromStringz in this case. Next in this example you even not need to call toStringz, because D string literals are null-terminated. But generally it is

Passing string literals to C

2014-12-31 Thread Laeeth Isharc via Digitalmars-d-learn
What's best practice here? D strings are not null-terminated. char* cpling(char *s) { So toString("This i

Re: Passing string literals to C

2014-12-31 Thread Laeeth Isharc via Digitalmars-d-learn
Argh - no way to edit. What's best practice here? D strings are not null-terminated. === cpling.c char* cpling(char *s) { s[0]='!'; return s; } === dcaller.d extern(C) char* cpling(char* s); void callC() { writefln("%s",fromStringz(cpling("hello\0"))); } or void callC() { writefln("

Re: Multiline String Literals without linefeeds?

2013-09-23 Thread Dicebot
On Monday, 23 September 2013 at 11:30:18 UTC, bearophile wrote: Dicebot: Rationale / link to discussion? I use it extensively. http://d.puremagic.com/issues/show_bug.cgi?id=3827 Bye, bearophile Thanks. Well, then we will have _guaranteed_ const-folding of adjacent concatenated string

Re: Multiline String Literals without linefeeds?

2013-09-23 Thread bearophile
Dicebot: Rationale / link to discussion? I use it extensively. http://d.puremagic.com/issues/show_bug.cgi?id=3827 Bye, bearophile

Re: Multiline String Literals without linefeeds?

2013-09-23 Thread Dicebot
On Monday, 23 September 2013 at 11:10:07 UTC, bearophile wrote: simendsjo: Isn't "some" "string" replaced with "somestring" early on? Yes, unfortunately. And it's something Walter agreed with me to kill, but nothing has happened... Bye, bearophile Rationale / link to discussion? I use it

Re: Multiline String Literals without linefeeds?

2013-09-23 Thread bearophile
simendsjo: Isn't "some" "string" replaced with "somestring" early on? Yes, unfortunately. And it's something Walter agreed with me to kill, but nothing has happened... Bye, bearophile

Re: Multiline String Literals without linefeeds?

2013-09-23 Thread simendsjo
On Monday, 23 September 2013 at 09:42:59 UTC, bearophile wrote: John Carter: is there a similar mechanism in D? Or should I do... string foo = "long" "string" "without" "linefeeds" ; Genrally you should do: string foo = "long" ~ "string" ~ "without" ~

Re: Multiline String Literals without linefeeds?

2013-09-23 Thread bearophile
John Carter: is there a similar mechanism in D? Or should I do... string foo = "long" "string" "without" "linefeeds" ; Genrally you should do: string foo = "long" ~ "string" ~ "without" ~ "linefeeds"; See also: http://d.puremagic.com/issues/show_bug.c

Re: Multiline String Literals without linefeeds?

2013-09-23 Thread simendsjo
On Monday, 23 September 2013 at 04:43:16 UTC, John Carter wrote: In C/C++ in the presence of the preprocessor a string char foo[] = "\ long\ string\ without\ linefeeds\ "; Is translated by the preprocessor to char foo[] = "longstringwithoutlinefeeds"; is there a similar mechanism in D? Or sho

Multiline String Literals without linefeeds?

2013-09-22 Thread John Carter
In C/C++ in the presence of the preprocessor a string char foo[] = "\ long\ string\ without\ linefeeds\ "; Is translated by the preprocessor to char foo[] = "longstringwithoutlinefeeds"; is there a similar mechanism in D? Or should I do... string foo = "long" "string" "without" "linefeeds" ;

Re: string literals

2013-05-31 Thread Kenji Hara
On Friday, 31 May 2013 at 14:20:45 UTC, Jack Applegame wrote: What's the reason that the string literal is a dynamic array, not a static? So sometimes it is not possible to get string length compile time: void foo(T: E[N], E, size_t N)(auto ref T data) { pragma(msg, "static"); pragma(m

Re: string literals

2013-05-31 Thread Jack Applegame
On Friday, 31 May 2013 at 15:35:51 UTC, Jonathan M Davis wrote: The fact that it's a string is irrelevant, and making it a static array woludn't help any. If data were a template argument, it would work, but it's a funciton argument, so it won't. If to pass reference to static array as functi

Re: string literals

2013-05-31 Thread Jonathan M Davis
und without ever being copied is a definite efficiency boost for handling string literals (and a lot of string handling involves string literals). Making them static arrays wouldn't buy us anything and would cost us a lot. > So sometimes it is not possible to get string length compile time:

Re: string literals

2013-05-31 Thread bearophile
Jack Applegame: What's the reason that the string literal is a dynamic array, not a static? Originally it was a fixed sized array. But in most cases you want a dynamic array. Rust language forces you to specify where to allocate the string literal with a symbol before the string, as ~"hell

string literals

2013-05-31 Thread Jack Applegame
What's the reason that the string literal is a dynamic array, not a static? So sometimes it is not possible to get string length compile time: void foo(T: E[N], E, size_t N)(auto ref T data) { pragma(msg, "static"); pragma(msg, data.length); } void foo(T: E[], E)(auto ref T data) { p

Re: Unicode encodings and string literals

2012-10-08 Thread Jacob Carlborg
rstand properly, converts string to UTF-16, so that they can be sent to various Windows API functions. But string literals, for example in MessageBox, are fine, no conversion is needed. I don't understand the magic, what is converted, and when? If some variable was used e.g. "appName.

Unicode encodings and string literals

2012-10-08 Thread Lubos Pintes
UTF-16, so that they can be sent to various Windows API functions. But string literals, for example in MessageBox, are fine, no conversion is needed. I don't understand the magic, what is converted, and when? If some variable was used e.g. "appName.toUTF16z", and not "Error".toUTF16z

Re: Why aren't wide string literals zero-terminated just like strings?

2011-05-18 Thread Andrej Mitrovic
Ah, I had the wrong assumption but it is a bug. Reported: http://d.puremagic.com/issues/show_bug.cgi?id=6032 And thanks for disassembling!

Re: Why aren't wide string literals zero-terminated just like strings?

2011-05-18 Thread Steven Schveighoffer
On Wed, 18 May 2011 16:57:37 -0400, Andrej Mitrovic wrote: Skip the rest of the code until you reach main: http://codepad.org/zPAgFnPX We have this notion that string *literals* are zero-terminated, which enables us to send them to C functions expecting zero-terminated char* strings

Why aren't wide string literals zero-terminated just like strings?

2011-05-18 Thread Andrej Mitrovic
Skip the rest of the code until you reach main: http://codepad.org/zPAgFnPX We have this notion that string *literals* are zero-terminated, which enables us to send them to C functions expecting zero-terminated char* strings. But the same doesn't apply to wide string literals, e.g. "

Re: std.conv.parse of string literals

2010-09-09 Thread bearophile
Andrej Mitrovic: > This might be related to that bug report you wrote where you could > assign one string literal to another. Right. And recently there's another similar bug report in Bugzilla. So I may add this case just to one of those bug reports. Bye, bearophile

Re: std.conv.parse of string literals

2010-09-09 Thread Andrej Mitrovic
This might be related to that bug report you wrote where you could assign one string literal to another. bearophile Wrote: > > But a string literal isn't a lvalue. This seems all wrong.

std.conv.parse of string literals

2010-09-09 Thread bearophile
This is a small D2 program that uses parse: import std.conv: parse; void main() { parse!int("111"); parse!int("111"); } Gives the error: std.conv.ConvError: std.conv(1122): Can't convert value `' of type string base 2 to type int But a string literal isn't a lvalue. This seems all wron

Re: String literals have only one instance?

2010-08-20 Thread Rory Mcguire
Rory Mcguire wrote: > Are all string literals that have the same value initialized to the same > address? > > void main() { > string same() { > return "This"; > } > assert("This" is same()); > assert("This" is "This"); > }

Re: String literals have only one instance?

2010-08-19 Thread Sean Kelly
Rory Mcguire Wrote: > Are all string literals that have the same value initialized to the same > address? > > void main() { > string same() { > return "This"; > } > assert("This" is same()); > assert("T

Re: String literals have only one instance?

2010-08-19 Thread Simen kjaeraas
Rory Mcguire wrote: Are all string literals that have the same value initialized to the same address? void main() { string same() { return "This"; } assert("This" is same()); assert("This" is "This"); }

Re: String literals have only one instance?

2010-08-19 Thread Johannes Pfau
On 19.08.2010 09:53, Rory Mcguire wrote: > Are all string literals that have the same value initialized to the same > address? > > void main() { > string same() { > return "This"; > } > assert("This" is same()); >

Re: String literals have only one instance?

2010-08-19 Thread Stewart Gordon
e OP's point is: Are identical string literals *guaranteed* to be the same instance? Regardless of implementation? Regardless of whether they're next to each other, in different modules or anything in between? Regardless of the phase of the moon? Stewart.

Re: String literals have only one instance?

2010-08-19 Thread bearophile
Rory Mcguire: > Are all string literals that have the same value initialized to the same > address? > ... > Can this be relied upon? Probably a conforming D implementation is free to not give the same address to those. Bye, bearophile

Re: String literals have only one instance?

2010-08-19 Thread Jonathan Davis
On 8/19/10, Rory Mcguire wrote: > Are all string literals that have the same value initialized to the same > address? > > void main() { > string same() { > return "This"; > } > assert("This" is same()); > assert(

String literals have only one instance?

2010-08-19 Thread Rory Mcguire
Are all string literals that have the same value initialized to the same address? void main() { string same() { return "This"; } assert("This" is same()); assert("This" is "This"); } Can this be relied upon?

Re: Why are string literals zero-terminated?

2010-07-20 Thread awishformore
Am 20.07.2010 15:38, schrieb Lars T. Kyllingstad: On Tue, 20 Jul 2010 13:26:56 +, Lars T. Kyllingstad wrote: On Tue, 20 Jul 2010 14:59:18 +0200, awishformore wrote: Following this discussion on announce, I was wondering why string literals are zero-terminated. Or to re-formulate, why

Re: Why are string literals zero-terminated?

2010-07-20 Thread Lars T. Kyllingstad
On Tue, 20 Jul 2010 13:26:56 +, Lars T. Kyllingstad wrote: > On Tue, 20 Jul 2010 14:59:18 +0200, awishformore wrote: > >> Following this discussion on announce, I was wondering why string >> literals are zero-terminated. Or to re-formulate, why only string >> lite

Re: Why are string literals zero-terminated?

2010-07-20 Thread Lars T. Kyllingstad
On Tue, 20 Jul 2010 14:59:18 +0200, awishformore wrote: > Following this discussion on announce, I was wondering why string > literals are zero-terminated. Or to re-formulate, why only string > literals are zero-terminated. Why that inconsistency? What's the > rationale behin

Why are string literals zero-terminated?

2010-07-20 Thread awishformore
Following this discussion on announce, I was wondering why string literals are zero-terminated. Or to re-formulate, why only string literals are zero-terminated. Why that inconsistency? What's the rationale behind it? Does anyone know? /Max >>>> Did you test with a string th