Handling arbitrary char ranges

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 20 22:38:28 PDT 2016


On 21.04.2016 04:35, Alex Parrill wrote:
> On Wednesday, 20 April 2016 at 22:44:37 UTC, ag0aep6g wrote:
>> On 20.04.2016 23:59, Alex Parrill wrote:
[...]
> That's not assigning the elements of a void[]; it's just changing what
> the slice points to and adjusting the length, like doing `void* ptr =
> someOtherPtr;`

True, but assigning elements is possible via slices as shown.

[...]
> It only seems to work on arrays, not arbitrary ranges, sliceable or not.
> Though see below.

Yes, assigning slices and more complex vector operations only works with 
dynamic arrays.

[...]
>>>      auto range = chain("hello", " ", "world").map!(ch => cast(char)
>>> ch);
[...]
>>>          auto written = schema.encode(range.front, currentPos);
[...]
>> You're "converting" chars to UTF-8 here, right? That's a nop. char is
>> a UTF-8 code unit already.
>
> It can be either chars, wchars, or dchars.

Your range specifically has element type char, though. Not wchar or 
dchar. And Matt Kline wants to work on char ranges (and maybe string), 
not on arbitrary ranges of char/wchar/dchar.

[...]
> byChar would work. byWChar and byDChar might cause endian-ness issues.

Easily combined with the endianess functions from std.bitmanip:
----
void main()
{
     import std.algorithm: equal;
     import std.bitmanip: nativeToBigEndian, nativeToLittleEndian;
     import std.utf: byWchar;

     string utf8 = "foobär";
     auto utf16le = utf8.byWchar.map!nativeToLittleEndian;
     auto utf16be = utf8.byWchar.map!nativeToBigEndian;

     assert(equal(utf16le,
         [['f', 0], ['o', 0], ['o', 0], ['b', 0], [0xE4, 0], ['r', 0]]));

     assert(equal(utf16be,
         [[0, 'f'], [0, 'o'], [0, 'o'], [0, 'b'], [0, 0xE4], [0, 'r']]));
}
----


More information about the Digitalmars-d-learn mailing list