Bug 10087 – std.range.chunks problem with chunkSize = 0
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2013-05-15T09:49:44Z
Last change time
2020-03-21T03:56:37Z
Assigned to
No Owner
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2013-05-15T09:49:44Z
import std.stdio: writeln;
import std.range: chunks;
void main() {
chunks("abcd"d, 0).writeln;
}
Prints an unbound range of empty strings, I think this is not good:
["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" ...
I suggest to refuse the chunkSize = 0 with a pre-condition (or worse with an enforce).
An alternative solution is to make 0 a special case, and return the whole iterable:
std.range.chunks("abcd"d, 0).writeln;
==>
["abcd"]
Comment #1 by peter.alexander.au — 2014-02-11T13:33:00Z
Hmm, not sure what the benefit of rejecting chunks(r, 0) is.
Sure, it's pointless to take a range in chunks of 0, but it's well-defined.
My worry is that someone is using chunks in some generic code somewhere that genuinely asks for chunkSize of 0 in some cases. Implementing your suggestion will break their code for no benefit.
Comment #2 by b2.temp — 2015-12-07T03:18:37Z
> I suggest to refuse the chunkSize = 0 with a pre-condition (or worse with an
> enforce).
You have an assertion for this in Chunk ctor, so you can detect the problem when testing.
Comment #3 by ag0aep6g — 2015-12-07T10:22:56Z
(In reply to bb.temp from comment #2)
> You have an assertion for this in Chunk ctor, so you can detect the problem
> when testing.
The assert needs to be documented, though:
https://github.com/D-Programming-Language/phobos/pull/3858