Re: no [] operator overload for type Chunks!(char[])

2018-05-31 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/30/18 5:41 PM, Malte wrote:

On Wednesday, 30 May 2018 at 21:27:44 UTC, Ali Çehreli wrote:

On 05/30/2018 02:19 PM, Malte wrote:
Why does this code complain at the last line about a missing [] 
operator overload?


auto buffer = new char[6];
auto chunked = buffer.chunks(3);
chunked[1][2] = '!';

Same happens with wchar.
Dchar and byte work as expected.


UTF-8 auto decoding strikes again. :)

Even though the original container is char[], passing it through 
Phobos algorithms generated range of dchar. The thing is, those dchar 
elements are generated (decoded from chars) "on the fly" as one 
iterates over the range. Which means, there is no array of dchar to 
speak of, so there is no random access.




I see. Not what I would have expected, but makes sense for people 
working with UTF-8 strings.


Thanks for the fast answer.


You can use byCodeUnit to turn it back into an indexable range:

auto chunked = buffer.byCodeUnit.chunks(3);

-Steve


Re: no [] operator overload for type Chunks!(char[])

2018-05-30 Thread Malte via Digitalmars-d-learn

On Wednesday, 30 May 2018 at 21:27:44 UTC, Ali Çehreli wrote:

On 05/30/2018 02:19 PM, Malte wrote:
Why does this code complain at the last line about a missing 
[] operator overload?


auto buffer = new char[6];
auto chunked = buffer.chunks(3);
chunked[1][2] = '!';

Same happens with wchar.
Dchar and byte work as expected.


UTF-8 auto decoding strikes again. :)

Even though the original container is char[], passing it 
through Phobos algorithms generated range of dchar. The thing 
is, those dchar elements are generated (decoded from chars) "on 
the fly" as one iterates over the range. Which means, there is 
no array of dchar to speak of, so there is no random access.


Ali


I see. Not what I would have expected, but makes sense for people 
working with UTF-8 strings.


Thanks for the fast answer.


Re: no [] operator overload for type Chunks!(char[])

2018-05-30 Thread Ali Çehreli via Digitalmars-d-learn

On 05/30/2018 02:19 PM, Malte wrote:
Why does this code complain at the last line about a missing [] operator 
overload?


auto buffer = new char[6];
auto chunked = buffer.chunks(3);
chunked[1][2] = '!';

Same happens with wchar.
Dchar and byte work as expected.


UTF-8 auto decoding strikes again. :)

Even though the original container is char[], passing it through Phobos 
algorithms generated range of dchar. The thing is, those dchar elements 
are generated (decoded from chars) "on the fly" as one iterates over the 
range. Which means, there is no array of dchar to speak of, so there is 
no random access.


Ali


no [] operator overload for type Chunks!(char[])

2018-05-30 Thread Malte via Digitalmars-d-learn
Why does this code complain at the last line about a missing [] 
operator overload?


auto buffer = new char[6];
auto chunked = buffer.chunks(3);
chunked[1][2] = '!';

Same happens with wchar.
Dchar and byte work as expected.