[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
Tue Mar 15 22:25:56 PDT 2011


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



--- Comment #3 from Rob Jacques <sandford at jhu.edu> 2011-03-15 22:22:43 PDT ---
Patch version 4. (DMD 2.052, Line 272+)
* Removed some debug code that was accidentally included in the last version
* Refactored and reformatted
* Fixed/added support for using character ranges as output ranges
  * Added an associated unittest
  * Requires the removal of unittest two static asserts:
    static assert(!__traits(compiles, put(a, 'a')));
    static assert(!__traits(compiles, put(a, "ABC")));
* put's other unit tests have been tested using DMD 2.052

// BUG template constraints result in a recursive expansion with isOutputRange
void put(R, E)(ref R r, E e) {
    // Patch from Issue 4880
    static if ( __traits(compiles, {return R.init.put(E.init);}) ) {
        // commit to using the "put" method
             static if (is(typeof(r.put(e))))          r.put(e);
        else static if (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( isSomeString!R && isSomeChar!E ) {
        static if( (typeof(r[0])).sizeof < E.sizeof ) {
            // Transcoding is required to support char[].put(dchar)
            Unqual!(typeof(r[0]))[(typeof(r[0])).sizeof == 1 ? 4 : 2]
                                                             encoded;
            auto len = std.utf.encode(encoded, e);
            assert(r.length >= len);
            foreach(c;encoded[0 .. len]) {
                r[0] = c;
                r = r[1..$];
            }
        } else {
            assert(!r.empty);
            if(r.length == 0) r.length = 1;
            r[0] = e;
            r = r[1..$];
        }
    } else static if (isInputRange!R && is(typeof(r.front = e) )) {
        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(hasSlicing!E && hasLength!E && is(typeof(put(r, e[0])))){
        for (; e.length > 0; e = e[1..e.length]) put(r, e[0]);
    } else static if (is(typeof(r(e)))) { // Commit using opCall
        r(e);
    } else static if (is(typeof(r((&e)[0..1])))) {
        r((&e)[0..1]);
    } else {
        static assert(false, "Can't put a "~E.stringof~" into a "~R.stringof);
    }
}

// char[] as an output range
unittest {
    auto a = "\u2666"d;
    auto b = "\u2666";
    auto s = new char[b.length];
    auto t = s;
    put(t,a);
    assert( s == b );
}

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