www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - from bytes to string

reply Coder <coder nospam.org> writes:
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!
Nov 27 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
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.
Nov 27 2021
parent reply Coder <coder nospam.org> writes:
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
Nov 27 2021
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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!
Nov 27 2021
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
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
Nov 27 2021
parent Coder <coder nospam.org> writes:
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!
Nov 27 2021