[Issue 5233] New: [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 00:15:04 PST 2010


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

           Summary: [patch] std.range.put accepts *any* element type when
                    putting to an array.
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: patch
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: sandford at jhu.edu


--- Comment #0 from Rob Jacques <sandford at jhu.edu> 2010-11-18 00:13:49 PST ---
Essentially, there is a lack of a static assert in some of the branches of the
put function, leading to any element being accepted for certain input ranges
(notably strings) which define a non-assignable 'front'. Unfortunately, ding
template constraints result in a recursive expansion with isOutputRange. I've
listed a patch below which adds the static asserts and special cases strings so
they work as expected. Oh, here's a simple unit test:

assert( !isOutputRange!(char[],real) );

Patch:

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)))) {
                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 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