Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 21, 2014 put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself? |
September 21, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote:
> I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself?
call:
auto app = appender!string();
string[] s = ["foo", "baa"];
app.put(s);
give a:
\src\phobos\std\conv.d(9,9): Error: static assert "immutable(char) cannot be emplaced from a string." (b)
How do I fix it?
|
September 21, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | On Sun, Sep 21, 2014 at 11:41:56PM +0000, AsmMan via Digitalmars-d-learn wrote: > I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself? Try this: import std.array : appender; import std.algorithm : joiner, copy; string[] arr = ["ab", "cd", "efg"]; auto app = appender!string(); arr.joiner.copy(app); assert(app.data == "abcdefg"); T -- One reason that few people are aware there are programs running the internet is that they never crash in any significant way: the free software underlying the internet is reliable to the point of invisibility. -- Glyn Moody, from the article "Giving it all away" |
September 21, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan Attachments: | On Sun, 21 Sep 2014 23:41:56 +0000 AsmMan via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself? hm... '.put' should work. i.e. string s = `something`; auto ap = appender!string(); ap.put(s); // now ap.data returns `something` |
September 21, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Attachments: | On Mon, 22 Sep 2014 02:51:41 +0300 ketmar via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: sorry, forgot that. i misunderstood your question. |
September 22, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | On Sunday, 21 September 2014 at 23:48:59 UTC, AsmMan wrote:
> On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote:
>> I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself?
>
> call:
>
> auto app = appender!string();
> string[] s = ["foo", "baa"];
> app.put(s);
>
> give a:
>
> \src\phobos\std\conv.d(9,9): Error: static assert "immutable(char) cannot be emplaced from a string." (b)
>
> How do I fix it?
put(app, s);
This is not an ideal solution, since Appender may reallocate several times when appending the array items. Ideally, appender itself should take a range of strings, so that it can preallocate memory for them only once.
|
September 22, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | On Monday, 22 September 2014 at 00:09:22 UTC, Vladimir Panteleev wrote:
> On Sunday, 21 September 2014 at 23:48:59 UTC, AsmMan wrote:
>> On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote:
>>> I'd like to copy an array string into a appender!string() but I can't see how to do this without loop myself over the string array. Is there a native function or should I write it myself?
>>
>> call:
>>
>> auto app = appender!string();
>> string[] s = ["foo", "baa"];
>> app.put(s);
>>
>> give a:
>>
>> \src\phobos\std\conv.d(9,9): Error: static assert "immutable(char) cannot be emplaced from a string." (b)
>>
>> How do I fix it?
>
> put(app, s);
>
> This is not an ideal solution, since Appender may reallocate several times when appending the array items. Ideally, appender itself should take a range of strings, so that it can preallocate memory for them only once.
this give undefined identifier: 'put' error. (std.array is already included, buffer.put(string) doesn't give same error)
The copy of an array doesn't happen often as string but do suggest to I want something else instead of appender?
|
September 22, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | On Monday, 22 September 2014 at 00:18:03 UTC, AsmMan wrote: > this give undefined identifier: 'put' error. (std.array is already included, buffer.put(string) doesn't give same error) You need to import std.range. > The copy of an array doesn't happen often as string but do suggest to I want something else instead of appender? No, I was just thinking aloud about how Appender could be improved in the future. To minimize allocations right now, you could write your own put-like function which calls Appender.reserve. |
September 22, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | On Monday, 22 September 2014 at 00:30:44 UTC, Vladimir Panteleev wrote: > On Monday, 22 September 2014 at 00:18:03 UTC, AsmMan wrote: >> this give undefined identifier: 'put' error. (std.array is already included, buffer.put(string) doesn't give same error) > > You need to import std.range. Thanks, I was thinking std.array is enough >> The copy of an array doesn't happen often as string but do suggest to I want something else instead of appender? > > No, I was just thinking aloud about how Appender could be improved in the future. > > To minimize allocations right now, you could write your own put-like function which calls Appender.reserve. this is not critical (for now, I'm not doing any optmization) but I'll save the idea. |
September 22, 2014 Re: put string[] into a appender without loop? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Sunday, 21 September 2014 at 23:50:50 UTC, H. S. Teoh via Digitalmars-d-learn wrote:
> On Sun, Sep 21, 2014 at 11:41:56PM +0000, AsmMan via Digitalmars-d-learn wrote:
>> I'd like to copy an array string into a appender!string() but I can't
>> see how to do this without loop myself over the string array. Is there
>> a native function or should I write it myself?
>
> Try this:
>
> import std.array : appender;
> import std.algorithm : joiner, copy;
>
> string[] arr = ["ab", "cd", "efg"];
> auto app = appender!string();
> arr.joiner.copy(app);
> assert(app.data == "abcdefg");
>
>
> T
FYI, that's probably scary expensive in terms of encoding/decoding.
Using the "free" std.range.put should take care of everything, natively.
|
Copyright © 1999-2021 by the D Language Foundation