Thread overview
from bytes to string
Nov 27, 2021
Coder
Nov 27, 2021
Adam D Ruppe
Nov 27, 2021
Coder
Nov 27, 2021
H. S. Teoh
Nov 27, 2021
Adam D Ruppe
Nov 27, 2021
Coder
November 27, 2021

I'm lost, std.utf, std.conv, std.encoding, std.uni

My application is receiving data over a socket as immutable(ubyte)[].
How to validate them and transform them to utf8 string? What is the best way?

Thank you!

November 27, 2021
On Saturday, 27 November 2021 at 13:54:11 UTC, Coder wrote:
> My application is receiving data over a socket as immutable(ubyte)[].
> How to validate them and transform them to utf8 string? What is the best way?

If they're already supposed to be utf8, just cast it to char[] then you can call std.utf.validate on it if you want and idup it into a string to keep.

If it is some other encoding... then it depends on what encoding.
November 27, 2021
On Saturday, 27 November 2021 at 13:56:46 UTC, Adam D Ruppe wrote:
> On Saturday, 27 November 2021 at 13:54:11 UTC, Coder wrote:
>> My application is receiving data over a socket as immutable(ubyte)[].
>> How to validate them and transform them to utf8 string? What is the best way?
>
> If they're already supposed to be utf8, just cast it to char[] then you can call std.utf.validate on it if you want and idup it into a string to keep.
>
> If it is some other encoding... then it depends on what encoding.

Thank you Adam,

Question, why a function can not be nothrow if I catch in the body?

void foo() nothrow {
    import std.utf : validate, UTFException;
    try {
        validate("a");
    }
    catch(UTFException){
    }
}
Error: function `std.utf.validate!string.validate` is not `nothrow`
Error: `nothrow` function `foo` may throw

November 27, 2021
On Sat, Nov 27, 2021 at 03:24:43PM +0000, Coder via Digitalmars-d-learn wrote: [...]
> Question, why a function can not be nothrow if I catch in the body?
> 
> void foo() nothrow {
>     import std.utf : validate, UTFException;
>     try {
>         validate("a");
>     }
>     catch(UTFException){
>     }
> }
> Error: function `std.utf.validate!string.validate` is not `nothrow`
> Error: `nothrow` function `foo` may throw

Because it may throw some other kind of Exception besides UTFException.


T

-- 
Stop staring at me like that! It's offens... no, you'll hurt your eyes!
November 27, 2021
On Saturday, 27 November 2021 at 15:24:43 UTC, Coder wrote:
> Question, why a function can not be nothrow if I catch in the body?

Ever play Pokemon? You can't just catch the cute Bulbasaur and call it done (even though the grass type is like playing on easy mode). You gotta catch 'em all!

> void foo() nothrow {
>     import std.utf : validate, UTFException;
>     try {
>         validate("a");
>     }
>     catch(Exception){ // make that Exception
>     }
> }

cuz nothrow doesn't know about specific exception types, it only looks at Exception as a whole. It doesn't actually know what validate throws.


All right, we gotta rap some pokemon like its 1998.

electro
diglet
nidoran
mankey
venosaur
November 27, 2021
On Saturday, 27 November 2021 at 15:29:45 UTC, Adam D Ruppe wrote:
> On Saturday, 27 November 2021 at 15:24:43 UTC, Coder wrote:
>> Question, why a function can not be nothrow if I catch in the body?
>
> Ever play Pokemon? You can't just catch the cute Bulbasaur and call it done (even though the grass type is like playing on easy mode). You gotta catch 'em all!
>
>> void foo() nothrow {
>>     import std.utf : validate, UTFException;
>>     try {
>>         validate("a");
>>     }
>>     catch(Exception){ // make that Exception
>>     }
>> }
>
> cuz nothrow doesn't know about specific exception types, it only looks at Exception as a whole. It doesn't actually know what validate throws.
>
>
> All right, we gotta rap some pokemon like its 1998.
>
> electro
> diglet
> nidoran
> mankey
> venosaur

Thumbs Up!