The OpenD Programming Language

decodeFront

decodeFront is a variant of decode which specifically decodes the first code point. Unlike decode, decodeFront accepts any input range of code units (rather than just a string or random access range). It also takes the range by ref and pops off the elements as it decodes them. If numCodeUnits is passed in, it gets set to the number of code units which were in the code point which was decoded.

  1. dchar decodeFront(S str, size_t numCodeUnits)
    dchar
    decodeFront
    (
    UseReplacementDchar useReplacementDchar = No.useReplacementDchar
    S
    )
    (
    ref S str
    ,
    out size_t numCodeUnits
    )
    out (result) { assert (isValidDchar(result)); }
  2. dchar decodeFront(S str, size_t numCodeUnits)
  3. dchar decodeFront(S str)

Parameters

useReplacementDchar

if invalid UTF, return replacementDchar rather than throwing

str S

input string or indexable Range

numCodeUnits size_t

set to number of code units processed

Return Value

Type: dchar

decoded character

Throws

UTFException if str.front is not the start of a valid UTF sequence. If an exception is thrown, then there is no guarantee as to the number of code units which were popped off, as it depends on the type of range being used and how many code units had to be popped off before the code point was determined to be invalid.

Examples

import std.range.primitives;
string str = "Hello, World!";

assert(str.decodeFront == 'H' && str == "ello, World!");
str = "å";
assert(str.decodeFront == 'å' && str.empty);
str = "å";
size_t i;
assert(str.decodeFront(i) == 'å' && i == 2 && str.empty);

Meta