Added 'enforce' checks on ranges' emptiness.

This commit is contained in:
Michail Pevnev 2017-08-01 18:31:34 +03:00
parent e313fa1fbf
commit 6a381528a3

View file

@ -1068,9 +1068,16 @@ struct Node
/* Input range functionality. */ /* Input range functionality. */
bool empty() @property { return position >= subnodes.length; } bool empty() @property { return position >= subnodes.length; }
void popFront() { position++; }
void popFront()
{
enforce(!empty, "Attempted to popFront an empty sequence");
position++;
}
T front() @property T front() @property
{ {
enforce(!empty, "Attempted to take the front of an empty sequence");
static if (is(Unqual!T == Node)) static if (is(Unqual!T == Node))
return subnodes[position]; return subnodes[position];
else else
@ -1081,9 +1088,15 @@ struct Node
Range save() { return this; } Range save() { return this; }
/* Bidirectional range functionality. */ /* Bidirectional range functionality. */
void popBack() { subnodes = subnodes[0 .. $ - 1]; } void popBack()
{
enforce(!empty, "Attempted to popBack an empty sequence");
subnodes = subnodes[0 .. $ - 1];
}
T back() T back()
{ {
enforce(!empty, "Attempted to take the back of an empty sequence");
static if (is(Unqual!T == Node)) static if (is(Unqual!T == Node))
return subnodes[$ - 1]; return subnodes[$ - 1];
else else
@ -1144,15 +1157,34 @@ struct Node
/* Input range functionality. */ /* Input range functionality. */
bool empty() { return position >= pairs.length; } bool empty() { return position >= pairs.length; }
void popFront() { position++; }
Pair front() { return pairs[position]; } void popFront()
{
enforce(!empty, "Attempted to popFront an empty mapping");
position++;
}
Pair front()
{
enforce(!empty, "Attempted to take the front of an empty mapping");
return pairs[position];
}
/* Forward range functionality. */ /* Forward range functionality. */
Range save() { return this; } Range save() { return this; }
/* Bidirectional range functionality. */ /* Bidirectional range functionality. */
void popBack() { pairs = pairs[0 .. $ - 1]; } void popBack()
Pair back() { return pairs[$ - 1]; } {
enforce(!empty, "Attempted to popBack an empty mapping");
pairs = pairs[0 .. $ - 1];
}
Pair back()
{
enforce(!empty, "Attempted to take the back of an empty mapping");
return pairs[$ - 1];
}
/* Random-access range functionality. */ /* Random-access range functionality. */
size_t length() const @property { return pairs.length; } size_t length() const @property { return pairs.length; }
@ -1244,7 +1276,6 @@ struct Node
assert(m1.mappingValues.equal([2, 3])); assert(m1.mappingValues.equal([2, 3]));
} }
/** Foreach over a sequence, getting each element as T. /** Foreach over a sequence, getting each element as T.
* *
* If T is Node, simply iterate over the nodes in the sequence. * If T is Node, simply iterate over the nodes in the sequence.