Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1547

Show
Ignore:
Timestamp:
05/23/10 18:09:19 (15 years ago)
Author:
SHOO
Message:

Fixed bugzilla 4228: std.array.replace contains 2 bugs

4th parameter allows null.
Approach of remove method was wrong.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docsrc/changelog.dd

    r1546 r1547  
    1010    $(LI std.traits: Added templates to get compile-time information about functions.) 
    1111    $(LI std.typecons: Added tie and AutoImplement.) 
    1212    ) 
    1313    $(BUGSFIXED 
    1414    $(LI $(BUGZILLA 2835): std.socket.TcpSocket doesn't actually connect) 
    1515    $(LI $(BUGZILLA 3200): std.xml doesn't follow spec for Tag.text) 
    1616    $(LI $(BUGZILLA 3873): std.range.repeat should have popBack defined) 
    1717    $(LI $(BUGZILLA 3880): std.regex functions with const/immutable Regex object) 
    1818    $(LI $(BUGZILLA 4109): writeln doesn't work with empty static array) 
    1919    $(LI $(BUGZILLA 4202): Changset 1517 doesn't compile) 
     20    $(LI $(BUGZILLA 4228): std.array.replace contains 2 bugs) 
    2021    ) 
    2122) 
    2223 
    2324<div id=version> 
    2425$(UL 
    2526    $(NEW 047) 
    2627    $(NEW 046) 
    2728    $(NEW 045) 
    2829    $(NEW 044) 
    2930    $(NEW 043) 
  • trunk/phobos/std/array.d

    r1501 r1547  
    549549//     int[] a = [1, 2, 3, 4, 5]; 
    550550//     erase(a, 2u); 
    551551//     assert(a == [1, 2, 4, 5]); 
    552552// } 
    553553 
    554554/** 
    555555Replaces elements from $(D array) with indices ranging from $(D from) 
    556556(inclusive) to $(D to) (exclusive) with the range $(D stuff). Expands 
    557557or shrinks the array as needed. 
    558558 */ 
    559 void replace(T, Range)(ref T[] array, size_t from, size_t to, 
    560         Range stuff
     559void replace(T, Range)(ref T[] array, size_t from, size_t to, Range stuff) 
     560    if (is(ElementType!Range == T)
    561561{ 
    562562    // container = container[0 .. from] ~ stuff ~ container[to .. $]; 
    563563    if (overlap(array, stuff)) 
    564564    { 
    565565        // use slower/conservative method 
    566566        array = array[0 .. from] ~ stuff ~ array[to .. $]; 
    567567    } 
    568568    else if (stuff.length <= to - from) 
    569569    { 
    570570        // replacement reduces length 
    571571        // BUG 2128 
    572572        //immutable stuffEnd = from + stuff.length; 
    573573        auto stuffEnd = from + stuff.length; 
    574574        array[from .. stuffEnd] = stuff; 
    575         remove(array, tuple(stuffEnd, to)); 
     575        array = remove(array, tuple(stuffEnd, to)); 
    576576    } 
    577577    else 
    578578    { 
    579579        // replacement increases length 
    580580        // @@@TODO@@@: optimize this 
    581581        immutable replaceLen = to - from; 
    582582        array[from .. to] = stuff[0 .. replaceLen]; 
    583583        insert(array, to, stuff[replaceLen .. $]); 
    584584    } 
    585585} 
    586586 
     587 
     588void replace(T, Range)(ref T[] array, size_t from, size_t to, Range stuff) 
     589    if (!is(ElementType!Range == T) && is(Unqual!Range == void*)) 
     590{ 
     591    replace(array, from, to, cast(T[])[]); 
     592} 
     593 
     594 
     595 
    587596unittest 
    588597{ 
    589598    int[] a = [1, 4, 5]; 
    590599    replace(a, 1u, 2u, [2, 3, 4]); 
    591600    assert(a == [1, 2, 3, 4, 5]); 
     601    replace(a, 1u, 2u, cast(int[])[]); 
     602    assert(a == [1, 3, 4, 5]); 
     603    replace(a, 1u, 2u, null); 
     604    assert(a == [1, 4, 5]); 
    592605} 
    593606 
    594607/** 
    595608Implements an output range that appends data to an array. This is 
    596609recommended over $(D a ~= data) because it is more efficient. 
    597610 
    598611Example: 
    599612---- 
    600613string arr; 
    601614auto app = appender(&arr);