asString

Utility function which interprets and validates a byte array as a UTF-8 string.

Most of $(ZMQD)'s message API deals in ubyte[] arrays, but very often, the message _data contains plain text. asString() allows for easy and safe interpretation of raw _data as characters. It checks that data is a valid UTF-8 encoded string, and returns a char[] array that refers to the same memory region.

@safe pure
inout(char)[]
asString
(
inout(ubyte)[] data
)

Throws

$(STDREF utf,UTFException) if data is not a valid UTF-8 string.

Examples

auto s1 = Socket(SocketType.pair);
s1.bind("inproc://zmqd_asString_example");
auto s2 = Socket(SocketType.pair);
s2.connect("inproc://zmqd_asString_example");

auto msg = Frame(12);
msg.data.asString()[] = "Hello World!";
s1.send(msg);

ubyte[12] buf;
s2.receive(buf);
assert(buf.asString() == "Hello World!");

See Also

$(STDREF string,representation), which performs the opposite operation.

Meta