Skip to content

Commit

Permalink
Fix issue 12890 - index based replace
Browse files Browse the repository at this point in the history
  • Loading branch information
Safety0ff committed Aug 15, 2014
1 parent 08137bf commit c4f1c43
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions std/array.d
Expand Up @@ -1919,13 +1919,6 @@ unittest
}
}

/+
Commented out until the replace which has been deprecated has been removed.
I'd love to just remove it in favor of replaceInPlace, but then code would then
use this version of replaceInPlace and silently break. So, it's here so that it
can be used once replace has not only been deprecated but removed, but
until then, it's commented out.
/++
Replaces elements from $(D array) with indices ranging from $(D from)
(inclusive) to $(D to) (exclusive) with the range $(D stuff). Returns a new
Expand Down Expand Up @@ -2019,8 +2012,29 @@ unittest
testStr!(dstring, string)();
testStr!(dstring, wstring)();
testStr!(dstring, dstring)();

enum s = "0123456789";
enum w = "⁰¹²³⁴⁵⁶⁷⁸⁹"w;
enum d = "⁰¹²³⁴⁵⁶⁷⁸⁹"d;

assert(replace(s, 0, 0, "***") == "***0123456789");
assert(replace(s, 10, 10, "***") == "0123456789***");
assert(replace(s, 3, 8, "1012") == "012101289");
assert(replace(s, 0, 5, "43210") == "4321056789");
assert(replace(s, 5, 10, "43210") == "0123443210");

assert(replace(w, 0, 0, "***"w) == "***⁰¹²³⁴⁵⁶⁷⁸⁹"w);
assert(replace(w, 10, 10, "***"w) == "⁰¹²³⁴⁵⁶⁷⁸⁹***"w);
assert(replace(w, 3, 8, "¹⁰¹²"w) == "⁰¹²¹⁰¹²⁸⁹"w);
assert(replace(w, 0, 5, "⁴³²¹⁰"w) == "⁴³²¹⁰⁵⁶⁷⁸⁹"w);
assert(replace(w, 5, 10, "⁴³²¹⁰"w) == "⁰¹²³⁴⁴³²¹⁰"w);

assert(replace(d, 0, 0, "***"d) == "***⁰¹²³⁴⁵⁶⁷⁸⁹"d);
assert(replace(d, 10, 10, "***"d) == "⁰¹²³⁴⁵⁶⁷⁸⁹***"d);
assert(replace(d, 3, 8, "¹⁰¹²"d) == "⁰¹²¹⁰¹²⁸⁹"d);
assert(replace(d, 0, 5, "⁴³²¹⁰"d) == "⁴³²¹⁰⁵⁶⁷⁸⁹"d);
assert(replace(d, 5, 10, "⁴³²¹⁰"d) == "⁰¹²³⁴⁴³²¹⁰"d);
}
+/

/++
Replaces elements from $(D array) with indices ranging from $(D from)
Expand Down Expand Up @@ -2070,15 +2084,7 @@ void replaceInPlace(T, Range)(ref T[] array, size_t from, size_t to, Range stuff
(is(T == const T) || is(T == immutable T))) ||
isSomeString!(T[]) && is(ElementType!Range : dchar)))
{
auto app = appender!(T[])();
app.put(array[0 .. from]);
app.put(stuff);
app.put(array[to .. $]);
array = app.data;

//This simplified version can be used once the old replace has been removed
//and the new one uncommented out.
//array = replace(array, from, to stuff);
array = replace(array, from, to, stuff);
}

//Verify Examples.
Expand Down

0 comments on commit c4f1c43

Please sign in to comment.