digitalmars.D.bugs - [Issue 7281] New: std.string.reversed
- d-bugmail puremagic.com (32/32) Jan 12 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7281
- d-bugmail puremagic.com (16/16) Jan 12 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7281
- d-bugmail puremagic.com (14/14) Jan 12 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7281
- d-bugmail puremagic.com (16/22) Jan 12 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7281
- d-bugmail puremagic.com (9/9) Jan 12 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7281
- d-bugmail puremagic.com (15/16) Jan 12 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7281
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 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: -------
Jan 12 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281 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: -------
Jan 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 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: -------
Jan 12 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 16:06:50 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 asYou 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: -------
Jan 12 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281 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: -------
Jan 12 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7281 bearophile_hugs eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |WONTFIXto!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: -------
Jan 12 2012