Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

fix Issue 7944 - std.range.iota.popFront() cycles when the range is empty #545

Merged
merged 2 commits into from about 2 years ago

3 participants

Brad Anderson Steven Schveighoffer David Simcha
Brad Anderson
eco commented April 19, 2012

Added !empty assertions to front, popFront, back, and popBack to the integer iota like some other ranges do to catch popping an empty range. Also added the !empty assert to floating point iota front() which was the only one of those four that was missing.

http://d.puremagic.com/issues/show_bug.cgi?id=7944

Brad Anderson fix Issue 7944 - std.range.iota.popFront() cycles when the range is e…
…mpty

Added !empty assertions to front, popFront, back, and popBack to the integer
iota like some other ranges do. Also added the one that was missing to the
floating point iota.
028699f
Steven Schveighoffer
Collaborator

Should we add asserts in opIndex?

Brad Anderson
eco commented April 20, 2012

Makes sense. I'll take a look.

David Simcha dsimcha merged commit 9233d3d into from April 22, 2012
David Simcha dsimcha closed this April 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Apr 20, 2012
Brad Anderson fix Issue 7944 - std.range.iota.popFront() cycles when the range is e…
…mpty

Added !empty assertions to front, popFront, back, and popBack to the integer
iota like some other ranges do. Also added the one that was missing to the
floating point iota.
028699f
Brad Anderson Add index < length assert to iota.opIndex 23bfcd7
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 13 additions and 9 deletions. Show diff stats Hide diff stats

  1. 22  std/range.d
22  std/range.d
@@ -4352,15 +4352,17 @@ if ((isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
4352 4352
             }
4353 4353
         }
4354 4354
         @property bool empty() const { return current == pastLast; }
4355  
-        @property Value front() { return current; }
  4355
+        @property Value front() { assert(!empty); return current; }
4356 4356
         alias front moveFront;
4357  
-        void popFront() { current += step; }
4358  
-        @property Value back() { return pastLast - step; }
  4357
+        void popFront() { assert(!empty); current += step; }
  4358
+        @property Value back() { assert(!empty); return pastLast - step; }
4359 4359
         alias back moveBack;
4360  
-        void popBack() { pastLast -= step; }
  4360
+        void popBack() { assert(!empty); pastLast -= step; }
4361 4361
         @property auto save() { return this; }
4362 4362
         Value opIndex(ulong n)
4363 4363
         {
  4364
+            assert(n < this.length);
  4365
+
4364 4366
             // Just cast to Value here because doing so gives overflow behavior
4365 4367
             // consistent with calling popFront() n times.
4366 4368
             return cast(Value) (current + step * n);
@@ -4418,15 +4420,17 @@ if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
4418 4420
             }
4419 4421
         }
4420 4422
         @property bool empty() const { return current == pastLast; }
4421  
-        @property Value front() { return current; }
  4423
+        @property Value front() { assert(!empty); return current; }
4422 4424
         alias front moveFront;
4423  
-        void popFront() { ++current; }
4424  
-        @property Value back() { return pastLast - 1; }
  4425
+        void popFront() { assert(!empty); ++current; }
  4426
+        @property Value back() { assert(!empty); return pastLast - 1; }
4425 4427
         alias back moveBack;
4426  
-        void popBack() { --pastLast; }
  4428
+        void popBack() { assert(!empty); --pastLast; }
4427 4429
         @property auto save() { return this; }
4428 4430
         Value opIndex(ulong n)
4429 4431
         {
  4432
+            assert(n < this.length);
  4433
+
4430 4434
             // Just cast to Value here because doing so gives overflow behavior
4431 4435
             // consistent with calling popFront() n times.
4432 4436
             return cast(Value) (current + n);
@@ -4489,7 +4493,7 @@ if (isFloatingPoint!(CommonType!(B, E, S)))
4489 4493
             }
4490 4494
         }
4491 4495
         @property bool empty() const { return index == count; }
4492  
-        @property Value front() { return start + step * index; }
  4496
+        @property Value front() { assert(!empty); return start + step * index; }
4493 4497
         alias front moveFront;
4494 4498
         void popFront()
4495 4499
         {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.