Re: string to char[4] FourCC conversion

2023-05-27 Thread kdevel via Digitalmars-d-learn
On Friday, 26 May 2023 at 13:18:15 UTC, Steven Schveighoffer wrote: [...] This worked for me: ```d char[4] fourC(string s) { if(s.length >= 4) return s[0 .. 4]; Silent truncation? Non-ASCII chars? char[4] res = 0; According to [1], [2] or [3] that should read ``` c

Re: string to char[4] FourCC conversion

2023-05-26 Thread realhet via Digitalmars-d-learn
On Friday, 26 May 2023 at 13:18:15 UTC, Steven Schveighoffer wrote: This worked for me: ```d char[4] fourC(string s) { if(s.length >= 4) return s[0 .. 4]; char[4] res = 0; res[0 .. s.length] = s; return res; } ``` Sometimes I forget that the return does an impli

Re: string to char[4] FourCC conversion

2023-05-26 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/26/23 8:19 AM, realhet wrote: Hello, Is there a way to do it nicer/better/faster/simpler? ``` char[4] fourC(string s) { uint res;//Zero initialized, not 0xff initialized. auto cnt = min(s.length, 4),     p = cast(char[4]*)(); (*p)[0..cnt] = s[0..cnt]; return

string to char[4] FourCC conversion

2023-05-26 Thread realhet via Digitalmars-d-learn
Hello, Is there a way to do it nicer/better/faster/simpler? ``` char[4] fourC(string s) { uint res;//Zero initialized, not 0xff initialized. autocnt = min(s.length, 4), p = cast(char[4]*)(); (*p)[0..cnt] = s[0..cnt]; return *p; } ``` I tried

Re: string to char* in betterC

2020-03-11 Thread 9il via Digitalmars-d-learn
On Wednesday, 11 March 2020 at 16:10:48 UTC, 9il wrote: On Wednesday, 11 March 2020 at 16:07:06 UTC, Abby wrote: What is the proper way to get char* from string which is used in c functions? toStringz does returns: /usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo cannot be used

Re: string to char* in betterC

2020-03-11 Thread rikki cattermole via Digitalmars-d-learn
On 12/03/2020 5:07 AM, Abby wrote: What is the proper way to get char* from string which is used in c functions? toStringz does returns: /usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo cannot be used with -betterC and I think string.ptr is not safe because it's not zero

Re: string to char* in betterC

2020-03-11 Thread 9il via Digitalmars-d-learn
On Wednesday, 11 March 2020 at 16:07:06 UTC, Abby wrote: What is the proper way to get char* from string which is used in c functions? toStringz does returns: /usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo cannot be used with -betterC and I think string.ptr is not safe because

string to char* in betterC

2020-03-11 Thread Abby via Digitalmars-d-learn
What is the proper way to get char* from string which is used in c functions? toStringz does returns: /usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo cannot be used with -betterC and I think string.ptr is not safe because it's not zero termined. So what should I do? realloc

Re: string to char conv

2018-08-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, August 13, 2018 6:42:02 PM MDT zeus via Digitalmars-d-learn wrote: > On Tuesday, 14 August 2018 at 00:24:53 UTC, Jonathan M Davis > > wrote: > > On Monday, August 13, 2018 6:06:22 PM MDT zeus via > > > > Digitalmars-d-learn wrote: > >> [...] &g

Re: string to char conv

2018-08-13 Thread zeus via Digitalmars-d-learn
On Tuesday, 14 August 2018 at 00:24:53 UTC, Jonathan M Davis wrote: On Monday, August 13, 2018 6:06:22 PM MDT zeus via Digitalmars-d-learn wrote: [...] Why are you casting the string to a char*? That's just going to make writeln print out the pointer value. If you want to print out

Re: string to char conv

2018-08-13 Thread Jonathan M Davis via Digitalmars-d-learn
e in c++ wich give me as results > > 0xABCDEF123abcdef12345678909832190 how i can get in d > > 0xABCDEF123abcdef12345678909832190 instead of 4D77EB > > > > > > // D > > > > void test(string test){ > > > > char* testi

Re: string to char conv

2018-08-13 Thread Jonathan M Davis via Digitalmars-d-learn
get in d > 0xABCDEF123abcdef12345678909832190 instead of 4D77EB > > > // D > > void test(string test){ > char* testi = cast(char*)(test); > writeln(testi); > > } > > > void main() > { > test("0xABCDEF123abcdef12345678909832190"); >

string to char conv

2018-08-13 Thread zeus via Digitalmars-d-learn
){ char* testi = cast(char*)(test); writeln(testi); } void main() { test("0xABCDEF123abcdef12345678909832190"); } // C++ void test(string str){ const char* testi = str.c_str(); printf("%s\n", testi); } int main(int argc, char const

Re: Converting a string[] to char**

2017-05-09 Thread David Zhang via Digitalmars-d-learn
On Tuesday, 9 May 2017 at 07:59:19 UTC, Stanislav Blinov wrote: On Tuesday, 9 May 2017 at 07:50:33 UTC, David Zhang wrote: If indeed there is no way to avoid allocation, do the allocations have to remain 'alive' for the duration of the instance? Or can I deallocate immediately afterwards? I

Re: Converting a string[] to char**

2017-05-09 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 9 May 2017 at 07:50:33 UTC, David Zhang wrote: If indeed there is no way to avoid allocation, do the allocations have to remain 'alive' for the duration of the instance? Or can I deallocate immediately afterwards? I can't seem to find it in the Vulkan spec. 2.3.1. Object

Re: Converting a string[] to char**

2017-05-09 Thread David Zhang via Digitalmars-d-learn
in a separate parameter. import std.algorithm.iteration: map; import std.array: array; import std.conv: to; import std.string: toStringz; string[] strs = ["foo", "bar", "baz"]; /* convert string[] to char*[]: */ immutable(char)*[] chptrs = strs.map!to

Re: Converting a string[] to char**

2017-05-08 Thread ag0aep6g via Digitalmars-d-learn
, tightly packed. A `string[]` isn't that. A single `string` is a pointer-and-length pair. So a `string[]` has pointers and lengths alternating in memory. Casting from `string[]` to `char*[]` means reinterpreting string lengths as pointers. That's not what you want. When dereferencing those fake

Re: Converting a string[] to char**

2017-05-08 Thread rikki cattermole via Digitalmars-d-learn
On 09/05/2017 5:22 AM, David Zhang wrote: Hi, I'm playing around with Vulkan, and part of its initialization code calls for an array of strings as char**. I've tried casting directly (cast(char**)) and breaking it down into an array of char*s (char*[]) before getting the pointer to its first

Converting a string[] to char**

2017-05-08 Thread David Zhang via Digitalmars-d-learn
Hi, I'm playing around with Vulkan, and part of its initialization code calls for an array of strings as char**. I've tried casting directly (cast(char**)) and breaking it down into an array of char*s (char*[]) before getting the pointer to its first element ([0]). It provides the correct

Re: How can I concatenate a string, a char array and an int

2016-11-30 Thread Andrea Fontana via Digitalmars-d-learn
On Tuesday, 29 November 2016 at 15:01:37 UTC, Anders S wrote: Thanks guys for a really quick answer !! OK, a little bit awkward to use but getting there posting a new question about char * to struct ;) Thanks /anders Also: import std.conv : text; string temp = "This is a number"; string

Re: How can I concatenate a string, a char array and an int

2016-11-29 Thread Anders S via Digitalmars-d-learn
Thanks guys for a really quick answer !! OK, a little bit awkward to use but getting there posting a new question about char * to struct ;) Thanks /anders

Re: How can I concatenate a string, a char array and an int

2016-11-29 Thread Nicholas Wilson via Digitalmars-d-learn
314356); but get error Error: function core.stdc.stdio.sprintf (char* s, const(char*) format, ...) is not callable using argument types (char[80], string, int) because in D arrays do not decay to pointers. To get a null terminated string use toStringz Nor does this work char [50] temp = "

Re: How can I concatenate a string, a char array and an int

2016-11-29 Thread rumbu via Digitalmars-d-learn
str); but get error Error: function core.stdc.stdio.sprintf (char* s, const(char*) format, ...) is not callable using argument types (char[80], string, int) Nor does this work char [50] temp = "This is a number"; string greeting5= temp~" "~314356;

How can I concatenate a string, a char array and an int

2016-11-29 Thread Anders S via Digitalmars-d-learn
w in Dlang and import core.stdc.string and code: char [80] str; sprintf(str, "This is a number = %d", 314356); writefln("%s", str); but get error Error: function core.stdc.stdio.sprintf (char* s, const(char*) format, ...) is not callable using argument types (char[80], str

Re: Template overloads involving `string` and `char[constant]` return value

2016-09-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 21 September 2016 at 12:39:57 UTC, Johan Engelen wrote: It should really be mentioned in the documentation of toHexString, with an actual example instead of a unittest. Do you use my dpldocs.info? I add such notes there from time to time:

Re: Template overloads involving `string` and `char[constant]` return value

2016-09-21 Thread Johan Engelen via Digitalmars-d-learn
On Wednesday, 21 September 2016 at 13:06:08 UTC, Adam D. Ruppe wrote: the variable you are assigning the result to never does anything with regard to overloads or template args. Gotcha, thanks.

Re: Template overloads involving `string` and `char[constant]` return value

2016-09-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 21 September 2016 at 12:29:54 UTC, Johan Engelen wrote: Wouldn't something like this be possible? `T toHexString(string toHexString(Order order = Order.increasing, LetterCase letterCase = LetterCase.upper, T)(.) if (T == string)` I'm not sure what that's supposed to

Re: Template overloads involving `string` and `char[constant]` return value

2016-09-21 Thread Johan Engelen via Digitalmars-d-learn
On Wednesday, 21 September 2016 at 12:20:14 UTC, Adam D. Ruppe wrote: This is a pretty common pitfall (and IMO one of the most egregious design flaws in the language), I see it all the time. I write very little D code, so I guess it had to happen at some point then. Man, this is really bad

Re: Template overloads involving `string` and `char[constant]` return value

2016-09-21 Thread Johan Engelen via Digitalmars-d-learn
On Wednesday, 21 September 2016 at 12:20:14 UTC, Adam D. Ruppe wrote: It is neither, the compiler chose the right overload (remember, overloads are chosen based on the arguments alone, the type you specify for the variable holding the return value isn't a consideration there) and the

Re: Template overloads involving `string` and `char[constant]` return value

2016-09-21 Thread Adam D. Ruppe via Digitalmars-d-learn
, but the language allows you to implicitly slice that into a pointer (mistaken design in any case, doubly so since it a stack pointer)... and moreover it is implicitly cast to immutable! So it will implicitly cast that char[x] to string in a LOT of places... and it is almost always wrong

Template overloads involving `string` and `char[constant]` return value

2016-09-21 Thread Johan Engelen via Digitalmars-d-learn
hobos bug (overloads don't work like that). How are template overloads involving `string` and `char[constant]` return values supposed to work? Still can't believe I am the first one to run into this, what am I doing wrong? -Johan

Re: string and char[] in Phobos

2016-03-29 Thread Puming via Digitalmars-d-learn
the type information, which is why inout exists - Well, I never got inout until now, thanks! [...] I don't know what you're using in Phobos that takes string and returns char[]. That implies an allocation, and if the function is pure, char[] may have been selected, because it could be implicitly

Re: string and char[] in Phobos

2016-03-19 Thread Kagamin via Digitalmars-d-learn
When a string is not an in parameter, it can't be declared `in char[]`.

string and char[] in Phobos

2016-03-19 Thread Puming via Digitalmars-d-learn
Hi, I saw from the forum that functions with string like arguments better use `in char[]` instead of `string` type, because then it can accept both string and char[] types. But recently when actually using D, I found that many phobos functions/constructors use `string`, while many returns

Re: string and char[] in Phobos

2016-03-18 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, March 18, 2016 08:24:24 Puming via Digitalmars-d-learn wrote: > Hi, > > I saw from the forum that functions with string like arguments > better use `in char[]` instead of `string` type, because then it > can accept both string and char[] types. > > But recently whe

Re: Using replaceInPlace, string and char[]

2015-08-16 Thread TSalm via Digitalmars-d-learn
Must create a ticket for it ? I think so. Unless others object in 10 minutes... :) :-) Done : https://issues.dlang.org/show_bug.cgi?id=14925 Thanks for your help

Using replaceInPlace, string and char[]

2015-08-15 Thread TSalm via Digitalmars-d-learn
= SwapStrategy.stable, Range)(Range range) if (isBidirectionalRange!Range hasLvalueElements!Range) inout.d(13): Error: template instance std.array.replaceInPlace!(char, char[]) error instantiating Don't understand why this doesn't work: it compiles fine and runs perfectly if I change char[] by string

Re: Using replaceInPlace, string and char[]

2015-08-15 Thread TSalm via Digitalmars-d-learn
/array.d(2247): std.array.replaceInPlace!(char, char[]).replaceInPlace(ref char[] array, uint from, uint to, char[] stuff) Must create a ticket for it ? Don't understand why this doesn't work: it compiles fine and runs perfectly if I change char[] by string You mean, this: import std.array

Re: Using replaceInPlace, string and char[]

2015-08-15 Thread Ali Çehreli via Digitalmars-d-learn
This looks like a bug to me. The template constraints of the two overloads are pretty complicated. This case should match only one of them. On 08/15/2015 12:43 AM, TSalm wrote: Don't understand why this doesn't work: it compiles fine and runs perfectly if I change char[] by string You mean

Re: Using replaceInPlace, string and char[]

2015-08-15 Thread Ali Çehreli via Digitalmars-d-learn
On 08/15/2015 01:47 AM, TSalm wrote: Must create a ticket for it ? I think so. Unless others object in 10 minutes... :) In the other hand using string is not efficient since this certainly make a copy of the original string. Right ? This is better to use replaceInPlace with char[], but

Re: string to char array?

2015-06-08 Thread Kyoji Klyden via Digitalmars-d-learn
On Monday, 8 June 2015 at 09:54:28 UTC, Kagamin wrote: On Sunday, 7 June 2015 at 17:41:11 UTC, Kyoji Klyden wrote: Do you perchance have any links to learning resources for the D runtime(aside from just the github repository), and also maybe x86 architecture stuff? (I know intel has some 1000+

Re: string to char array?

2015-06-03 Thread Kyoji Klyden via Digitalmars-d-learn
Ooooh okay, I'm starting to get it. I think this last question should clear it up for me: When a string is made, how is the struct Slice handled? What does ptr get assigned?

The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Tell me, please, why this code works correctly always: import std.stdio; int n; readf(%s\n, n); string s, t; readf(%s\n%s\n, s, t); And this code works correctly is not always: import std.stdio; readf(%s\n, n); char[200010] s, t; scanf(%s%s, s.ptr, t.ptr); Data is entered only in this

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Ivan Kazmenko via Digitalmars-d-learn
On Saturday, 21 March 2015 at 14:31:20 UTC, Dennis Ritchie wrote: In C++ it is fully working: char s[25], t[25]; scanf(%s%s, s, t); Indeed. Generate a 10-character string: - import std.range, std.stdio; void main () {'a'.repeat (10).writeln;} - Try to copy it with D

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 15:05:56 UTC, Ivan Kazmenko wrote: On Saturday, 21 March 2015 at 14:31:20 UTC, Dennis Ritchie wrote: In C++ it is fully working: char s[25], t[25]; scanf(%s%s, s, t); Indeed. And why in D copied only the first 32767 characters of the string? I'm more

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
In C++ it is fully working: char s[25], t[25]; scanf(%s%s, s, t); http://codeforces.com/contest/527/submission/10376381?locale=en

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread anonymous via Digitalmars-d-learn
On Saturday, 21 March 2015 at 08:37:59 UTC, Dennis Ritchie wrote: Tell me, please, why this code works correctly always: [...] And this code works correctly is not always: import std.stdio; readf(%s\n, n); char[200010] s, t; scanf(%s%s, s.ptr, t.ptr); Please go into more detail about how

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 12:08:05 UTC, anonymous wrote: Please go into more detail about how it doesn't work. Task: http://codeforces.com/contest/527/problem/B?locale=en It works: char[200010] s, t; s = readln.strip; t = readln.strip;

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread FG via Digitalmars-d-learn
On 2015-03-21 at 16:05, Ivan Kazmenko wrote: Generate a 10-character string [...] Try to copy it with D scanf and printf: - import std.stdio; void main () { char [10] a; scanf (%s, a.ptr); printf (%s\n, a.ptr); } - Only 32767 first characters of the string are

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote: In what universe?! Which OS, compiler and architecture? On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote: In what universe?! Which OS, compiler and architecture? Windows 8.1 x64, dmd 2.066.1: import std.range, std.stdio; void main

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread anonymous via Digitalmars-d-learn
On Saturday, 21 March 2015 at 15:05:56 UTC, Ivan Kazmenko wrote: Generate a 10-character string: - import std.range, std.stdio; void main () {'a'.repeat (10).writeln;} - Try to copy it with D scanf and printf: - import std.stdio; void main () { char [10] a;

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread FG via Digitalmars-d-learn
On 2015-03-21 at 21:02, Dennis Ritchie wrote: In what universe?! Which OS, compiler and architecture? Windows 8.1 x64, dmd 2.066.1: That's strange. I cannot recreate the problem on Win7 x64 with dmd 2.066.1, neither when compiled for 32- nor 64-bit. I have saved the a's to a file and use

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread anonymous via Digitalmars-d-learn
On Saturday, 21 March 2015 at 23:00:46 UTC, Ivan Kazmenko wrote: To me, it looks like a bug somewhere, though I don't get where exactly. Is it in bits of DigitalMars C/C++ compiler code glued into druntime? As far as I understand, the bug is in snn.lib's scanf. snn.lib is Digital Mars's

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Ivan Kazmenko via Digitalmars-d-learn
On Saturday, 21 March 2015 at 16:34:44 UTC, Dennis Ritchie wrote: And why in D copied only the first 32767 characters of the string? I'm more days couldn't understand what was going on... To me, it looks like a bug somewhere, though I don't get where exactly. Is it in bits of DigitalMars

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread FG via Digitalmars-d-learn
On 2015-03-21 at 22:15, FG wrote: On 2015-03-21 at 21:02, Dennis Ritchie wrote: In what universe?! Which OS, compiler and architecture? Windows 8.1 x64, dmd 2.066.1: That's strange. I cannot recreate the problem on Win7 x64 with dmd 2.066.1, neither when compiled for 32- nor 64-bit. I have

Re: The difference in string and char[], readf() and scanf()

2015-03-21 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 21 March 2015 at 23:00:46 UTC, Ivan Kazmenko wrote: On Saturday, 21 March 2015 at 16:34:44 UTC, Dennis Ritchie wrote: And why in D copied only the first 32767 characters of the string? I'm more days couldn't understand what was going on... To me, it looks like a bug somewhere,

Re: parse string as char

2015-02-09 Thread Kagamin via Digitalmars-d-learn
https://github.com/Hackerpilot/libdparse/blob/master/src/std/d/lexer.d#L1491

Re: parse string as char

2015-02-09 Thread FG via Digitalmars-d-learn
Of course consuming it dchar by dchar also works: string s = `\tabŁŃ\r\nx`; assert(parseDchar(s) == '\t'); assert(parseDchar(s) == 'a'); assert(parseDchar(s) == 'b'); assert(parseDchar(s) == 'Ł'); assert(parseDchar(s) == 'Ń'); assert(parseDchar(s) == '\r');

Re: parse string as char

2015-02-09 Thread FG via Digitalmars-d-learn
On 2015-02-09 at 03:40, Timothee Cour via Digitalmars-d-learn wrote: Is there a simple way to parse a string as a char? eg: unittest{ assert(parseChar(`a`)=='a'); assert(parseChar(`\n`)=='\n'); //NOTE: I'm looking at `\n` not \n // should also work with other forms of characters, see

parse string as char

2015-02-08 Thread Timothee Cour via Digitalmars-d-learn
Is there a simple way to parse a string as a char? eg: unittest{ assert(parseChar(`a`)=='a'); assert(parseChar(`\n`)=='\n'); //NOTE: I'm looking at `\n` not \n // should also work with other forms of characters, see http://dlang.org/lex.html } Note, std.conv.to doesn't work (`\n`.to!char

Re: parse string as char

2015-02-08 Thread Rikki Cattermole via Digitalmars-d-learn
On 9/02/2015 3:40 p.m., Timothee Cour via Digitalmars-d-learn wrote: Is there a simple way to parse a string as a char? eg: unittest{ assert(parseChar(`a`)=='a'); assert(parseChar(`\n`)=='\n'); //NOTE: I'm looking at `\n` not \n // should also work with other forms of characters, see

Associative Array, key and value as type of string or char[]

2013-02-02 Thread timewulf
' ] I tried with all combinations of char[char[]] over string[char[]], char[string] to string[string] with depending quotations 'abc' to abc. My idea is just to print out the combinations to console. I'm not even .sure, if it's a good idea to do so, but what makes me cry is, after all trying, is that I

Re: Associative Array, key and value as type of string or char[]

2013-02-02 Thread Namespace
It seems you are right. I get the same error. You should open a bug report for this. But as long as it isn't fixed you can use an output via foreach. import std.stdio; void main() { string[string] ch_Description = [ kill : kills a process, pause : pauses

Re: Associative Array, key and value as type of string or char[]

2013-02-02 Thread timewulf
On 02.02.2013 16:06, Namespace wrote: import std.stdio; void main() { string[string] ch_Description = [ kill : kills a process, pause : pauses a process ]; // writeln(ch_Description); // does not work, but it should foreach (string key, string value;

Re: Associative Array, key and value as type of string or char[]

2013-02-02 Thread FG
On 2013-02-02 16:06, Namespace wrote: string[string] ch_Description = [ kill : kills a process, pause : pauses a process ]; writeln(ch_Description); // does not work, but it should Doesn't work in the current version? In DMD 2.060 it works.

Re: Associative Array, key and value as type of string or char[]

2013-02-02 Thread Namespace
On Saturday, 2 February 2013 at 16:06:09 UTC, FG wrote: On 2013-02-02 16:06, Namespace wrote: string[string] ch_Description = [ kill : kills a process, pause : pauses a process ]; writeln(ch_Description); // does not work, but it should Doesn't work in the current

Re: Associative Array, key and value as type of string or char[]

2013-02-02 Thread Namespace
Works now.

Re: Associative Array, key and value as type of string or char[]

2013-02-02 Thread Ali Çehreli
On 02/02/2013 08:00 AM, timewulf wrote: After searching for the differences to my code, I found my fault: It is not your fault. :) Now I've just to look, how to declare this array in the way of public constants. You need 'static this()': import std.stdio; string[string] ch_Description;

Re: Associative Array, key and value as type of string or char[]

2013-02-02 Thread timewulf
On 03.02.2013 06:09, Ali Çehreli wrote: You need 'static this()': import std.stdio; string[string] ch_Description; static this() { ch_Description = [ kill : kills a process, pause : pauses a process ]; } void main() { // ... } You can even make

Re: string[] to char**

2012-03-24 Thread simendsjo
On Fri, 23 Mar 2012 19:23:07 +0100, Ali Çehreli acehr...@yahoo.com wrote: On 03/23/2012 08:48 AM, simendsjo wrote: What's the best way to convert char** from string[]? In C, char** communicates transfer of ownership. Is that what you are trying to do? Are you going to pass a slice to a C

Re: string[] to char**

2012-03-24 Thread simendsjo
On Fri, 23 Mar 2012 17:09:18 +0100, bearophile bearophileh...@lycos.com wrote: On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote: What's the best way to convert char** from string[]? This is one way to do it: import std.algorithm, std.array, std.string, core.stdc.stdio; void main

Re: string[] to char**

2012-03-24 Thread simendsjo
On Fri, 23 Mar 2012 22:42:08 +0100, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: On 3/23/12, bearophile bearophileh...@lycos.com wrote: This is one way to do it: immutable(char)** p = array(map!toStringz(data)).ptr; This is asked so frequently that I think we could consider

Re: string[] to char**

2012-03-24 Thread simendsjo
On Sat, 24 Mar 2012 11:41:48 +0100, simendsjo simend...@gmail.com wrote: On Fri, 23 Mar 2012 17:09:18 +0100, bearophile bearophileh...@lycos.com wrote: On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote: What's the best way to convert char** from string[]? This is one way to do

Re: string[] to char**

2012-03-24 Thread bearophile
simendsjo: Oh, I didn't find toStringz, so I turned it into: It's in std.string. auto c_strings = strings.map!(toUTFz!(char*)).array().ptr; That's wrong syntax, that 2.059head doesn't enforce yet, map needs an ending (). Bye, bearophile

string[] to char**

2012-03-23 Thread simendsjo
What's the best way to convert char** from string[]?

Re: string[] to char**

2012-03-23 Thread bearophile
On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote: What's the best way to convert char** from string[]? This is one way to do it: import std.algorithm, std.array, std.string, core.stdc.stdio; void main() { auto data = [red, yellow, green]; immutable(char)** p = array(map

Re: string[] to char**

2012-03-23 Thread Ali Çehreli
On 03/23/2012 08:48 AM, simendsjo wrote: What's the best way to convert char** from string[]? In C, char** communicates transfer of ownership. Is that what you are trying to do? Are you going to pass a slice to a C function to be filled in by that C function? Such functions usually assign

Re: string[] to char**

2012-03-23 Thread Andrej Mitrovic
On 3/23/12, bearophile bearophileh...@lycos.com wrote: This is one way to do it: immutable(char)** p = array(map!toStringz(data)).ptr; This is asked so frequently that I think we could consider adding it to Phobos.

Re: A possible feature request: Make writef with %s call to!string for char* parameters

2011-07-12 Thread Steven Schveighoffer
such as this: struct Info { char* name; char* info; } I think it would be convenient if writef would call to!string for char* parameters behind the scenes if %s is the format specifier. I mean yes, you can use to!string in your code, or you can write D classes that wrap C libraries, but when

Re: string to char*

2010-09-14 Thread klickverbot
On 9/11/10 3:00 PM, shd wrote: Hello, I'm having a problem in passing a value to char* expecting function in D 2.0. Already tried: to!(char*)(my string); but it seems like there (Phobos) is no template like this. Then, tried: cast(char*)to!(char[])(my string) which looked ok, but i think

Re: string to char*

2010-09-12 Thread Jonathan M Davis
to always copy the string, then char* would likely be a better choice. But it could be that whatever issue made it so that the non-copying version was commented out will be fixed at some point, and toStringz() will once again cease to make a copy if it doesn't have to, at which point it would

Re: string to char*

2010-09-12 Thread bearophile
Jonathan M Davis: Well, if you look at toStringz()'s implementation, you may notice that there's commented out code which would not make a copy if there's a 0 in memory one passed the end of the string. It would simply use that 0 as the end of the const char* and avoid the copy. That

string to char*

2010-09-11 Thread shd
Hello, I'm having a problem in passing a value to char* expecting function in D 2.0. Already tried: to!(char*)(my string); but it seems like there (Phobos) is no template like this. Then, tried: cast(char*)to!(char[])(my string) which looked ok, but i think it's not a proper way to do

Re: string to char*

2010-09-11 Thread Mariusz Gliwiński
On 2010-09-11 15:13, Simen kjaeraas wrote: Why does the function expect a char*? If it is an external C function, and it might change the passed values, you should make a duplicate mutable string, or use char[] in lieu of string. If it is an external C function that will *not* change the passed

Re: string to char*

2010-09-11 Thread Andrej Mitrovic
function, and it might change the passed values, you should make a duplicate mutable string, or use char[] in lieu of string. If it is an external C function that will *not* change the passed values, and you have write access to the D headers to interface to C, use const char* instead. If no write

Re: string to char*

2010-09-11 Thread bearophile
shd: I'm having a problem in passing a value to char* expecting function in D 2.0. Already tried: to!(char*)(my string); A solution, maybe correct: import std.string: toStringz, indexOf; import std.c.string: strlen; import std.stdio: writeln; void main() { string s = my string

Re: Can't assign string to char * like the docs say

2009-05-14 Thread Gide Nwawudu
On Wed, 13 May 2009 16:42:51 -0400, Steven Schveighoffer schvei...@yahoo.com wrote: it is a documentation bug, this behavior is not allowed. Please submit a bug to bugzilla: http://d.puremagic.com/issues/ On the other hand, string *literals* are implicitly castable to char *: char *p = abc;

Can't assign string to char * like the docs say

2009-05-13 Thread Doctor J
Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't compile: void main() { string str = abc; char* p = str; // pointer to 1st element } Error: cannot implicitly convert expression (str) of type char[] to char* I agree it shouldn't

Re: Can't assign string to char * like the docs say

2009-05-13 Thread Trass3r
Doctor J schrieb: Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't compile: void main() { string str = abc; char* p = str; // pointer to 1st element } Error: cannot implicitly convert expression (str) of type char[] to char

Re: Can't assign string to char * like the docs say

2009-05-13 Thread Steven Schveighoffer
On Wed, 13 May 2009 14:28:46 -0400, Doctor J nob...@nowhere.com wrote: Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't compile: void main() { string str = abc; char* p = str; // pointer to 1st element } Error: cannot