digitalmars.D.learn - What is the simplest way of doing nogc string concatenation?
- Gary Willoughby (1/1) Nov 03 2016 What is the simplest way of doing @nogc string concatenation?
- Jonathan M Davis via Digitalmars-d-learn (27/28) Nov 03 2016 std.range.chain is the closest that you're going to get with actual stri...
- Steven Schveighoffer (5/6) Nov 03 2016 Where does it go?
- Guillaume Piolat (4/5) Nov 04 2016 I use sprintf + zero-terminated strings (or a RAII struct to
- Guillaume Piolat (5/10) Nov 04 2016 Example with the the useful "%.s" format:
- ketmar (4/15) Nov 04 2016 but be cautious: this will still stop on zero byte. most of the
What is the simplest way of doing nogc string concatenation?
Nov 03 2016
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
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
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
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: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'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
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:but be cautious: this will still stop on zero byte. most of the time it doesn't matter, but...On Thursday, 3 November 2016 at 18:54:14 UTC, Gary Willoughby wrote: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'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