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.codeLength - multiple declarations

Function codeLength

Returns the number of code units that are required to encode the code point c when C is the character type used to encode it.

ubyte codeLength(C) (
  dchar c
) pure nothrow @nogc @safe
if (isSomeChar!C);

Example

writeln(codeLength!char('a')); // 1
writeln(codeLength!wchar('a')); // 1
writeln(codeLength!dchar('a')); // 1

writeln(codeLength!char('\U0010FFFF')); // 4
writeln(codeLength!wchar('\U0010FFFF')); // 2
writeln(codeLength!dchar('\U0010FFFF')); // 1

Function codeLength

Returns the number of code units that are required to encode str in a string whose character type is C. This is particularly useful when slicing one string with the length of another and the two string types use different character types.

size_t codeLength(C, InputRange) (
  InputRange input
)
if (isInputRange!InputRange && !isInfinite!InputRange && isSomeChar!(ElementType!InputRange));

Parameters

NameDescription
C the character type to get the encoding length for
input the input range to calculate the encoding length from

Returns

The number of code units in input when encoded to C

Example

assert(codeLength!char("hello world") ==
       "hello world".length);
assert(codeLength!wchar("hello world") ==
       "hello world"w.length);
assert(codeLength!dchar("hello world") ==
       "hello world"d.length);

assert(codeLength!char(`プログラミング`) ==
       `プログラミング`.length);
assert(codeLength!wchar(`プログラミング`) ==
       `プログラミング`w.length);
assert(codeLength!dchar(`プログラミング`) ==
       `プログラミング`d.length);

string haystack = `Être sans la verité, ça, ce ne serait pas bien.`;
wstring needle = `Être sans la verité`;
assert(haystack[codeLength!char(needle) .. $] ==
       `, ça, ce ne serait pas bien.`);

Authors

Walter Bright and Jonathan M Davis

License

Boost License 1.0.