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.

Function std.utf.decodeBack

decodeBack is a variant of decode which specifically decodes the last code point. Unlike decode, decodeBack accepts any bidirectional 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.

dchar decodeBack(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar, S) (
  ref S str,
  out size_t numCodeUnits
)
if (isSomeString!S);

dchar decodeBack(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar, S) (
  ref S str,
  out size_t numCodeUnits
)
if (!isSomeString!S && isSomeChar!(ElementType!S) && isBidirectionalRange!S && (isRandomAccessRange!S && hasLength!S || !isRandomAccessRange!S));

dchar decodeBack(Flag!("useReplacementDchar") useReplacementDchar = No.useReplacementDchar, S) (
  ref S str
)
if (isSomeString!S || isRandomAccessRange!S && hasLength!S && isSomeChar!(ElementType!S) || !isRandomAccessRange!S && isBidirectionalRange!S && isSomeChar!(ElementType!S));

Parameters

NameDescription
useReplacementDchar if invalid UTF, return replacementDchar rather than throwing
str input string or bidirectional Range
numCodeUnits gives the number of code units processed

Returns

A decoded UTF character.

Throws

UTFException if str.back is not the end of a valid UTF sequence. If an exception is thrown, the str itself remains unchanged, but there is no guarantee as to the value of numCodeUnits (when passed).

Example

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

assert(str.decodeBack == '!' && str == "Hello, World");
str = "å";
assert(str.decodeBack == 'å' && str.empty);
str = "å";
size_t i;
assert(str.decodeBack(i) == 'å' && i == 2 && str.empty);

Authors

Walter Bright and Jonathan M Davis

License

Boost License 1.0.