ubyte[] to string with @nogc - different behaviors

Andy Balba pwplus7 at gmail.com
Mon Aug 10 03:56:16 UTC 2020


On Wednesday, 5 August 2020 at 21:15:10 UTC, Luhrel wrote:
> On Wednesday, 5 August 2020 at 20:28:22 UTC, Steven 
> Schveighoffer wrote:
>>
>> ...
>>
>> But still, you are mallocing a block and throwing it away.
>>
>> And I realize, this wouldn't have worked anyway, as str is a 
>> string, which means you can't overwrite the data.
>>
>> I think there is likely a better way to do what you are trying 
>> to do.
>>
>
> Maybe with a ref parameter, but I think this will only move the 
> problem outside the function.
>
>>
>>...
>>
>> What is the source of the incoming data? Will it be around by 
>> the time you need to use this string? Can you just slice it 
>> without copying? I'm not familiar with this code, so I don't 
>> know the requirements.
>>
>> I would think:
>>
>> return cast(const(char)[])buffer[0 .. strnlen(buffer.ptr, 
>> buffer.length))];
>>
>> or something like that.
>>
>> -Steve
>
> Will try that.


What am I missing here ??
Prior to reading this post, I would have tried

  ubyte[4] ss= [0x23, 0x24, 0x25, 0x26] ;    writeln ("ss ", ss);
.. which outputs (as expected) ss  [35, 36, 37, 38]

And to convert ss to string, I would have tried :
     string sss = cast(string) ss;           writeln ("sss ", sss);
.. which outputs (as expected) sss  #$%&


So, unless I'm missing something, isn't converting ubyte[] to 
string super simple ?



More information about the Digitalmars-d mailing list