D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7281 - std.string.reversed
Summary: std.string.reversed
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-12 10:47 UTC by bearophile_hugs
Modified: 2012-01-12 18:23 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-01-12 10:47:00 UTC
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 bearophile_hugs 2012-01-12 10:53:09 UTC
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 Jonathan M Davis 2012-01-12 15:54:46 UTC
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 Andrej Mitrovic 2012-01-12 16:06:50 UTC
(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 Jonathan M Davis 2012-01-12 16:39:38 UTC
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 bearophile_hugs 2012-01-12 18:23:10 UTC
(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.