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 surpri

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 Wysiwyg

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:

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?

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

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

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

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

== 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

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
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

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 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

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,

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

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

2015-12-18 Thread Fer22f via Digitalmars-d-learn
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 strings

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: 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

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

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

Re: Passing string literals to C

2015-01-01 Thread Gary Willoughby via Digitalmars-d-learn
(%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 null terminated or use std.string.toStringz (when using string

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() {

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 Daniel Kozák via Digitalmars-d-learn
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 better to use toStringz when need pass D strings to C code. Important Note: When passing a char* to a C function, and the C function keeps it around for any

Re: Passing string literals to C

2014-12-31 Thread Mike Parker via Digitalmars-d-learn
(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 right thing (you can also pass MyStr.ptr if you want). Use

Re: Passing string literals to C

2014-12-31 Thread John Colvin via Digitalmars-d-learn
* 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-terminated. You can typically pass them as-is and D will do

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 Laeeth Isharc via Digitalmars-d-learn
Thanks for the help. Laeeth

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.cgi?id=3827 You

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 ~ linefeeds;

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 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
Dicebot: Rationale / link to discussion? I use it extensively. http://d.puremagic.com/issues/show_bug.cgi?id=3827 Bye, bearophile

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 ;

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) {

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

Re: string literals

2013-05-31 Thread Jonathan M Davis
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: void foo(T: E[N], E

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

Unicode encodings and string literals

2012-10-08 Thread Lubos Pintes
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: Unicode encodings and string literals

2012-10-08 Thread Jacob Carlborg
, 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.toUTF16z, and not Error.toUTF16z

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. somestringw

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 n...@none.none 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

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!

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 wrong.

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.

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: 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); } Can this be relied upon? Interesting thanks guys. Was just curious about the speed

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: String literals have only one instance?

2010-08-19 Thread Jonathan Davis
On 8/19/10, Rory Mcguire rjmcgu...@gm_no_ail.com 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); } Can

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 Stewart Gordon
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 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()); assert(This is This); } Can this be relied upon? I

Re: String literals have only one instance?

2010-08-19 Thread Simen kjaeraas
Rory Mcguire rjmcgu...@gm_no_ail.com 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); } Can this be relied upon

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(This is This); } Can this be relied upon? This should

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

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 literals are zero-terminated. Why

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