www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Building a string from n chars

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
Is there a simpler way to way to

s ~= repeat('*', n).array.to!string;

if s has to be of type string?
Sep 03 2014
next sibling parent reply "Meta" <jared771 gmail.com> writes:
On Wednesday, 3 September 2014 at 19:43:26 UTC, Nordlöw wrote:
 Is there a simpler way to way to

 s ~= repeat('*', n).array.to!string;

 if s has to be of type string?
Does this work? s ~= "*".replicate(n);
Sep 03 2014
next sibling parent "Meta" <jared771 gmail.com> writes:
On Wednesday, 3 September 2014 at 20:20:09 UTC, Meta wrote:
 On Wednesday, 3 September 2014 at 19:43:26 UTC, Nordlöw wrote:
 Is there a simpler way to way to

 s ~= repeat('*', n).array.to!string;

 if s has to be of type string?
Does this work? s ~= "*".replicate(n);
Sorry, I should qualify that replicate is from std.array. You can also just do either `s ~= repeat('*', n).array` OR `s ~= repeat('*', n).to!string`. Either should work.
Sep 03 2014
prev sibling parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Wednesday, 3 September 2014 at 20:20:09 UTC, Meta wrote:
 Does this work?

 s ~= "*".replicate(n);
Yes, thanks. So what's best? type ~= '*'.repeat(pointerCount).array; or type ~= "*".replicate(pointerCount); ? Further, -vgc says only ~= will allocate: t_repeat_replicate.d(12,19): vgc: operator ~= may cause GC allocation t_repeat_replicate.d(13,19): vgc: operator ~= may cause GC allocation Is DMD/Phobos already that clever!? :=)
Sep 04 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 4 September 2014 at 19:22:42 UTC, Nordlöw wrote:
 Is DMD/Phobos already that clever!?
Further -vgc has nothing to say about string t1; t1 ~= '*'.repeat(n).array; string t2; t2 ~= "*".replicate(n); .
Sep 04 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 4 September 2014 at 19:24:00 UTC, Nordlöw wrote:
     string t1; t1 ~= '*'.repeat(n).array;
     string t2; t2 ~= "*".replicate(n);
After having read I came to the conclusion that the lazy std.range:repeat is preferred. I'm still a bit confused about the fact that -vgc gives no warnings about GC-allocations, though.
Sep 04 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 4 September 2014 at 20:38:39 UTC, Nordlöw wrote:
 On Thursday, 4 September 2014 at 19:24:00 UTC, Nordlöw wrote:
    string t1; t1 ~= '*'.repeat(n).array;
    string t2; t2 ~= "*".replicate(n);
After having read I came to the conclusion that the lazy std.range:repeat is preferred.
If lazy is good enough for you yes. AFAIK, replicate is *very* close in terms of implementation to what a.repeat(n).array() would do anyways. Heck, I'd be surprised if it did it differently, since (again, AFAIK) repeat.array() is "optimal" anyways.
 I'm still a bit confused about the fact that -vgc gives no 
 warnings about GC-allocations, though.
Strange indeed. Both solutions allocate a slice, and then append that slice. The "s[]='*'" Solution I gave you will not create a temporary allocation.
Sep 04 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 4 September 2014 at 20:57:43 UTC, monarch_dodra 
wrote:
 On Thursday, 4 September 2014 at 20:38:39 UTC, Nordlöw wrote:
 On Thursday, 4 September 2014 at 19:24:00 UTC, Nordlöw wrote:
   string t1; t1 ~= '*'.repeat(n).array;
   string t2; t2 ~= "*".replicate(n);
After having read I came to the conclusion that the lazy std.range:repeat is preferred.
If lazy is good enough for you yes. AFAIK, replicate is *very* close in terms of implementation to what a.repeat(n).array() would do anyways. Heck, I'd be surprised if it did it differently, since (again, AFAIK) repeat.array() is "optimal" anyways.
I re-read the doc and implementation: replicate replicates a *range*. It is a bit optimized to detect the case where the range is a single element, but it still has to do the check, and even then (implementation detail), it is less efficient. I might create a pull to tweak that.
Sep 04 2014
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 3 September 2014 at 19:43:26 UTC, Nordlöw wrote:
 Is there a simpler way to way to

 s ~= repeat('*', n).array.to!string;

 if s has to be of type string?
s ~= repeat('*', n).array(); Should be enough. Why the "to!string"? There's 1 useless allocation, but I think that's OK for code this trivial? Else, you can do: s.length+=n; s[$-n .. $] []= '*'; This is also relatively simple. The ownside is doing double assignement, as "length" will initialize new elements to "char.init". Probably not noticeable. There *might* be more "efficient" ways to do it, but I'd doubt it qualifies as "simpler". Or if it's really observeable. Are these good enough for you?
Sep 03 2014
next sibling parent "Cassio Butrico" <cassio_butrico ig.com.br> writes:
On Wednesday, 3 September 2014 at 20:46:40 UTC, monarch_dodra 
wrote:
 On Wednesday, 3 September 2014 at 19:43:26 UTC, Nordlöw wrote:
 Is there a simpler way to way to

 s ~= repeat('*', n).array.to!string;

 if s has to be of type string?
s ~= repeat('*', n).array(); Should be enough. Why the "to!string"? There's 1 useless allocation, but I think that's OK for code this trivial? Else, you can do: s.length+=n; s[$-n .. $] []= '*'; This is also relatively simple. The ownside is doing double assignement, as "length" will initialize new elements to "char.init". Probably not noticeable. There *might* be more "efficient" ways to do it, but I'd doubt it qualifies as "simpler". Or if it's really observeable. Are these good enough for you?
hello Nordlöw a time I did something like that, see. string repet(string a, int i) { int b; string c; for(b=0;b<=i;b++) {c ~= a;} return c; } can be adapted to char[].
Sep 03 2014
prev sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Wednesday, 3 September 2014 at 20:46:40 UTC, monarch_dodra 
wrote:
 Is there a simpler way to way to

 s ~= repeat('*', n).array.to!string;

 if s has to be of type string?
s ~= repeat('*', n).array(); Should be enough. Why the "to!string"?
You're correct. The .to!string was unnecessary.
Sep 04 2014