Thread overview
[Issue 7281] New: std.string.reversed
Jan 12, 2012
Jonathan M Davis
Jan 13, 2012
Andrej Mitrovic
Jan 13, 2012
Jonathan M Davis
January 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281

           Summary: std.string.reversed
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-01-12 10:47:00 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281



--- Comment #1 from bearophile_hugs@eml.cc 2012-01-12 10:53:09 PST ---
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;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-01-12 15:54:46 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-01-12 16:06:50 PST ---
(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[]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281



--- Comment #4 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-01-12 16:39:38 PST ---
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))

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #5 from bearophile_hugs@eml.cc 2012-01-12 18:23:10 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------