View source code
Display the source code in std/utf.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.utf.encode - multiple declarations

Function encode

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).

size_t encode(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar) (
  out char[4] buf,
  dchar c
) pure @safe;

size_t encode(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar) (
  out wchar[2] buf,
  dchar c
) pure @safe;

size_t encode(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar) (
  out dchar[1] buf,
  dchar c
) pure @safe;

Throws

UTFException if c is not a valid UTF code point.

Example

import std.exception : assertThrown;
import std.typecons : Yes;

char[4] buf;

assert(encode(buf, '\u0000') == 1 && buf[0 .. 1] == "\u0000");
assert(encode(buf, '\u007F') == 1 && buf[0 .. 1] == "\u007F");
assert(encode(buf, '\u0080') == 2 && buf[0 .. 2] == "\u0080");
assert(encode(buf, '\uE000') == 3 && buf[0 .. 3] == "\uE000");
assert(encode(buf, 0xFFFE) == 3 && buf[0 .. 3] == "\xEF\xBF\xBE");
assertThrown!UTFException(encode(buf, cast(dchar) 0x110000));

encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000);
auto slice = buf[];
writeln(slice.decodeFront); // replacementDchar

Example

import std.exception : assertThrown;
import std.typecons : Yes;

wchar[2] buf;

assert(encode(buf, '\u0000') == 1 && buf[0 .. 1] == "\u0000");
assert(encode(buf, '\uD7FF') == 1 && buf[0 .. 1] == "\uD7FF");
assert(encode(buf, '\uE000') == 1 && buf[0 .. 1] == "\uE000");
assert(encode(buf, '\U00010000') == 2 && buf[0 .. 2] == "\U00010000");
assert(encode(buf, '\U0010FFFF') == 2 && buf[0 .. 2] == "\U0010FFFF");
assertThrown!UTFException(encode(buf, cast(dchar) 0xD800));

encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000);
auto slice = buf[];
writeln(slice.decodeFront); // replacementDchar

Example

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);
writeln(buf[0]); // replacementDchar

Function encode

Encodes c in str's encoding and appends it to str.

void encode(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar) (
  scope ref char[] str,
  dchar c
) pure @safe;

void encode(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar) (
  scope ref wchar[] str,
  dchar c
) pure @safe;

void encode(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar) (
  scope ref dchar[] str,
  dchar c
) pure @safe;

Throws

UTFException if c is not a valid UTF code point.

Example

char[] s = "abcd".dup;
dchar d1 = 'a';
dchar d2 = 'ø';

encode(s, d1);
writeln(s.length); // 5
writeln(s); // "abcda"
encode(s, d2);
writeln(s.length); // 7
writeln(s); // "abcdaø"

Authors

Walter Bright and Jonathan M Davis

License

Boost License 1.0.