www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it a bug?

reply Jack Applegame <japplegame gmail.com> writes:
This doesn't compile:

import std.range;
import std.algorithm;

void main() {
	char[64] arr;
	copy(chain("test1", "test2"), arr[0..10]);
}

http://dpaste.dzfl.pl/24230ac02e6e
Nov 25 2015
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Wednesday, 25 November 2015 at 08:10:03 UTC, Jack Applegame 
wrote:
 This doesn't compile:

 import std.range;
 import std.algorithm;

 void main() {
 	char[64] arr;
 	copy(chain("test1", "test2"), arr[0..10]);
 }

 http://dpaste.dzfl.pl/24230ac02e6e
Essentially this comes down to the question: 'Should output ranges auto-encode like input ranges auto-decode'. The code above suggests the answer is currently "no". Workarounds: Either do `dchar[64] arr;` or import std.range; import std.algorithm; import std.utf; void main() { char[64] arr; copy(chain("test1", "test2").byCodeUnit, arr[0..10].byCodeUnit); } ranges iterate over strings by code-point (i.e. dchar). byCodeUnit forces iteration by code-unit i.e. char. You could also do import std.range; import std.algorithm; void main() { char[64] arr; copy(chain(cast(immutable(ubyte)[])"test1", cast(immutable(ubyte)[])"test2"), cast(ubyte[])arr[0..10]); } or any other method that means you deal in ubyte[] instead of char[].
Nov 25 2015
parent Jack Applegame <japplegame gmail.com> writes:
On Wednesday, 25 November 2015 at 09:31:15 UTC, John Colvin wrote:
 import std.range;
 import std.algorithm;
 import std.utf;

 void main() {
 	char[64] arr;
 	copy(chain("test1", "test2").byCodeUnit, 
 arr[0..10].byCodeUnit);
 }
I'll use byCodeUnit. Thanks.
Nov 25 2015