www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19642] New: std.range.slide!(No.withPartial) on lengthless

https://issues.dlang.org/show_bug.cgi?id=19642

          Issue ID: 19642
           Summary: std.range.slide!(No.withPartial) on lengthless forward
                    range: get empty when expecting one window
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: kirsybuu gmail.com

Failing example (tested in DMD64 D Compiler v2.084.0):

unittest {
    auto r1 = recurrence!((a,n) => a[n-1] - 1)(2).until(0);
    assert(r1.equal([2,1])); // succeeds
    auto r2 = r1.slide!(No.withPartial)(2);
    assert(! r2.empty); // fails, should be equal to [[2,1]]
}

The issue appears to be in the "Standard constructor" of the Forward Range
version of std.range.Slides when needsEndTracker = true. The nextSource field
is initialized using drop(stepSize) and later _empty is set to true if
nextSource.empty, which is incorrect behavior because if exactly n elements can
be dropped, then the range should contain one window of those n elements.

Suggested fix: do something similar to Slides.popFront() such as
1) change line 8430 of std.range.package.d
            nextSource = source.save.drop(windowSize);
to
            nextSource = source.save;
            auto poppedElems = nextSource.popFrontN(windowSize);
2) change line 8458
                if (nextSource.empty)
to
                if (poppedElements < windowSize)

--
Feb 03 2019