www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the simplest way of doing nogc string concatenation?

reply Gary Willoughby <dev nomad.so> writes:
What is the simplest way of doing  nogc string concatenation?
Nov 03 2016
next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
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
Nov 03 2016
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Nov 03 2016
prev sibling parent reply Guillaume Piolat <first.last gmail.com> writes:
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).
Nov 04 2016
parent reply Guillaume Piolat <first.last gmail.com> writes:
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'
Nov 04 2016
parent ketmar <ketmar ketmar.no-ip.org> writes:
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...
Nov 04 2016