retro() on a `string` creates a range of `dchar`, causing array() pains
Ali Çehreli
acehreli at yahoo.com
Tue Apr 17 09:42:19 PDT 2012
On 04/17/2012 09:12 AM, Timon Gehr wrote:
> On 04/17/2012 06:09 PM, Ali Çehreli wrote:
>> The algorithm must be building a local string.
> It does not have to build a local string, see
> http://dlang.org/phobos/std_utf.html#strideBack
I never said otherwise. :p
I was too lazy to locate where 2.059's algorithm.d was placed under.
Apparently it is here:
/usr/include/x86_64-linux-gnu/dmd/phobos/std/algorithm.d
The algorithm is smart. It reverses individual Unicode characters
in-place first and then reverses the whole string one last time:
void reverse(Char)(Char[] s)
if (isNarrowString!(Char[]) && !is(Char == const) && !is(Char == immutable))
{
auto r = representation(s);
for (size_t i = 0; i < s.length; )
{
immutable step = std.utf.stride(s, i);
if (step > 1)
{
.reverse(r[i .. i + step]);
i += step;
}
else
{
++i;
}
}
reverse(r);
}
Ali
P.S. Being a C++ programmer, exception-safety is always warm in my mind.
Unfortunately the topic does not come up much in D forums. The algorithm
above is not exception-safe because stride() may throw. But this way off
topic on this thread. :)
More information about the Digitalmars-d-learn
mailing list