[Issue 5233] [patch] std.range.put accepts *any* element type when putting to an array.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 18 12:04:10 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=5233



--- Comment #1 from Rob Jacques <sandford at jhu.edu> 2010-11-18 12:02:54 PST ---
An updated patch allowing char[]/wchar[] to be put into ubyte/ushort output
ranges.



void put(R, E)(ref R r, E e) { // BUG, adding template constraints result in a
recursive expansion with isOutputRange
    static if ( __traits(compiles, {return R.init.put(E.init);}) ) { // Patch
from Issue 4880
        // commit to using the "put" method
        static if (!isArray!R && is(typeof(r.put(e)))) {
            r.put(e);
        } else static if (!isArray!R && is(typeof(r.put((&e)[0..1])))) {
            r.put((&e)[0..1]);
        } else {
            static assert(false, "Cannot put a "~E.stringof~" into a
"~R.stringof);
        }
    } else {
        static if (isInputRange!R) {
            // Commit to using assignment to front
            static if (is(typeof(r.front = e, r.popFront()))) {
                r.front = e;
                r.popFront();
            } else static if (isInputRange!E && is(typeof(put(r, e.front)))) {
                pragma(msg,R,"\t",E,"  =====");
                for (; !e.empty; e.popFront()) put(r, e.front);
            } else static if( isSomeString!R && isSomeChar!E ) {
                static if( (typeof(r[0])).sizeof < E.sizeof ) {
                    // must do some transcoding around here to support
char[].put(dchar)
                    Unqual!(typeof(r[0]))[(typeof(r[0])).sizeof == 1 ? 4 : 2]
encoded;
                    auto len = std.utf.encode(encoded, e);
                    writeln( typeof(encoded).stringof,"\t", typeof(r[0]).sizeof
,"\t", E.sizeof  );
                    foreach(c;encoded[0 .. len]) {
                        r[0] = c;
                        r.popFront;
                    }
                } else {
                    r[0] = e;
                    r.popFront;
                }
            } else static if(hasSlicing!E && is(typeof(e.length == size_t)) &&
is(typeof(put(r, e[0]))) ) {
                for (; !e.empty; e = e[1..e.length]) put(r, e[0]);
            } else {
                static assert(false, "Cannot put a "~E.stringof~" into a
"~R.stringof);
            }
        } else {
            // Commit to using opCall
            static if (is(typeof(r(e)))) {
                r(e);
            } else static if (is(typeof(r((&e)[0..1])))) {
                r((&e)[0..1]);
            } else {
                static assert(false, "Cannot put a "~E.stringof~" into a
"~R.stringof);
            }
        }
    }
}

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


More information about the Digitalmars-d-bugs mailing list