View source code Display the source code in std/utf.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

std.utf.code_length - multiple declarations

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.

Prototype

size_t codeLength(C, InputRange)(
  InputRange input
)
if (isInputRange!InputRange && is(ElementType!InputRange : dchar));

Example

import std.conv : to;
assert(codeLength!char("hello world") ==
       to!string("hello world").length);
assert(codeLength!wchar("hello world") ==
       to!wstring("hello world").length);
assert(codeLength!dchar("hello world") ==
       to!dstring("hello world").length);

assert(codeLength!char(プログラミング) ==
       to!string(プログラミング).length);
assert(codeLength!wchar(プログラミング) ==
       to!wstring(プログラミング).length);
assert(codeLength!dchar(プログラミング) ==
       to!dstring(プログラミング).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.);

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.

Prototype

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

Example

assert(codeLength!char('a') == 1);
assert(codeLength!wchar('a') == 1);
assert(codeLength!dchar('a') == 1);

assert(codeLength!char('\U0010FFFF') == 4);
assert(codeLength!wchar('\U0010FFFF') == 2);
assert(codeLength!dchar('\U0010FFFF') == 1);

Authors

Walter Bright and Jonathan M Davis

License

Boost License 1.0.

Comments