about std.string.representation

bioinfornatics bioinfornatics at fedoraproject.org
Tue Nov 19 15:57:59 PST 2013


On Tuesday, 19 November 2013 at 23:53:33 UTC, bioinfornatics 
wrote:
> On Tuesday, 19 November 2013 at 06:11:26 UTC, Ali Çehreli wrote:
>> On 11/18/2013 07:48 PM, bioinfornatics wrote:
>>> On Thursday, 14 November 2013 at 12:01:04 UTC, Ali Çehreli 
>>> wrote:
>>>> On 11/13/2013 04:32 PM, bioinfornatics wrote:
>>>>> Hi,
>>>>> I try to understand which type char, dchar, wchar will give
>>>>> ubyte,ushort,uint…
>>>>
>>>> And for templates, there is std.range.ElementEncodingType:
>>>>
>>>> import std.stdio;
>>>> import std.range;
>>>>
>>>> void foo(R)(R range)
>>>> {
>>>>   // In contrast, ElementType!R for strings is always dchar
>>>>   writeln(typeid(ElementEncodingType!R));
>>>> }
>>>>
>>>> void main()
>>>> {
>>>>   string  t  = "test";
>>>>   char[]  c  = "test".dup;
>>>>   dchar[] dc = "test"d.dup;
>>>>   wchar[] wc = "test"w.dup;
>>>>
>>>>   foo(t);
>>>>   foo(c);
>>>>   foo(dc);
>>>>   foo(wc);
>>>> }
>>>>
>>>> Prints:
>>>>
>>>> immutable(char)
>>>> char
>>>> dchar
>>>> wchar
>>>>
>>>> Ali
>>>
>>> Thanks Ali that is interesting too …
>>>
>>> In same way i would like to know if they are a function wich 
>>> return
>>> ubyte, ushort, uint instead of:
>>>
>>> - char, dchar, wchar from std.range.ElementEncodingType
>>> - ubyte[], ushort[], uint[]  from std.string.representation
>>>
>>>
>>> maybe:
>>>
>>> foo(T)( string s ){
>>>    alias T typeof(s.representation[0]);
>>>>>>>>>>>> }
>>
>> I don't know an existing function but I think the following is 
>> what you are looking for:
>>
>> import std.range;
>>
>> template NonUtfElementEncodingType(S)
>> {
>>    alias ET = ElementEncodingType!S;
>>
>>    static if (is (ET == char)) {
>>        alias NonUtfElementEncodingType = ubyte;
>>
>>    } else static if (is (ET == wchar)) {
>>        alias NonUtfElementEncodingType = ushort;
>>
>>    } else static if (is (ET == dchar)) {
>>        alias NonUtfElementEncodingType = uint;
>>
>>    } else {
>>        alias NonUtfElementEncodingType = ET;
>>    }
>> }
>>
>> void main()
>> {
>>    alias Foo = NonUtfElementEncodingType!string;
>>    Foo[] myByteArray;
>> }
>>
>> Ali
>
> Yes that is what i want. whyu not to use a piece of code from 
> std.string.representation ?
>
> auto CharEncodingType(Char)(Char[] s) pure nothrow
>     if(isSomeChar!Char)
> {
>     // Get representation type
>     alias TypeTuple!(ubyte, ushort, uint)U;
>
>     // const and immutable storage classes
>     static if (is(Char == immutable)) alias immutable(U) T;
>     else static if (is(Char == const)) alias const(U) T;
>     else alias U T;
>
>     // shared storage class (because shared(const(T)) is 
> possible)
>     static if (is(Char == shared)) alias shared(T) ST;
>     else alias T ST;
>
>     return cast(ST) s;
> }

FIX

template CharEncodingType(Char[] s) pure nothrow
     if(isSomeChar!Char)
{
     // Get representation type
     alias TypeTuple!(ubyte, ushort, uint)[Char.sizeof / 2] U;

     // const and immutable storage classes
     static if (is(Char == immutable)) alias immutable(U) T;
     else static if (is(Char == const)) alias const(U) T;
     else alias U T;

     // shared storage class (because shared(const(T)) is possible)
     static if (is(Char == shared)) alias shared(T) 
CharEncodingType;
     else alias T CharEncodingType;
}


More information about the Digitalmars-d-learn mailing list