byGrapheme and retro with byCodePoint and text

Reverse String
Reverse String in D
module reverse_string;

import std.array : array;
import std.conv : text;
import std.range : retro;
import std.uni : byCodePoint, byGrapheme;

@safe pure string reverseString(string phrase)
{
    return phrase.byGrapheme.array.retro.byCodePoint.text;
}

This approach starts by importing from libraries for what is needed.

The reverseString function is marked @safe to ensure the compiler disallows certain unsafe practices in the function implementation. It is also marked as pure to ensure it does not modify any state external to itself.

Uniform Function Call Syntax is used to call a chain of functions, starting with the byGrapheme function called on the text input. byGraphemes iterates the characters in the input string by Graphemes, so that a character and its diacritic will remain associated together.

The graphemes are passed into the array function so they can be reversed by the retro function.

The retro function reverses the array of graphemes and passes them into the byCodePoint function.

The byCodePoints function transforms the graphemes back into code points, which are the original characters.

The code points are converted back into a string by the text function.

Finally, the function returns the reversed graphemes as a string.

If this test is added

// // Unicode diacritic
assert(reverseString("Amélie") == "eilémA");

It can be seen that if the graphemes are not reversed, the accent diacritic willl not be over the e, but over the l.

eiĺemA

However, by reversing the grapehemes, the accent diacritic remains over the e.

eilémA
13th Nov 2024 · Found it useful?