www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7086] New: Specialized in-place reverse() for char[]/wchar[]

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7086

           Summary: Specialized in-place reverse() for char[]/wchar[]
           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



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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7086


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrei metalanguage.com
         Resolution|                            |FIXED



10:27:01 PST ---
https://github.com/D-Programming-Language/phobos/pull/359

Very fun exercise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7086




Thank you Andrei Alexandrescu

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