Skip to content

Commit

Permalink
Add hasLength and isRandomAccessRange
Browse files Browse the repository at this point in the history
  • Loading branch information
andralex committed Dec 16, 2021
1 parent 564bc56 commit a94b148
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
12 changes: 3 additions & 9 deletions std/range/primitives.d
Expand Up @@ -1030,7 +1030,7 @@ See_Also:
*/
enum bool isBidirectionalRange(R) = isForwardRange!R
&& is(typeof((R r) => r.popBack))
&& is(ReturnType!((R r) => r.back) == ElementType!R);
&& is(ReturnType!((R r) => r.back) == ReturnType!((R r) => r.front));

///
@safe unittest
Expand Down Expand Up @@ -1561,14 +1561,8 @@ characters, but instead the number of encoding units, and as such is not useful
with range-oriented algorithms. To use strings as random-access ranges with
length, use $(REF representation, std, string) or $(REF byCodeUnit, std, utf).
*/
template hasLength(R)
{
static if (is(typeof(((R* r) => r.length)(null)) Length))
enum bool hasLength = is(Length == size_t) &&
!(isAutodecodableString!R && !isAggregateType!R);
else
enum bool hasLength = false;
}
enum bool hasLength(R) = is(typeof(((R* r) => r.length)(null)) == size_t)
&& (!isAutodecodableString!R || isAggregateType!R);

///
@safe unittest
Expand Down
43 changes: 41 additions & 2 deletions std2xalpha/range/primitives.d
Expand Up @@ -15,18 +15,21 @@ version (std_use_public_import)
else
{
import v1 = std.range.primitives;
// Unchanged in this version.
alias isInputRange = v1.isInputRange;
// TODO
alias put = v1.put;
// Unchanged in this version.
alias isOutputRange = v1.isOutputRange;
// Unchanged in this version.
alias isForwardRange = v1.isForwardRange;
// Unchanged in this version.
alias isBidirectionalRange = v1.isBidirectionalRange;
alias isRandomAccessRange = v1.isRandomAccessRange;
alias hasMobileElements = v1.hasMobileElements;
alias ElementEncodingType = v1.ElementEncodingType;
alias hasSwappableElements = v1.hasSwappableElements;
alias hasAssignableElements = v1.hasAssignableElements;
alias hasLvalueElements = v1.hasLvalueElements;
alias hasLength = v1.hasLength;
alias isInfinite = v1.isInfinite;
alias hasSlicing = v1.hasSlicing;
alias walkLength = v1.walkLength;
Expand Down Expand Up @@ -146,3 +149,39 @@ unittest
static assert(is(ElementType!(string) == immutable char));
static assert(is(ElementType!(dstring) == immutable(dchar)));
}

/**
@@@TODO@@@ This alias redefines `isRandomAccessRange` for std2x. The difference is of
course that the new `isRandomAccessRange` does not support autodecoding, so string
types are random access ranges.
*/
enum bool isRandomAccessRange(R) =
is(typeof(imported!"std2xalpha.traits".lvalueOf!R[1]) == ElementType!R)
&& isForwardRange!R
&& (isBidirectionalRange!R || isInfinite!R)
&& (hasLength!R || isInfinite!R)
&& (isInfinite!R || !is(typeof(imported!"std2xalpha.traits".lvalueOf!R[$ - 1]))
|| is(typeof(imported!"std2xalpha.traits".lvalueOf!R[$ - 1]) == ElementType!R))
;

///
@safe unittest
{
static assert(isForwardRange!string);
static assert(isBidirectionalRange!string);
static assert(isRandomAccessRange!string);
}

/**
@@@TODO@@@ This redefines `hasLength` for std2x. The difference is of
course that the new `hasLength` does not support autodecoding, so string
types do have length, as they should.
*/
enum bool hasLength(R) = is(typeof(((R* r) => r.length)(null)) == size_t);

///
@safe unittest
{
static assert(hasLength!string);
static assert(hasLength!wstring);
}

0 comments on commit a94b148

Please sign in to comment.