Convert ubyte [] to string in D

I get ubyte [] from an untrusted source and must convert it to a utf-8 encoded string. How can I convert it and verify that the bytes I gave are valid utf-8 data? There is no function in phobos that does this directly (i.e., it accepts ubyte [] or a range of ubyte and converts it to a string or a range of characters).

+5
source share
1 answer

std.utf.validate ?

And something like that?

 import std.stdio; import std.utf; void main() { ubyte[] bytes = cast(ubyte[])""; writeln("bytes: ", bytes); string str = cast(string)bytes; writeln("string: ", str); validate(str); writeln("valid"); } 

Application Output:

 bytes: [209, 129, 208, 190, 208, 177, 208, 176, 208, 186, 208, 176] string:  valid 
+2
source

Source: https://habr.com/ru/post/1238872/


All Articles