Handling arbitrary char ranges

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 20 14:59:11 PDT 2016


On Wednesday, 20 April 2016 at 17:09:29 UTC, Matt Kline wrote:
> [...]

First, you can't assign anything to a void[], for the same reason 
you can't dereference a void*. This includes the slice assignment 
that you are trying to do in `buf[0..minLen] = 
remainingData[0..minLen];`.

Cast the buffer to a `ubyte[]` buffer first, then you can assign 
bytes to it.

     auto bytebuf = cast(ubyte[]) buf;
     bytebuf[0] = 123;

Second, don't use slicing on ranges (unless you need it). Not all 
ranges support it...

	auto buf = [1,2,3];
	auto rng = filter!(x => x != 1)(buf);
	pragma(msg, hasSlicing!(typeof(rng))); // false

... and even ranges that support it don't support assigning to an 
array by slice:

	auto buf = new int[](3);
	buf[] = only(1,2,3)[]; // cannot implicitly convert expression 
(only(1, 2, 3).opSlice()) of type OnlyResult!(int, 3u) to int[]

Instead, use a loop (or maybe `put`) to fill the array.

Third, don't treat text as bytes; encode your characters.

	auto schema = EncodingScheme.create("utf-8");
	auto range = chain("hello", " ", "world").map!(ch => cast(char) 
ch);
	
	auto buf = new ubyte[](100);
	auto currentPos = buf;
	while(!range.empty && schema.encodedLength(range.front) <= 
currentPos.length) {
		auto written = schema.encode(range.front, currentPos);
		currentPos = currentPos[written..$];
		range.popFront();
	}
	buf = buf[0..buf.length - currentPos.length];

(PS there ought to be a range in Phobos that encodes each 
character, something like map maybe)


More information about the Digitalmars-d-learn mailing list