[Issue 4483] foreach over string or wstring, where element type not specified, does not support unicode

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jan 21 18:30:36 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=4483


Martin Nowak <code at dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code at dawg.eu


--- Comment #5 from Martin Nowak <code at dawg.eu> 2014-01-21 18:30:31 PST ---
(In reply to comment #4)
> I'm personally particularly interested to fix the foreach over a unicode
> literal. I guess we could change the type of such a literal and, for example,
> pick a type that fits the characters in the literal.

Sounds pretty hacky.

Andrei posted a good alternative solution.
http://forum.dlang.org/post/j7soe4$2rvt$1@digitalmars.com

We should also try to use range forech (.front, .popFront, .empty) because
decoding is much faster than with the runtime implementation which uses a
delegate.

After a proper deprecation cycle this could work like so.

    // iterate over any unicode string
    foreach (c; "foobar") {}  //dchar
    foreach (c; "foobar"c) {} //dchar
    foreach (c; "foobar"w) {} //dchar
    foreach (c; "foobar"d) {} //dchar

    // iterate over representation
    foreach (c; "foobar".rep) {}  //ubyte
    foreach (c; "foobar"c.rep) {} //ubyte
    foreach (c; "foobar"w.rep) {} //ushort
    foreach (c; "foobar"d.rep) {} //uint

    // conversion becomes an error
    foreach (char c; "foobar"c) {}   //error can't convert dchar to char
    foreach (char c; "foobar"w) {}   //error can't convert dchar to char
    foreach (char c; "foobar"d) {}   //error can't convert dchar to char
    foreach (wchar wc; "foobar"c) {} //error can't convert dchar to wchar
    foreach (wchar wc; "foobar"w) {} //error can't convert dchar to wchar
    foreach (wchar wc; "foobar"d) {} //error can't convert dchar to wchar

    // std.utf.transcode for transcoding (lazy iteration)
    foreach (c; "foobar".transcode!char()) {}   //char
    foreach (wc; "foobar".transcode!wchar()) {} //wchar
    // and so on and so forth

    // further changes
    "foobar".length // use "foobar".rep.length
    "foobar"[0]     // use "foobar".rep[0]

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


More information about the Digitalmars-d-bugs mailing list