Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 03, 2016 What is the simplest way of doing @nogc string concatenation? | ||||
---|---|---|---|---|
| ||||
What is the simplest way of doing @nogc string concatenation? |
November 03, 2016 Re: What is the simplest way of doing @nogc string concatenation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Thursday, November 03, 2016 18:54:14 Gary Willoughby via Digitalmars-d- learn wrote:
> What is the simplest way of doing @nogc string concatenation?
std.range.chain is the closest that you're going to get with actual strings. Dynamic arrays require the GC to do concatenation, because they have to allocate a new string. Now, you can do stuff like
char[] mallocConcat(const(char)[] lhs, const(char)[] right)
{
immutable len = lhs.length + right.length;
return (cast(char*)malloc(len))[0 .. len];
}
But then you have to worry about keeping track of that memory allocation somehow so that you free it appropriately later. Similarly, if you're dealing with some sort of maximum string size, you could have static array somewhere that you assign to and slice, but then you have to have somewhere appropriate for that static array to live - which is simple enough if you're just going to operate on it and then let it go away but is much harder if you actually want to pass the slice around like a normal string.
Really, if you want to be doing actual string concatenation without the GC and have it be sane, you need a string type which handles its own memory - presumably using malloc. Concenation with dynamic arrays really needs the GC - particularly in light of the fact that dynamic arrays are not reference counted.
In general though, I'd suggest trying to use chain and operating on ranges rather than creating an actual string to operate on. And if you want to avoid the whole auto-decoding thing in the process, you can combine it with std.utf.byCodeUnit.
- Jonathan M Davis
|
November 03, 2016 Re: What is the simplest way of doing @nogc string concatenation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On 11/3/16 2:54 PM, Gary Willoughby wrote:
> What is the simplest way of doing @nogc string concatenation?
Where does it go?
For instance, this should work:
auto newstr = "hello, ".chain("world");
-Steve
|
November 04, 2016 Re: What is the simplest way of doing @nogc string concatenation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Thursday, 3 November 2016 at 18:54:14 UTC, Gary Willoughby wrote:
> What is the simplest way of doing @nogc string concatenation?
I use sprintf + zero-terminated strings (or a RAII struct to convert slices to ZT strings).
|
November 04, 2016 Re: What is the simplest way of doing @nogc string concatenation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Piolat | On Friday, 4 November 2016 at 14:55:27 UTC, Guillaume Piolat wrote: > On Thursday, 3 November 2016 at 18:54:14 UTC, Gary Willoughby wrote: >> What is the simplest way of doing @nogc string concatenation? > > I use sprintf + zero-terminated strings (or a RAII struct to convert slices to ZT strings). Example with the the useful "%.s" format: https://github.com/AuburnSounds/dplug/blob/1037d8a99a6ac730f4d6cc56806dddc83bce07ed/client/dplug/client/client.d#L442 That way you don't have to add the terminal '\0' |
November 04, 2016 Re: What is the simplest way of doing @nogc string concatenation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Guillaume Piolat | On Friday, 4 November 2016 at 14:56:46 UTC, Guillaume Piolat wrote:
> On Friday, 4 November 2016 at 14:55:27 UTC, Guillaume Piolat wrote:
>> On Thursday, 3 November 2016 at 18:54:14 UTC, Gary Willoughby wrote:
>>> What is the simplest way of doing @nogc string concatenation?
>>
>> I use sprintf + zero-terminated strings (or a RAII struct to convert slices to ZT strings).
>
> Example with the the useful "%.s" format:
> https://github.com/AuburnSounds/dplug/blob/1037d8a99a6ac730f4d6cc56806dddc83bce07ed/client/dplug/client/client.d#L442
>
> That way you don't have to add the terminal '\0'
but be cautious: this will still stop on zero byte. most of the time it doesn't matter, but...
|
Copyright © 1999-2021 by the D Language Foundation