Re: C string to D without memory allocation?

2015-12-21 Thread Marc Schütz via Digitalmars-d-learn
On Monday, 21 December 2015 at 09:46:58 UTC, Shriramana Sharma wrote: Jonathan M Davis via Digitalmars-d-learn wrote: If it isn't, all that means is that the array's capacity will be 0, so it's going to have to reallocate So it's safe to return a string produced by fromStringz without having

Re: C string to D without memory allocation?

2015-12-21 Thread Shriramana Sharma via Digitalmars-d-learn
Jonathan M Davis via Digitalmars-d-learn wrote: > If it isn't, all that means is that the > array's capacity will be 0, so it's going to have to reallocate So it's safe to return a string produced by fromStringz without having to worry that the user would append to it? Then why is it marked @sy

Re: C string to D without memory allocation?

2015-12-21 Thread Jakob Ovrum via Digitalmars-d-learn
On Monday, 21 December 2015 at 08:35:22 UTC, Jonathan M Davis wrote: There's also fromStringz that Jakob suggests using elsewhere in this thread, but that really just boils down to return cString ? cString[0 .. strlen(cString)] : null; So, using that over simply slicing is primarily for d

Re: C string to D without memory allocation?

2015-12-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, December 21, 2015 18:39:32 Rikki Cattermole via Digitalmars-d-learn wrote: > size_t strLen = ...; > char* ptr = ...; > > string myCString = cast(string)ptr[0 .. strLen]; > > I can't remember if it will include the null terminator or not, but if > it does just decrease strLen by 1. Cast

Re: C string to D without memory allocation?

2015-12-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, December 21, 2015 05:43:59 Jakob Ovrum via Digitalmars-d-learn wrote: > On Monday, 21 December 2015 at 05:41:31 UTC, Shriramana Sharma > wrote: > > Rikki Cattermole wrote: > > > >> string myCString = cast(string)ptr[0 .. strLen]; > > > > Thanks but does this require that one doesn't atte

Re: C string to D without memory allocation?

2015-12-20 Thread Jakob Ovrum via Digitalmars-d-learn
On Monday, 21 December 2015 at 06:00:45 UTC, Shriramana Sharma wrote: I suppose what you mean is, the onus of guaranteeing that const(char)* refers to a null-terminated string is upon the person calling the to! function? Yes I understand, and Phobos documentation does say that using a pointer f

Re: C string to D without memory allocation?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
Jakob Ovrum wrote: > Use std.string.fromStringz. to!string assumes that pointers to > characters are null-terminated strings which is not safe or > general I suppose what you mean is, the onus of guaranteeing that const(char)* refers to a null-terminated string is upon the person calling the to

Re: C string to D without memory allocation?

2015-12-20 Thread Jakob Ovrum via Digitalmars-d-learn
On Monday, 21 December 2015 at 05:39:32 UTC, Rikki Cattermole wrote: size_t strLen = ...; char* ptr = ...; string myCString = cast(string)ptr[0 .. strLen]; I can't remember if it will include the null terminator or not, but if it does just decrease strLen by 1. Strings from C libraries shoul

Re: C string to D without memory allocation?

2015-12-20 Thread Jakob Ovrum via Digitalmars-d-learn
On Monday, 21 December 2015 at 05:43:04 UTC, Rikki Cattermole wrote: On 21/12/15 6:41 PM, Shriramana Sharma wrote: Rikki Cattermole wrote: string myCString = cast(string)ptr[0 .. strLen]; Thanks but does this require that one doesn't attempt to append to the returned string using ~= or such

Re: C string to D without memory allocation?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
Rikki Cattermole wrote: > string myCString = cast(string)ptr[0 .. strLen]; Thanks but does this require that one doesn't attempt to append to the returned string using ~= or such? In which case it is not safe, right? -- Shriramana Sharma, Penguin #395953

Re: C string to D without memory allocation?

2015-12-20 Thread Rikki Cattermole via Digitalmars-d-learn
On 21/12/15 6:41 PM, Shriramana Sharma wrote: Rikki Cattermole wrote: string myCString = cast(string)ptr[0 .. strLen]; Thanks but does this require that one doesn't attempt to append to the returned string using ~= or such? In which case it is not safe, right? Correct, ~= should only be use

Re: C string to D without memory allocation?

2015-12-20 Thread Jakob Ovrum via Digitalmars-d-learn
On Monday, 21 December 2015 at 05:41:31 UTC, Shriramana Sharma wrote: Rikki Cattermole wrote: string myCString = cast(string)ptr[0 .. strLen]; Thanks but does this require that one doesn't attempt to append to the returned string using ~= or such? In which case it is not safe, right? Grow

Re: C string to D without memory allocation?

2015-12-20 Thread Jakob Ovrum via Digitalmars-d-learn
On Monday, 21 December 2015 at 05:34:07 UTC, Shriramana Sharma wrote: Hello. I have the following code: import std.stdio, std.conv; extern(C) const(char) * textAttrN(const (char) * specString, size_t n); string textAttr(const(char)[] specString) { const(char) * ptr = textAttrN(specString.p

Re: C string to D without memory allocation?

2015-12-20 Thread Rikki Cattermole via Digitalmars-d-learn
On 21/12/15 6:34 PM, Shriramana Sharma wrote: Hello. I have the following code: import std.stdio, std.conv; extern(C) const(char) * textAttrN(const (char) * specString, size_t n); string textAttr(const(char)[] specString) { const(char) * ptr = textAttrN(specString.ptr, specString.length);

C string to D without memory allocation?

2015-12-20 Thread Shriramana Sharma via Digitalmars-d-learn
Hello. I have the following code: import std.stdio, std.conv; extern(C) const(char) * textAttrN(const (char) * specString, size_t n); string textAttr(const(char)[] specString) { const(char) * ptr = textAttrN(specString.ptr, specString.length); writeln(ptr); return to!string(ptr); } voi