@@ -737,13 +737,21 @@ unittest
737737 test(theAllocator);
738738}
739739
740+ // / Ditto
741+ Unqual! (ElementEncodingType! R)[] makeArray (Allocator, R)(auto ref Allocator alloc, R range)
742+ if (isInputRange! R && ! isInfinite! R)
743+ {
744+ alias T = Unqual! (ElementEncodingType! R);
745+ return makeArray! (T, Allocator, R)(alloc, range);
746+ }
747+
740748// / Ditto
741749T[] makeArray (T, Allocator, R)(auto ref Allocator alloc, R range)
742750if (isInputRange! R && ! isInfinite! R)
743751{
744752 static if (isForwardRange! R || hasLength! R)
745753 {
746- static if (hasLength! R)
754+ static if (hasLength! R || isNarrowString ! R )
747755 immutable length = range.length;
748756 else
749757 immutable length = range.save.walkLength;
@@ -763,10 +771,18 @@ if (isInputRange!R && !isInfinite!R)
763771 alloc.deallocate(m);
764772 }
765773
766- for (; ! range.empty; range.popFront, ++ i)
774+ import std.conv : emplace;
775+ static if (isNarrowString! R || isRandomAccessRange! R)
767776 {
768- import std.conv : emplace;
769- cast (void ) emplace! T(result.ptr + i, range.front);
777+ foreach (j; 0 .. range.length)
778+ {
779+ cast (void ) emplace! T(result.ptr + i++ , range[j]);
780+ }
781+ }
782+ else
783+ {
784+ for (; ! range.empty; range.popFront, ++ i)
785+ cast (void ) emplace! T(result.ptr + i, range.front);
770786 }
771787
772788 return result;
@@ -826,12 +842,40 @@ unittest
826842 a = alloc.makeArray! long ([5 , 42 ]);
827843 assert (a.length == 2 );
828844 assert (a == [ 5 , 42 ]);
845+
846+ // we can also infer the type
847+ auto b = alloc.makeArray([4.0 , 2.0 ]);
848+ static assert (is (typeof (b) == double []));
849+ assert (b == [4.0 , 2.0 ]);
829850 }
830851 import std.experimental.allocator.gc_allocator : GCAllocator;
831852 test(GCAllocator.instance);
832853 test(theAllocator);
833854}
834855
856+ // infer types for strings
857+ unittest
858+ {
859+ void test (A)(auto ref A alloc)
860+ {
861+ auto c = alloc.makeArray(" fooπ😜" );
862+ static assert (is (typeof (c) == char []));
863+ assert (c == " fooπ😜" );
864+
865+ auto d = alloc.makeArray(" fooπ😜" d);
866+ static assert (is (typeof (d) == dchar []));
867+ assert (d == " fooπ😜" );
868+
869+ auto w = alloc.makeArray(" fooπ😜" w);
870+ static assert (is (typeof (w) == wchar []));
871+ assert (w == " fooπ😜" );
872+ }
873+
874+ import std.experimental.allocator.gc_allocator : GCAllocator;
875+ test(GCAllocator.instance);
876+ test(theAllocator);
877+ }
878+
835879version (unittest )
836880{
837881 private struct ForcedInputRange
0 commit comments