Bug 7086 – Specialized in-place reverse() for char[]/wchar[]

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-12-09T05:20:00Z
Last change time
2011-12-10T00:10:37Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-12-09T05:20:00Z
Reversing an array of chars/wchars is a common enough operation (mutable arrays often come from precedent operations that have built them, and sometimes in the end you will use assumeUnique on them). Currently std.algorithm.reverse() can't be used on char[]/wchar[]: import std.algorithm; void main() { dchar[] s1 = "hello"d.dup; s1.reverse(); // OK wchar[] s2 = "hello"w.dup; s2.reverse(); // error char[] s3 = "hello".dup; s3.reverse(); // error } I suggest to add a char[]/wchar[] specialization to std.algorithm.reverse() (or to add a std.string.reverse()), usable on those types too. Generally std.algorithms don't work on UTF8/UTF16 because of the variable length of its items, but for this specific algorithm I think this is not a problem because: 1) Reversing an array is an O(n) operation, and decoding UTF adds a constant overhead, so the computational complexity of reverse doesn't change. 2) I think that if you reverse a char[] or wchar[] the result will fit in the input array, using the same amount of bytes. Example (where each letter-index pair is a byte): A1_A2_B1_C1_C2_C3_C4_D1 => D1_C1_C2_C3_C4_B1_A1_A2. Notes: - For this specific problem using a cast to ubyte[] or ushort[] before calling the currently implemented reverse() is not acceptable, it gives too much wrong results on unicode text. This is why I am asking for something better. - This enhancement request is not related to grapheme-awareness. This is left to future improvements or other code. Some example code: http://stackoverflow.com/questions/199260/how-do-i-reverse-a-utf-8-string-in-place
Comment #1 by andrei — 2011-12-09T10:27:01Z
Comment #2 by bearophile_hugs — 2011-12-10T00:10:37Z
Thank you Andrei Alexandrescu