www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Efficient string concatenation?

reply "Jacek Furmankiewicz" <jacek99 gmail.com> writes:
Since D strings are immutable (like in most other languages), 
string concatenation is usually pretty inefficient due to the 
need to create a new copy of the string every time.

I presume string concatenation using the typical array syntax can 
be optimized by the compiler to do all of this in one shot, e..g

string newString = string1 ~ string2 ~ string3;

but what happens if I need to concatenante a large string in a 
loop?

I tried looking through Phobos for a StringBuilder class (since 

anything similar.

What is the D way of doing efficient string concatenation 
(especially if it spans multiple statements, e.g. while in a 
loop)?
Nov 15 2013
next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Fri, 15 Nov 2013 23:26:19 +0100, Jacek Furmankiewicz wrote:

 Since D strings are immutable (like in most other languages), string
 concatenation is usually pretty inefficient due to the need to create a
 new copy of the string every time.
 
 I presume string concatenation using the typical array syntax can be
 optimized by the compiler to do all of this in one shot, e..g
 
 string newString = string1 ~ string2 ~ string3;
 
 but what happens if I need to concatenante a large string in a loop?
 
 I tried looking through Phobos for a StringBuilder class (since that is

 
 What is the D way of doing efficient string concatenation (especially if
 it spans multiple statements, e.g. while in a loop)?
std.array has an Appender type that can be used to build up a string (or any other array type) efficiently. E.g.: auto strBuilder = appender!string; while (...) { str.put("foo"); } // Get the array out: string str = strBuilder.data;
Nov 15 2013
prev sibling next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Fri, 15 Nov 2013 22:30:35 +0000, Justin Whear wrote:

 std.array has an Appender type that can be used to build up a string (or
 any other array type) efficiently.
Oh, and if you have an idea of how large the result might grow, be sure to use the reserve() method on the appender.
Nov 15 2013
prev sibling parent reply "Brad Anderson" <eco gnuk.net> writes:
On Friday, 15 November 2013 at 22:26:20 UTC, Jacek Furmankiewicz 
wrote:
 Since D strings are immutable (like in most other languages), 
 string concatenation is usually pretty inefficient due to the 
 need to create a new copy of the string every time.

 I presume string concatenation using the typical array syntax 
 can be optimized by the compiler to do all of this in one shot, 
 e..g

 string newString = string1 ~ string2 ~ string3;

 but what happens if I need to concatenante a large string in a 
 loop?

 I tried looking through Phobos for a StringBuilder class (since 

 anything similar.

 What is the D way of doing efficient string concatenation 
 (especially if it spans multiple statements, e.g. while in a 
 loop)?
Appender in std.array is probably what you are looking for. std.algorithm.joiner is also useful (no allocations at all even) but the use case is a bit different.
Nov 15 2013
next sibling parent reply "Jacek Furmankiewicz" <jacek99 gmail.com> writes:
Thank you all.

I am learning D by going through Ali Cehreli's otherwise 
excellent "Programming in D" PDF and he did not show this in his 
initial chapter on Strings.
Nov 15 2013
next sibling parent "qznc" <qznc web.de> writes:
On Friday, 15 November 2013 at 22:35:48 UTC, Jacek Furmankiewicz 
wrote:
 I am learning D by going through Ali Cehreli's otherwise 
 excellent "Programming in D" PDF and he did not show this in 
 his initial chapter on Strings.
Well, Appender is not string specific. D feels like being in a different league in terms of generic programming. Many stdlib stuff is implemented more abstract and generic, which makes it harder to find. Looking for some string operations, you might find it in std.algorithm, std.array, std.range, or std.string. Maybe it is different to C++ programmer, who are used to this from the STL.
Nov 15 2013
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/15/2013 02:35 PM, Jacek Furmankiewicz wrote:

 "Programming in D" PDF and he did not show this in his initial chapter on
 Strings.
Sorry about that. :) As I was targeting novices to programming, I tried to give as much as needed but as little as possible, so that the reader would not be overwhelmed. (I have to admit that the chapters about arrays, strings, and slices can be arranged in a better way.) In doing so, some important concepts have either been totally forgotten :p or appeared too late in the book. I just checked, Appender is all the way down in the Ranges chapter and not even as a proper section! Oops! :) Ali
Nov 15 2013
parent reply "Jacek Furmankiewicz" <jacek99 gmail.com> writes:
Thanks for the book! I printed it, all 673 pages of it. Immense 
work you have there.
Nov 15 2013
parent reply "ilya-stromberg" <ilya-stromberg-2009 yandex.ru> writes:
On Friday, 15 November 2013 at 23:51:42 UTC, Jacek Furmankiewicz 
wrote:
 Thanks for the book! I printed it, all 673 pages of it. Immense 
 work you have there.
I think it's good to listen a little critics from newcomers. I belive that it helps Ali Cehreli to improve the book. Also, you can use `text` function from `std.conv`. It can convert any data to the `string`. string newString = text(string1, 1, string2, 2, string3, 3); Note that it use `~` operator, so `appender` can be faster. P.S. We also have great book for D templates: https://github.com/PhilippeSigaud/D-templates-tutorial
Nov 16 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/16/2013 08:59 AM, ilya-stromberg wrote:

 I think it's good to listen a little critics from newcomers. I belive
 that it helps Ali Cehreli to improve the book.
Exactly! :) Two quotes from the Introduction chapter: "If you come across chapters that you find to be particularly difficult, it may be because the book fails to introduce all of the necessary concepts. Please e-mail the author about such problems to help make this book more useful." [...] "Please e-mail the author with any comments and corrections regarding this book. Thank you." Ali
Nov 16 2013
prev sibling parent "JR" <zorael gmail.com> writes:
On Friday, 15 November 2013 at 22:33:34 UTC, Brad Anderson wrote:
 Appender in std.array is probably what you are looking for.  
 std.algorithm.joiner is also useful (no allocations at all 
 even) but the use case is a bit different.
Is Appender considered up to Phobos' current standards? I vaguely remember reading something about its internal memory management being worthy of critique.
Nov 16 2013