Comment #0 by bearophile_hugs — 2012-01-12T10:47:00Z
I suggest to add a reversed() function to std.string (the "-ed" suffix is a Python convention that means it's not an in-place function. It creates a copy of the input data, works on it, and returns it):
import std.stdio, std.algorithm, std.traits;
immutable(T)[] reversed(T)(immutable(T[]) s)
@safe pure /*nothrow*/ if (isSomeChar!T) {
auto sr = s.dup; // druntime BUG not nothrow
sr.reverse();
return sr; // Implicit immutable cast because it's pure.
}
unittest {
assert("red".reversed() == "der"); // UTF-8
assert("red"w.reversed() == "der"); // UTF-16
assert("red"d.reversed() == "der"); // UTF-32
}
It's not nothrow yet because someArray.dup is not nothrow yet.
Comment #1 by bearophile_hugs — 2012-01-12T10:53:09Z
This function is handy because the alternative is to use code like this, that is much longer (3 or 4 lines long), it's not a single expression, and it needs two copies of the original string (or a copy and a cast, or a copy and one assume unique):
import std.algorithm: reverse;
void main() {
string s = "red";
char[] s1 = s.dup;
s1.reverse();
string sr = s1.idup;
}
Comment #2 by issues.dlang — 2012-01-12T15:54:46Z
Your example is not the shortest way of doing this. The typical way would be
array(retro(str));
which _is_ an expression and almost as concise as
reversed(str);
I question that adding another function is worth it.
Comment #3 by andrej.mitrovich — 2012-01-12T16:06:50Z
(In reply to comment #2)
> Your example is not the shortest way of doing this. The typical way would be
>
> array(retro(str));
>
> which _is_ an expression and almost as concise as
>
You can't assign that back to a string:
string str = "foo";
str = array(retro(str)).idup; // error
I don't know why array insists on creating dchar[] instead of char[]? Shorter example:
char[] duped = array(str); // error: can't convert dchar[] to char[]
Comment #4 by issues.dlang — 2012-01-12T16:39:38Z
Ah, yes. I forgot about that. array generates a dchar[], because retro returns a range of dchar, not a string, and array returns an array with the same element type as the range passed to it. The correct way to do it then would be
to!string(retro(str))
Comment #5 by bearophile_hugs — 2012-01-12T18:23:10Z
(In reply to comment #4)
> to!string(retro(str))
Or better text(retro(str)) or wtext(retro(str)) or dtext(retro(str)).
Or even to!(typeof(str))(retro(str)) if you want to be generic.
I think this is a good enough solution, so I close this issue. Thank you Jonathan.