www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - copy only reference rather duplicate a string in appender!string

reply Marc <jckj33 gmail.com> writes:
I do build a string by coping large parts of diffrent buffers, 
all those buffers live after the functional call, so rather than 
duplicate those string I'd like to copy only references to those 
parts rather duplicate every string. I combined appender!string, 
assumeUnique() and array slices. Something like this:

 auto buffer = appender!string;
 auto list = cycle(buffers);
 while(bufferIsFilled) {
   char[] buffer = get_buffer(); // pop a buffer from circular 
 buffer
   int s = get_buffer_size(x); // determine which part of that 
 buffer we need
   buffer.put(assumeUnique(buf[0 .. s])); // and here's my 
 question
 }
 return buffer.data;
Dec 26 2017
next sibling parent Marc <jckj33 gmail.com> writes:
of course a totally different approach to solve this is welcome, 

converting my thinking to the D way (which is new for me, since 
I'm even unifamiliar with python and such, which got such 
friendly syntax)
Dec 26 2017
prev sibling parent Neia Neutuladh <neia ikeran.org> writes:
On Tuesday, 26 December 2017 at 15:37:12 UTC, Marc wrote:
 I do build a string by coping large parts of diffrent buffers, 
 all those buffers live after the functional call, so rather 
 than duplicate those string I'd like to copy only references to 
 those parts rather duplicate every string. I combined 
 appender!string, assumeUnique() and array slices. Something 
 like this:
This depends on whether you have several variables as buffers or an array of buffers. With several variables: http://dpldocs.info/experimental-docs/std.range.chain.html With an array, or something else iterable: http://dpldocs.info/experimental-docs/std.algorithm.iteration.joiner.2.html So you can use: chain(buf1, buf2, buf3); Or: myBuffers = [buf1, buf2, buf3]; joiner(myBuffers); That produces something string-like. (A rope, but with a poor API.) You can iterate through it as a string, you can output it to a file, etc. You can't pass it to something that expects a string specifically; for instance, you can't return that from `MyClass.toString()`.
Dec 26 2017