Jump to page: 1 2
Thread overview
Efficient string concatenation?
Nov 15, 2013
Justin Whear
Nov 15, 2013
Justin Whear
Nov 15, 2013
Brad Anderson
Nov 15, 2013
qznc
Nov 15, 2013
Ali Çehreli
Nov 16, 2013
ilya-stromberg
Nov 16, 2013
Ali Çehreli
Nov 16, 2013
JR
November 15, 2013
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 the common solution in Java and C#), but did not find anything similar.

What is the D way of doing efficient string concatenation (especially if it spans multiple statements, e.g. while in a loop)?
November 15, 2013
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 the common solution in Java and C#), but did not find anything similar.
> 
> 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;
November 15, 2013
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.
November 15, 2013
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 that is the common solution in Java and C#), but did not find 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.
November 15, 2013
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.

November 15, 2013
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.
November 15, 2013
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

November 15, 2013
Thanks for the book! I printed it, all 673 pages of it. Immense work you have there.
November 16, 2013
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.
November 16, 2013
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
« First   ‹ Prev
1 2