@@ -12863,14 +12863,27 @@ if (
12863
12863
private :
12864
12864
R data;
12865
12865
E element;
12866
- size_t counter;
12867
- static if (isBidirectionalRange! R && hasLength! R) size_t backPosition;
12868
- size_t maxSize;
12866
+ static if (hasLength! R)
12867
+ {
12868
+ size_t padLength;
12869
+ }
12870
+ else
12871
+ {
12872
+ size_t minLength;
12873
+ size_t consumed;
12874
+ }
12869
12875
12870
12876
public :
12871
12877
bool empty () @property
12872
12878
{
12873
- return data.empty && counter >= maxSize;
12879
+ static if (hasLength! R)
12880
+ {
12881
+ return data.empty && padLength == 0 ;
12882
+ }
12883
+ else
12884
+ {
12885
+ return data.empty && consumed >= minLength;
12886
+ }
12874
12887
}
12875
12888
12876
12889
auto front () @property
@@ -12882,20 +12895,33 @@ if (
12882
12895
void popFront ()
12883
12896
{
12884
12897
assert (! empty, " Attempting to popFront an empty padRight" );
12885
- ++ counter;
12886
12898
12887
- if (! data.empty)
12899
+ static if (hasLength! R)
12900
+ {
12901
+ if (! data.empty)
12902
+ {
12903
+ data.popFront;
12904
+ }
12905
+ else
12906
+ {
12907
+ -- padLength;
12908
+ }
12909
+ }
12910
+ else
12888
12911
{
12889
- data.popFront;
12912
+ ++ consumed;
12913
+ if (! data.empty)
12914
+ {
12915
+ data.popFront;
12916
+ }
12890
12917
}
12891
12918
}
12892
12919
12893
12920
static if (hasLength! R)
12894
12921
{
12895
12922
size_t length () @property
12896
12923
{
12897
- import std.algorithm.comparison : max;
12898
- return max (data.length, maxSize);
12924
+ return data.length + padLength;
12899
12925
}
12900
12926
}
12901
12927
@@ -12914,16 +12940,15 @@ if (
12914
12940
auto back () @property
12915
12941
{
12916
12942
assert (! empty, " Attempting to fetch the back of an empty padRight" );
12917
- return backPosition > data.length ? element : data.back;
12943
+ return padLength > 0 ? element : data.back;
12918
12944
}
12919
12945
12920
12946
void popBack ()
12921
12947
{
12922
12948
assert (! empty, " Attempting to popBack an empty padRight" );
12923
- if (backPosition > data.length )
12949
+ if (padLength > 0 )
12924
12950
{
12925
- -- backPosition;
12926
- -- maxSize;
12951
+ -- padLength;
12927
12952
}
12928
12953
else
12929
12954
{
@@ -12937,8 +12962,7 @@ if (
12937
12962
E opIndex (size_t index)
12938
12963
{
12939
12964
assert (index <= this .length, " Index out of bounds" );
12940
- return (index > data.length && index <= maxSize) ? element :
12941
- data[index];
12965
+ return index >= data.length ? element : data[index];
12942
12966
}
12943
12967
}
12944
12968
@@ -12954,20 +12978,26 @@ if (
12954
12978
b <= length,
12955
12979
" Attempting to slice using an out of bounds index on a padRight"
12956
12980
);
12957
- return Result ((b <= data.length) ? data[a .. b] : data[a .. data.length],
12981
+ return Result (
12982
+ a >= data.length ? data[0 .. 0 ] : b <= data.length ? data[a .. b] : data[a .. data.length],
12958
12983
element, b - a);
12959
12984
}
12960
12985
12961
12986
alias opDollar = length;
12962
12987
}
12963
12988
12964
- this (R r, E e, size_t max )
12989
+ this (R r, E e, size_t n )
12965
12990
{
12966
12991
data = r;
12967
12992
element = e;
12968
- maxSize = max;
12969
- static if (isBidirectionalRange! R && hasLength! R)
12970
- backPosition = max;
12993
+ static if (hasLength! R)
12994
+ {
12995
+ padLength = n > data.length ? n - data.length : 0 ;
12996
+ }
12997
+ else
12998
+ {
12999
+ minLength = n;
13000
+ }
12971
13001
}
12972
13002
12973
13003
@disable this ();
@@ -13011,6 +13041,8 @@ pure @safe unittest
13011
13041
RangeType r3;
13012
13042
assert (r3.padRight(0 , 12 )[0 ] == 1 );
13013
13043
assert (r3.padRight(0 , 12 )[2 ] == 3 );
13044
+ assert (r3.padRight(0 , 12 )[9 ] == 10 );
13045
+ assert (r3.padRight(0 , 12 )[10 ] == 0 );
13014
13046
assert (r3.padRight(0 , 12 )[11 ] == 0 );
13015
13047
}
13016
13048
@@ -13022,6 +13054,14 @@ pure @safe unittest
13022
13054
.padRight(0 , 12 )[0 .. 3 ]
13023
13055
.equal([1 , 2 , 3 ])
13024
13056
);
13057
+ assert (r4
13058
+ .padRight(0 , 12 )[0 .. 10 ]
13059
+ .equal([1U , 2U , 3U , 4U , 5U , 6U , 7U , 8U , 9U , 10U ])
13060
+ );
13061
+ assert (r4
13062
+ .padRight(0 , 12 )[0 .. 11 ]
13063
+ .equal([1U , 2U , 3U , 4U , 5U , 6U , 7U , 8U , 9U , 10U , 0 ])
13064
+ );
13025
13065
assert (r4
13026
13066
.padRight(0 , 12 )[2 .. $]
13027
13067
.equal([3U , 4U , 5U , 6U , 7U , 8U , 9U , 10U , 0 , 0 ])
@@ -13031,6 +13071,10 @@ pure @safe unittest
13031
13071
.equal([1U , 2U , 3U , 4U , 5U , 6U , 7U , 8U , 9U , 10U , 0 , 0 ])
13032
13072
);
13033
13073
}
13074
+
13075
+ // drop & dropBack test opslice ranges when available, popFront/popBack otherwise
13076
+ RangeType r5;
13077
+ foreach (i; 1 .. 13 ) assert (r5.padRight(0 , 12 ).drop(i).walkLength == 12 - i);
13034
13078
}
13035
13079
}
13036
13080
@@ -13043,3 +13087,54 @@ pure @safe unittest
13043
13087
static immutable r2 = [1 , 2 , 3 , 4 , 0 , 0 ];
13044
13088
assert (r1.padRight(0 , 6 ).equal(r2));
13045
13089
}
13090
+
13091
+ // Test back, popBack, and save
13092
+ @safe pure unittest
13093
+ {
13094
+ import std.algorithm.comparison : equal;
13095
+
13096
+ auto r1 = [1 , 2 , 3 , 4 ].padRight(0 , 6 );
13097
+ assert (r1.back == 0 );
13098
+
13099
+ r1.popBack;
13100
+ auto r2 = r1.save;
13101
+ assert (r1.equal([1 , 2 , 3 , 4 , 0 ]));
13102
+ assert (r2.equal([1 , 2 , 3 , 4 , 0 ]));
13103
+
13104
+ r1.popBackN(2 );
13105
+ assert (r1.back == 3 );
13106
+ assert (r1.length == 3 );
13107
+ assert (r2.length == 5 );
13108
+ assert (r2.equal([1 , 2 , 3 , 4 , 0 ]));
13109
+
13110
+ r2.popFront;
13111
+ assert (r2.length == 4 );
13112
+ assert (r2[0 ] == 2 );
13113
+ assert (r2[1 ] == 3 );
13114
+ assert (r2[2 ] == 4 );
13115
+ assert (r2[3 ] == 0 );
13116
+ assert (r2.equal([2 , 3 , 4 , 0 ]));
13117
+
13118
+ r2.popBack;
13119
+ assert (r2.equal([2 , 3 , 4 ]));
13120
+
13121
+ auto r3 = [1 , 2 , 3 , 4 ].padRight(0 , 6 );
13122
+ size_t len = 0 ;
13123
+ while (! r3.empty)
13124
+ {
13125
+ ++ len;
13126
+ r3.popBack;
13127
+ }
13128
+ assert (len == 6 );
13129
+ }
13130
+
13131
+ // Issue 19042
13132
+ @safe pure unittest
13133
+ {
13134
+ import std.algorithm.comparison : equal;
13135
+
13136
+ assert ([2 , 5 , 13 ].padRight(42 , 10 ).chunks(5 )
13137
+ .equal! equal([[2 , 5 , 13 , 42 , 42 ], [42 , 42 , 42 , 42 , 42 ]]));
13138
+
13139
+ assert ([1 , 2 , 3 , 4 ].padRight(0 , 10 )[7 .. 9 ].equal([0 , 0 ]));
13140
+ }
0 commit comments