string is rarely useful as a function argument

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Dec 30 16:03:58 PST 2011


On 12/30/11 5:07 PM, Timon Gehr wrote:
> On 12/31/2011 12:00 AM, Andrei Alexandrescu wrote:
>> On 12/30/11 4:01 PM, Walter Bright wrote:
>>> On 12/30/2011 11:55 AM, Timon Gehr wrote:
>>>> Me too. I think the way we have it now is optimal.
>>>
>>> Consider your X macro implementation. Strip out the utf.stride code and
>>> use plain indexing - it will not break the code in any way. The naive
>>> implementation still works correctly with ASCII and UTF-8.
>>>
>>> That's not true for any other multibyte encoding, which is why UTF-8 is
>>> inspired genius.
>>
>> It's true for any encoding with the prefix property, such as Huffman.
>>
>> Using .raw is /optimal/ because it states the assumption appropriately.
>> The user knows '$' cannot be in the prefix of any other symbol, so she
>> can state the byte alone is the character. If that were a non-ASCII
>> character, the assumption wouldn't have worked.
>>
>> So yeah, UTF-8 is great. But it is not miraculous. We need .raw.
>>
>>
>> Andrei
>
> auto raw(S)(S s) if(isNarrowString!S){
> static if(is(S==string)) return cast(ubyte[])s;
> else static if(is(S==wstring)) return cast(ushort[])s;
> }

Almost there.

https://github.com/D-Programming-Language/phobos/blob/master/std/string.d#L809


Andrei


More information about the Digitalmars-d mailing list