From 6a381528a33dac75d615d26aa8bb6f12ac75c368 Mon Sep 17 00:00:00 2001 From: Michail Pevnev Date: Tue, 1 Aug 2017 18:31:34 +0300 Subject: [PATCH] Added 'enforce' checks on ranges' emptiness. --- source/dyaml/node.d | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/source/dyaml/node.d b/source/dyaml/node.d index cbae8c6..368cfe9 100644 --- a/source/dyaml/node.d +++ b/source/dyaml/node.d @@ -1068,9 +1068,16 @@ struct Node /* Input range functionality. */ bool empty() @property { return position >= subnodes.length; } - void popFront() { position++; } + + void popFront() + { + enforce(!empty, "Attempted to popFront an empty sequence"); + position++; + } + T front() @property { + enforce(!empty, "Attempted to take the front of an empty sequence"); static if (is(Unqual!T == Node)) return subnodes[position]; else @@ -1081,9 +1088,15 @@ struct Node Range save() { return this; } /* 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() { + enforce(!empty, "Attempted to take the back of an empty sequence"); static if (is(Unqual!T == Node)) return subnodes[$ - 1]; else @@ -1144,15 +1157,34 @@ struct Node /* Input range functionality. */ 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. */ Range save() { return this; } /* Bidirectional range functionality. */ - void popBack() { pairs = pairs[0 .. $ - 1]; } - Pair back() { return pairs[$ - 1]; } + void popBack() + { + 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. */ size_t length() const @property { return pairs.length; } @@ -1244,7 +1276,6 @@ struct Node assert(m1.mappingValues.equal([2, 3])); } - /** Foreach over a sequence, getting each element as T. * * If T is Node, simply iterate over the nodes in the sequence.