UTFException if c is not a valid UTF code point.
1 import std.exception : assertThrown; 2 import std.typecons : Yes; 3 4 char[4] buf; 5 6 assert(encode(buf, '\u0000') == 1 && buf[0 .. 1] == "\u0000"); 7 assert(encode(buf, '\u007F') == 1 && buf[0 .. 1] == "\u007F"); 8 assert(encode(buf, '\u0080') == 2 && buf[0 .. 2] == "\u0080"); 9 assert(encode(buf, '\uE000') == 3 && buf[0 .. 3] == "\uE000"); 10 assert(encode(buf, 0xFFFE) == 3 && buf[0 .. 3] == "\xEF\xBF\xBE"); 11 assertThrown!UTFException(encode(buf, cast(dchar) 0x110000)); 12 13 encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000); 14 auto slice = buf[]; 15 assert(slice.decodeFront == replacementDchar);
1 import std.exception : assertThrown; 2 import std.typecons : Yes; 3 4 wchar[2] buf; 5 6 assert(encode(buf, '\u0000') == 1 && buf[0 .. 1] == "\u0000"); 7 assert(encode(buf, '\uD7FF') == 1 && buf[0 .. 1] == "\uD7FF"); 8 assert(encode(buf, '\uE000') == 1 && buf[0 .. 1] == "\uE000"); 9 assert(encode(buf, '\U00010000') == 2 && buf[0 .. 2] == "\U00010000"); 10 assert(encode(buf, '\U0010FFFF') == 2 && buf[0 .. 2] == "\U0010FFFF"); 11 assertThrown!UTFException(encode(buf, cast(dchar) 0xD800)); 12 13 encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000); 14 auto slice = buf[]; 15 assert(slice.decodeFront == replacementDchar);
import std.exception : assertThrown; import std.typecons : Yes; dchar[1] buf; assert(encode(buf, '\u0000') == 1 && buf[0] == '\u0000'); assert(encode(buf, '\uD7FF') == 1 && buf[0] == '\uD7FF'); assert(encode(buf, '\uE000') == 1 && buf[0] == '\uE000'); assert(encode(buf, '\U0010FFFF') == 1 && buf[0] == '\U0010FFFF'); assertThrown!UTFException(encode(buf, cast(dchar) 0xD800)); encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000); assert(buf[0] == replacementDchar);
Encodes c into the static array, buf, and returns the actual length of the encoded character (a number between 1 and 4 for char[4] buffers and a number between 1 and 2 for wchar[2] buffers).