D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10087 - std.range.chunks problem with chunkSize = 0
Summary: std.range.chunks problem with chunkSize = 0
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-05-15 09:49 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-05-15 09:49:44 UTC
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 Peter Alexander 2014-02-11 13:33:00 UTC
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 basile-z 2015-12-07 03:18:37 UTC
> 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 ag0aep6g 2015-12-07 10:22:56 UTC
(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