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

--- Comment #0 from 2011-12-09 05:20:00 PST ---
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.

- 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:

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to