Re: Find a char among string (string.findAmong.char)

2021-07-07 Thread rassoc via Digitalmars-d-learn
On Wednesday, 7 July 2021 at 12:22:11 UTC, BoQsc wrote: I think nested foreach loops are more readable. ``` import std; void main() { alias alphabet = letters; char[26] letters = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',

Re: Find a char among string (string.findAmong.char)

2021-07-07 Thread BoQsc via Digitalmars-d-learn
I think nested foreach loops are more readable. ``` import std; void main() { alias alphabet = letters; char[26] letters = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',

Re: Find a char among string (string.findAmong.char)

2021-07-06 Thread BoQsc via Digitalmars-d-learn
On Tuesday, 6 July 2021 at 15:48:35 UTC, rassoc wrote: You can also do: ```d import std; void main() { // https://dlang.org/phobos/std_ascii.html#.lowercase "Book.".filter!(c => lowercase.canFind(c)) .each!(c => writeln(c, " found")); // Output: // o found //

Re: Find a char among string (string.findAmong.char)

2021-07-06 Thread rassoc via Digitalmars-d-learn
You can also do: ```d import std; void main() { // https://dlang.org/phobos/std_ascii.html#.lowercase "Book.".filter!(c => lowercase.canFind(c)) .each!(c => writeln(c, " found")); // Output: // o found // o found // k found } ```

Re: Find a char among string (string.findAmong.char)

2021-07-06 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 6 July 2021 at 11:35:14 UTC, BoQsc wrote: I tried out .canFind method, and to test it I removed the letter 'o' from the Alphabet. Weirdly enough .canFind method still found 'o' letter among the Alphabet. https://run.dlang.io/is/2Fvenf Looks like it has something to do with the

Re: Find a char among string (string.findAmong.char)

2021-07-06 Thread BoQsc via Digitalmars-d-learn
On Monday, 5 July 2021 at 19:48:13 UTC, jfondren wrote: On Monday, 5 July 2021 at 19:34:14 UTC, BoQsc wrote: But I really don't like how it looks less readable and makes less sense on first look. `if (([letter].findAmong(alphabet)).length)` I'd like to use some method on the `letter` instead

Re: Find a char among string (string.findAmong.char)

2021-07-05 Thread jfondren via Digitalmars-d-learn
On Monday, 5 July 2021 at 19:34:14 UTC, BoQsc wrote: But I really don't like how it looks less readable and makes less sense on first look. `if (([letter].findAmong(alphabet)).length)` I'd like to use some method on the `letter` instead of [] And `.length` does not make a lot of sense when

Re: Find a char among string (string.findAmong.char)

2021-07-05 Thread BoQsc via Digitalmars-d-learn
On Monday, 5 July 2021 at 19:25:23 UTC, jfondren wrote: On Monday, 5 July 2021 at 19:19:19 UTC, BoQsc wrote: If I use `[letter].findAmong(alphabet)` in my code, it considers a dot (.) punctuation character as a letter. You can see it here: https://run.dlang.io/is/YWmaXU It returns a

Re: Find a char among string (string.findAmong.char)

2021-07-05 Thread jfondren via Digitalmars-d-learn
On Monday, 5 July 2021 at 19:19:19 UTC, BoQsc wrote: If I use `[letter].findAmong(alphabet)` in my code, it considers a dot (.) punctuation character as a letter. You can see it here: https://run.dlang.io/is/YWmaXU It returns a zero-length array that, because it's not null, is true. That's

Re: Find a char among string (string.findAmong.char)

2021-07-05 Thread BoQsc via Digitalmars-d-learn
On Monday, 5 July 2021 at 18:59:09 UTC, jfondren wrote: On Monday, 5 July 2021 at 18:53:27 UTC, jfondren wrote: If you replace the findAmong call with `[letter].findAmong(alphabet)`, this works. Consider: ```d import std; void main() { import std.ascii : alphabet = letters;

Re: Find a char among string (string.findAmong.char)

2021-07-05 Thread jfondren via Digitalmars-d-learn
On Monday, 5 July 2021 at 18:53:27 UTC, jfondren wrote: If you replace the findAmong call with `[letter].findAmong(alphabet)`, this works. Consider: ```d import std; void main() { import std.ascii : alphabet = letters; string wordExample = "Book."; foreach (letter;

Re: Find a char among string (string.findAmong.char)

2021-07-05 Thread jfondren via Digitalmars-d-learn
On Monday, 5 July 2021 at 18:45:10 UTC, BoQsc wrote: I get an error when I try to find that letter is among alphabet. onlineapp.d(13): Error: template `std.algorithm.searching.findAmong` cannot deduce function from argument types `!()(immutable(char), immutable(string))`, candidates

Find a char among string (string.findAmong.char)

2021-07-05 Thread BoQsc via Digitalmars-d-learn
I get an error when I try to find that letter is among alphabet. onlineapp.d(13): Error: template `std.algorithm.searching.findAmong` cannot deduce function from argument types `!()(immutable(char), immutable(string))`, candidates are: /dlang/dmd/linux/bin64/../../src/phobos/std/algorithm

Re: std.conv.to!string refuses to convert a char* to string.

2017-12-09 Thread Venkat via Digitalmars-d-learn
Thank you, core.runtime.Runtime.initialize() fixed the issue. I am now able to use to!string as well. I found your posts and Ali Çehreli's posts on this subject. I think I have some understanding now.

Re: std.conv.to!string refuses to convert a char* to string.

2017-12-09 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 9 December 2017 at 06:14:36 UTC, Venkat wrote: Thanks for the quick response. std.string.fromStringz did the trick. I am not sure what was the deal with to!string. Be careful with fromStringz. It doesn't allocate a new string, so the returned string can easily become corrupted if

Re: std.conv.to!string refuses to convert a char* to string.

2017-12-08 Thread Venkat via Digitalmars-d-learn
Thanks for the quick response. std.string.fromStringz did the trick. I am not sure what was the deal with to!string.

Re: std.conv.to!string refuses to convert a char* to string.

2017-12-08 Thread Neia Neutuladh via Digitalmars-d-learn
On Saturday, 9 December 2017 at 05:55:21 UTC, Venkat wrote: I am trying out the DJni library (https://github.com/Monnoroch/DJni). For some reason std.conv.to!string doesn't want to convert a char* to a string.The lines below are taken from the log. I see that the last frame is at gc_qalloc. I

std.conv.to!string refuses to convert a char* to string.

2017-12-08 Thread Venkat via Digitalmars-d-learn
I am trying out the DJni library (https://github.com/Monnoroch/DJni). For some reason std.conv.to!string doesn't want to convert a char* to a string.The lines below are taken from the log. I see that the last frame is at gc_qalloc. I am not sure why it failed there. Can anybody elaborate on

Re: char e string em linguagem D

2017-07-14 Thread crimaniak via Digitalmars-d-learn
On Thursday, 13 July 2017 at 22:36:47 UTC, Basile B. wrote: return cast(char[])` ... Never cast a literal to char[]. modifying the resulting char[] will lead to AV, at least under linux. `.dup` the literal if you really needs char[]. Hmm, yes, my bad. Probably, it was necessary even

Re: char e string em linguagem D

2017-07-13 Thread dark777 via Digitalmars-d-learn
On Thursday, 13 July 2017 at 22:30:29 UTC, crimaniak wrote: On Thursday, 13 July 2017 at 21:49:40 UTC, dark777 wrote: Pessoal eu fiz o seguinte programa em C++. https://pastebin.com/CvVv6Spn porem tentei fazer o equivalente em D mas nao entendi muito bem... https://pastebin.com/2xw9geRR

Re: char e string em linguagem D

2017-07-13 Thread Basile B. via Digitalmars-d-learn
On Thursday, 13 July 2017 at 22:30:29 UTC, crimaniak wrote: On Thursday, 13 July 2017 at 21:49:40 UTC, dark777 wrote: char[] stalman() { return cast(char[])` ((__-^^-,-^^-__)) *---***---* *--|o o|--*

Re: char e string em linguagem D

2017-07-13 Thread crimaniak via Digitalmars-d-learn
On Thursday, 13 July 2017 at 21:49:40 UTC, dark777 wrote: Pessoal eu fiz o seguinte programa em C++. https://pastebin.com/CvVv6Spn porem tentei fazer o equivalente em D mas nao entendi muito bem... https://pastebin.com/2xw9geRR alguem poderia me ajudar? Se acepta utilizar intervalos en

char e string em linguagem D

2017-07-13 Thread dark777 via Digitalmars-d-learn
Pessoal eu fiz o seguinte programa em C++. https://pastebin.com/CvVv6Spn porem tentei fazer o equivalente em D mas nao entendi muito bem... https://pastebin.com/2xw9geRR alguem poderia me ajudar?

Re: what's the right way to get char* from string?

2016-05-06 Thread ZombineDev via Digitalmars-d-learn
On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote: Hello, When I need to call C function, often need to have char* pointer from string. "Interfacing to C++" page: https://dlang.org/spec/cpp_interface.html have following example. extern (C) int strcmp(char* string1, char* string

Re: what's the right way to get char* from string?

2016-05-05 Thread Alex Parrill via Digitalmars-d-learn
On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote: extern (C) int strcmp(char* string1, char* string2); This signature of strcmp is incorrect. strcmp accepts const char* arguments [1], which in D would be written as const(char)*. The immutable(char)* values returned from toStringz are

Re: what's the right way to get char* from string?

2016-05-05 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/5/16 3:36 PM, Steven Schveighoffer wrote: Only thing I can think of is.. um... horrible: char *toCharz(string s) { auto cstr = s.toStringz; return cstr[0 .. s.length + 1].dup.ptr; } Ignore this. What Jonathan said :) -Steve

Re: what's the right way to get char* from string?

2016-05-05 Thread aki via Digitalmars-d-learn
On Thursday, 5 May 2016 at 11:35:09 UTC, Jonathan M Davis wrote: If you want a different mutability, then use the more general function std.utf.toUTFz. e.g. from the documentation: auto p1 = toUTFz!(char*)("hello world"); auto p2 = toUTFz!(const(char)*)("hello world"); auto p3 =

Re: what's the right way to get char* from string?

2016-05-05 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/5/16 11:53 AM, pineapple wrote: On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote: Hello, When I need to call C function, often need to have char* pointer from string. This might help: import std.traits : isSomeString; import std.string : toStringz; extern (C) int strcmp(char

Re: what's the right way to get char* from string?

2016-05-05 Thread Jonathan M Davis via Digitalmars-d-learn
On Thu, 05 May 2016 07:49:46 + aki via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Hello, > > When I need to call C function, often need to > have char* pointer from string. > > "Interfacing to C++" page: > https://dlang.org/spe

Re: what's the right way to get char* from string?

2016-05-05 Thread pineapple via Digitalmars-d-learn
On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote: Hello, When I need to call C function, often need to have char* pointer from string. This might help: import std.traits : isSomeString; import std.string : toStringz; extern (C) int strcmp(char* string1, char* string2); int strcmpD0(S

what's the right way to get char* from string?

2016-05-05 Thread aki via Digitalmars-d-learn
Hello, When I need to call C function, often need to have char* pointer from string. "Interfacing to C++" page: https://dlang.org/spec/cpp_interface.html have following example. extern (C) int strcmp(char* string1, char* string2); import std.string; int myDfunction(char[] s) {

Re: can't zip a char[5], string[5], real[5]

2015-10-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, October 21, 2015 14:11:20 anonymous via Digitalmars-d-learn wrote: > On Wednesday, 21 October 2015 at 14:06:54 UTC, Shriramana Sharma > wrote: > > import std.stdio, std.range; > > void mywrite(char [5] chars, real[5] vals) > > { > > static string [5] fmts = ["%9.4f, ", "%9.4f; ",

Re: can't zip a char[5], string[5], real[5]

2015-10-21 Thread anonymous via Digitalmars-d-learn
On Wednesday, 21 October 2015 at 14:06:54 UTC, Shriramana Sharma wrote: import std.stdio, std.range; void mywrite(char [5] chars, real[5] vals) { static string [5] fmts = ["%9.4f, ", "%9.4f; ", "%3d, ", "%3d, ", "%3d\n"]; foreach (e; zip(chars, fmts, vals)) write(e[0], " = ",

can't zip a char[5], string[5], real[5]

2015-10-21 Thread Shriramana Sharma via Digitalmars-d-learn
[2])); } Compiling gives: zip_string.d(5): Error: template std.range.zip cannot deduce function from argument types !()(char[5], string[5], real[5]), candidates are: /usr/include/dmd/phobos/std/range/package.d(3678): std.range.zip(Ranges...)(Ranges ranges) if (Ranges.length && allSatisfy!

Re: cannot implicitly convert char[] to string

2015-08-15 Thread Ali Çehreli via Digitalmars-d-learn
builder fails, with the error message: runnable/test23.d(1219): Error: cannot implicitly convert expression (format(s = %s, s)) of type char[] to string The line which fails is `p = std.string.format(s = %s, s);` I don't understand why I can't convert a char[] to string. I think it has to do

Re: cannot implicitly convert char[] to string

2015-08-15 Thread cym13 via Digitalmars-d-learn
expression (format(s = %s, s)) of type char[] to string The line which fails is `p = std.string.format(s = %s, s);` I don't understand why I can't convert a char[] to string. I think it has to do with the fact that string is an alias to immutable(char)[] and you can't implicitely cast

Re: cannot implicitly convert char[] to string

2015-08-15 Thread cym13 via Digitalmars-d-learn
: runnable/test23.d(1219): Error: cannot implicitly convert expression (format(s = %s, s)) of type char[] to string The line which fails is `p = std.string.format(s = %s, s);` I don't understand why I can't convert a char[] to string. I think it has to do with the fact that string is an alias

Re: cannot implicitly convert char[] to string

2015-08-15 Thread Timon Gehr via Digitalmars-d-learn
implicitly convert expression (format(s = %s, s)) of type char[] to string The line which fails is `p = std.string.format(s = %s, s);` I don't understand why I can't convert a char[] to string. Get rid of the 'in' in format's signature. Oh, I see, this is by design (which I don't like

Re: cannot implicitly convert char[] to string

2015-08-15 Thread Timon Gehr via Digitalmars-d-learn
= %s, s)) of type char[] to string The line which fails is `p = std.string.format(s = %s, s);` I don't understand why I can't convert a char[] to string. Get rid of the 'in' in format's signature.

cannot implicitly convert char[] to string

2015-08-15 Thread vladde via Digitalmars-d-learn
I made a PR to phobos where I modified `std.format.format`. https://github.com/D-Programming-Language/phobos/pull/3528 However the auto builder fails, with the error message: runnable/test23.d(1219): Error: cannot implicitly convert expression (format(s = %s, s)) of type char[] to string

Re: Insert a char in string

2014-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 07/10/2014 09:05 AM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N

Re: Insert a char in string

2014-07-11 Thread JR via Digitalmars-d-learn
On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote: On 07/10/2014 06:05 PM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace

Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn
I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0');

Re: Insert a char in string

2014-07-10 Thread John Colvin via Digitalmars-d-learn
On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X

Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn
Sorry.. I mean: auto X = 100; auto N = X.insertInPlace(3,','); On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try

Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn
I used that solution: string InsertComma(string val) { return val[0 .. $-2] ~ , ~ val[$-2 .. $]; } On Thursday, 10 July 2014 at 16:23:44 UTC, John Colvin wrote: On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string

Re: Insert a char in string

2014-07-10 Thread via Digitalmars-d-learn
On Thursday, 10 July 2014 at 16:20:29 UTC, Alexandre wrote: Sorry.. I mean: auto X = 100; auto N = X.insertInPlace(3,','); On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I

Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn
at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N

Re: Insert a char in string

2014-07-10 Thread simendsjo via Digitalmars-d-learn
On 07/10/2014 06:05 PM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100

Re: Insert a char in string

2014-07-10 Thread Alexandre via Digitalmars-d-learn
at 19:33:15 UTC, simendsjo wrote: On 07/10/2014 06:05 PM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try

Re: Insert a char in string

2014-07-10 Thread simendsjo via Digitalmars-d-learn
, here in Brazil... On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote: On 07/10/2014 06:05 PM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use

Re: Modify char in string

2014-05-19 Thread Tim via Digitalmars-d-learn
On Sunday, 18 May 2014 at 19:09:52 UTC, Chris Cain wrote: On Sunday, 18 May 2014 at 18:55:59 UTC, Tim wrote: Hi everyone, is there any chance to modify a char in a string like: As you've seen, you cannot modify immutables (string is an immutable(char)[]). If you actually do want the string

Re: Modify char in string

2014-05-19 Thread Ali Çehreli via Digitalmars-d-learn
On 05/19/2014 10:07 AM, Tim wrote: I already tried: void main() { char[] sMyText = Replace the last char_; sMyText[$ - 1] = '.'; } but I always getting Error: cannot implicitly convert expression (Replace the last char_) of type string to char

Modify char in string

2014-05-18 Thread Tim via Digitalmars-d-learn
Hi everyone, is there any chance to modify a char in a string like: void main() { string sMyText = Replace the last char_; sMyText[$ - 1] = '.'; } But when I execute the code above I'm always getting cannot modify immutable expression at sMyText[__dollar -1LU]. I though D supported

Re: Modify char in string

2014-05-18 Thread bearophile via Digitalmars-d-learn
Tim: is there any chance to modify a char in a string like: void main() { string sMyText = Replace the last char_; sMyText[$ - 1] = '.'; } But when I execute the code above I'm always getting cannot modify immutable expression at sMyText[__dollar -1LU]. I though D supported

Re: Modify char in string

2014-05-18 Thread Chris Cain via Digitalmars-d-learn
On Sunday, 18 May 2014 at 18:55:59 UTC, Tim wrote: Hi everyone, is there any chance to modify a char in a string like: As you've seen, you cannot modify immutables (string is an immutable(char)[]). If you actually do want the string to be modifiable, you should define it as char[] instead

Does anybody have an example of overloading a function to accept char[] or string parameter

2014-03-30 Thread Gary Miller
to change it in the function. Can I overload a function to work for either a char[] or a string? Many of the library functions that I need to call require string arguments such as the replace below. I believe I can create a wrapper function for it like this to still get at the functionality

Re: Does anybody have an example of overloading a function to accept char[] or string parameter

2014-03-30 Thread evilrat
On Sunday, 30 March 2014 at 15:58:52 UTC, Gary Miller wrote: Are there any alternate libraries for D that have a mutable string datatype or is there a way to override the immutable characteristic of the string datatype by reallocating it or something? string.dup property does a copy of

Re: Does anybody have an example of overloading a function to accept char[] or string parameter

2014-03-30 Thread bearophile
Gary Miller: char[] ReplaceAllSubstrings(inout char[] Original, in char[] SearchString, in char[] Substring) { string SOriginal = Original.dup; string SSearchString = SearchString.dup

[Style] Converting from char[] to string, to!string vs idup

2014-03-25 Thread Mark Isaacson
I am presently working my way through TDPL for the second time, and there's an example in chapter 1 to the effect of: [code] string currentParagraph; foreach(line; stdin.byLine()) { if (line.length 2) { currentParagraph = to!string(line[2 .. $]); } } [/code] The explicit conversion

Re: [Style] Converting from char[] to string, to!string vs idup

2014-03-25 Thread Justin Whear
general? I tried taking a peek at the implementation of to!string, but it wasn't easy enough to follow and boil down into components. I believe char[] - string conversion with to! will use this implementation: https://github.com/D-Programming-Language/phobos/blob/ master/std/conv.d#L823 So

Re: [Style] Converting from char[] to string, to!string vs idup

2014-03-25 Thread bearophile
Mark Isaacson: why use `to!string` instead of just doing `line[2 .. $].idup`? I sometimes prefer the text function: = line[2 .. $].text; Bye, bearophile

Re: [Style] Converting from char[] to string, to!string vs idup

2014-03-25 Thread monarch_dodra
On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote: Is there a performance benefit? Is it simply because it's more general? Mostly because it's more generic. For example: If instead you want to do string = char[], then your code will have to be changed to use dup. if instead you

Re: [Style] Converting from char[] to string, to!string vs idup

2014-03-25 Thread Mark Isaacson
Much appreciated everyone! I had a vague intuition that these were the reasons, but it was helpful to spell them out. I'm especially partial to the self-documentation reasoning.

Re: [Style] Converting from char[] to string, to!string vs idup

2014-03-25 Thread H. S. Teoh
On Tue, Mar 25, 2014 at 10:02:33PM +, monarch_dodra wrote: On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote: Is there a performance benefit? Is it simply because it's more general? [...] There is *1* thing you should take into account though: to! is a no-op for string=string

Re: [Style] Converting from char[] to string, to!string vs idup

2014-03-25 Thread Steven Schveighoffer
? [...] There is *1* thing you should take into account though: to! is a no-op for string=string or char[]=char[], or anything else that can be implicitly converted as such. In contrast, dup/idup will create an actual copy. Not that this is good or bad. Just something you should keep in mind. I think it's a good

Re: How to handle char* to string from C functions?

2014-01-02 Thread Gary Willoughby
then do string r = to!string(result); or char[] r = result[0 .. strlen(result)]; and use that/ to!(string) works great thanks!

Re: How to handle char* to string from C functions?

2014-01-02 Thread Gary Willoughby
then do string r = to!string(result); or char[] r = result[0 .. strlen(result)]; and use that/ Another question: how do i convert const(char)** to string[]?

Re: How to handle char* to string from C functions?

2014-01-02 Thread bearophile
Gary Willoughby: Another question: how do i convert const(char)** to string[]? If you know that you have N strings, then a solution is (untested): pp[0 .. N].map!text.array If it doesn't work, try: pp[0 .. N].map!(to!string).array Bye, bearophile

Re: How to handle char* to string from C functions?

2014-01-02 Thread uc
You are going to need the length of your c char*[] then a for-loop should do it :D

Re: How to handle char* to string from C functions?

2014-01-02 Thread Adam D. Ruppe
On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote: If you know that you have N strings, then a solution is (untested): Or if it is zero terminated, maybe pp.until!a is null.map!text.array Though personally I'd just use the plain old for loop.

Re: How to handle char* to string from C functions?

2014-01-02 Thread monarch_dodra
On Thursday, 2 January 2014 at 15:53:40 UTC, Adam D. Ruppe wrote: On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote: If you know that you have N strings, then a solution is (untested): Or if it is zero terminated, maybe pp.until!a is null.map!text.array Though personally I'd

Re: How to handle char* to string from C functions?

2014-01-02 Thread Gary Willoughby
On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote: Gary Willoughby: Another question: how do i convert const(char)** to string[]? If you know that you have N strings, then a solution is (untested): pp[0 .. N].map!text.array If it doesn't work, try: pp[0 .. N].map!(to!string

Re: How to handle char* to string from C functions?

2014-01-02 Thread uc
i'll answer in code http://dpaste.dzfl.pl/2bb1a1a8

Re: How to handle char* to string from C functions?

2014-01-02 Thread bearophile
Gary Willoughby: I've noticed that const(char)** can be accessed via indexes: writefln(%s, pp[0].to!(string)); //etc. cool! This is a feature that works with all pointers to a sequence of items, like in C. But array bounds are not verified, so it's more bug-prone. So if you know the

How to handle char* to string from C functions?

2014-01-01 Thread Gary Willoughby
I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func(); writefln(String: %s, *result); I only get one character printed. I guess this is expected because i'm only returned a pointer to the first char.

Re: How to handle char* to string from C functions?

2014-01-01 Thread Adam D. Ruppe
On Wednesday, 1 January 2014 at 23:03:06 UTC, Gary Willoughby wrote: I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func();' you can then do string r = to!string(result); or char[] r = result[0

Re: How to handle char* to string from C functions?

2014-01-01 Thread John Colvin
On Wednesday, 1 January 2014 at 23:03:06 UTC, Gary Willoughby wrote: I'm calling an external C function which returns a string delivered via a char*. When i print this string out, like this: char* result = func(); writefln(String: %s, *result); I only get one character printed. You're not

Re: Cannot cast char[] to string.

2013-11-14 Thread Dicebot
On Thursday, 14 November 2013 at 19:41:13 UTC, Agustin wrote: I'm trying to use http://dlang.org/phobos/std_net_curl.html and when i compile the same example i get: cannot implicitly convert expression (get(cast(const(char)[])address, AutoProtocol())) of type char[] to string string

Cannot cast char[] to string.

2013-11-14 Thread Agustin
I'm trying to use http://dlang.org/phobos/std_net_curl.html and when i compile the same example i get: cannot implicitly convert expression (get(cast(const(char)[])address, AutoProtocol())) of type char[] to string string address = http://dlang.org;; string _data = get(address);

Re: Cannot cast char[] to string.

2013-11-14 Thread Brad Anderson
On Thursday, 14 November 2013 at 19:41:13 UTC, Agustin wrote: I'm trying to use http://dlang.org/phobos/std_net_curl.html and when i compile the same example i get: cannot implicitly convert expression (get(cast(const(char)[])address, AutoProtocol())) of type char[] to string string

Re: Cannot cast char[] to string.

2013-11-14 Thread Ali Çehreli
())) of type char[] to string string address = http://dlang.org;; string _data = get(address); `get` returns mutable data, one should respect it: char[] data = get(address); // or just use `auto data = ` However, that data can automatically be converted to string if get() were pure. (I can understand

Re: Cannot cast char[] to string.

2013-11-14 Thread Jonathan M Davis
)[])address, AutoProtocol())) of type char[] to string string address = http://dlang.org;; string _data = get(address); You have two options: string address = http://dlang.org;; string _data = get(address).idup(); // create immutable copy or string address = http://dlang.org

Issue with char and string overlap

2013-07-19 Thread JS
I'm trying to create a split function that can handle both char and string delims. I initially created two separate functions but this doesn't work for default parameters since the compiler doesn't know which one to choose(but in this case both would work fine and it would be nice to inform

Re: Issue with char and string overlap

2013-07-19 Thread JS
On Friday, 19 July 2013 at 17:18:00 UTC, JS wrote: I'm trying to create a split function that can handle both char and string delims. I initially created two separate functions but this doesn't work for default parameters since the compiler doesn't know which one to choose(but in this case

Re: Issue with char and string overlap

2013-07-19 Thread monarch_dodra
On Friday, 19 July 2013 at 17:18:00 UTC, JS wrote: I'm trying to create a split function that can handle both char and string delims. I initially created two separate functions but this doesn't work for default parameters since the compiler doesn't know which one to choose(but in this case

Re: Issue with char and string overlap

2013-07-19 Thread Ali Çehreli
On 07/19/2013 10:40 AM, anonymous wrote: On Friday, 19 July 2013 at 17:18:00 UTC, JS wrote: for(int j = 0; j s.length - d.length; j++) This j would shadow the one above. Just choose another name. Even better: foreach (k; 0 .. s.length - d.length) or: foreach (k;

Re: Issue with char and string overlap

2013-07-19 Thread H. S. Teoh
On Fri, Jul 19, 2013 at 07:17:57PM +0200, JS wrote: [...] string[] split(T)(string s, T d) if (is(T == char) || is(T == string)) { int i = 0, oldj = 0; bool ok = true; string[] r; foreach(j, c; s) { static if (is(T == char

Re: Issue with char and string overlap

2013-07-19 Thread monarch_dodra
On Friday, 19 July 2013 at 17:25:34 UTC, JS wrote: BTW, I'd like to have a default value for d. That or efficiently allow for variadic d, which then the default delim could easily be tested for. To answer your previous question about shadowing, you are probably experiencing an old bug where

Re: Issue with char and string overlap

2013-07-19 Thread anonymous
On Friday, 19 July 2013 at 17:18:00 UTC, JS wrote: both functions work separately but when i uncomment the string version I get an error about the string version shadowing. import std.stdio, std.cstream; string[] split(T)(string s, T d) if (is(T == char) || is(T == string)) { int i

Fastest way to append char to string?

2012-12-11 Thread Chopin
Is this the fastest way to append a char to string? char c = 'a'; string s; s ~= c; ? I have a program that does this many many times... and it's slow. So I was wondering it it could be it. Thanks for tips!

Re: Fastest way to append char to string?

2012-12-11 Thread monarch_dodra
On Tuesday, 11 December 2012 at 15:52:31 UTC, Chopin wrote: Is this the fastest way to append a char to string? char c = 'a'; string s; s ~= c; ? I have a program that does this many many times... and it's slow. So I was wondering it it could be it. Thanks for tips! This may or may

Re: Fastest way to append char to string?

2012-12-11 Thread bearophile
Chopin: Is this the fastest way to append a char to string? char c = 'a'; string s; s ~= c; ? I have a program that does this many many times... and it's slow. So I was wondering it it could be it. Try the appender from std.array. It's supposed to be faster, but sometimes it's not faster

Re: char and string with umlauts

2011-10-22 Thread Jim Danley
, October 20, 2011 8:19 PM Subject: Re: char and string with umlauts On Thursday, October 20, 2011 09:48 Jim Danley wrote: I have been a programmer for many years and started using D about one year back. Suddenly, I find myself in unfamiliar territory. I need to used Finish umlauts in chars

char and string with umlauts

2011-10-20 Thread Jim Danley
I have been a programmer for many years and started using D about one year back. Suddenly, I find myself in unfamiliar territory. I need to used Finish umlauts in chars and strings, but they are not part of my usual American ASCII character set. Can anyone point me in the right direction?

Re: char and string with umlauts

2011-10-20 Thread Trass3r
Make sure your source file is saved in UTF-8 format.

Re: char and string with umlauts

2011-10-20 Thread Jonathan M Davis
On Thursday, October 20, 2011 09:48 Jim Danley wrote: I have been a programmer for many years and started using D about one year back. Suddenly, I find myself in unfamiliar territory. I need to used Finish umlauts in chars and strings, but they are not part of my usual American ASCII character

Re: char and string with umlauts

2011-10-20 Thread Ali Çehreli
On Thu, 20 Oct 2011 19:48:54 +0300, Jim Danley wrote: I have been a programmer for many years and started using D about one year back. Suddenly, I find myself in unfamiliar territory. I need to used Finish umlauts in chars and strings, but they are not part of my usual American ASCII

Re: char and string with umlauts

2011-10-20 Thread GrahamC
On Thu, 20 Oct 2011 19:48:54 +0300, Jim Danley wrote: I have been a programmer for many years and started using D about one year back.  Suddenly, I find myself in unfamiliar territory.  I need to used Finish umlauts in chars and strings, but they are not part of my usual American ASCII

Re: char[] to string

2011-06-11 Thread bearophile
Jonathan M Davis: Even better though, would be to use std.conv.to - e.g. to!string(input). This will convert input to a string, but it has the advantage that if input is already a string, then it'll just return the string rather than making another copy like idup would. I didn't

  1   2   >