D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12877 - std.random.uniform cannot handle dchar variates
Summary: std.random.uniform cannot handle dchar variates
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-08 16:40 UTC by Joseph Rushton Wakeling
Modified: 2014-06-10 12:14 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Joseph Rushton Wakeling 2014-06-08 16:40:43 UTC
The uniform!T specialization, which returns a random variate covering the entire bit range of an integral or character type T, will fail in the event that T == dchar because dchar.max is significantly less than the largest number that can be stored in dchar's 32 bits.

This in turn leads uniform!"[]"(dchar.min, dchar.max) to fail, because the integral/character-type uniform() calls uniform!ReturnType in the event of a closed interval with bounds [ReturnType.min, ReturnType.max].

See forum discussion for further details, including code that illustrates the bug: http://forum.dlang.org/thread/mailman.1906.1402217668.2907.digitalmars-d-learn@puremagic.com
Comment 1 Joseph Rushton Wakeling 2014-06-08 17:06:13 UTC
Fix submitted: https://github.com/D-Programming-Language/phobos/pull/2235
Comment 2 github-bugzilla 2014-06-10 12:14:40 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/cb5ee35ad69024293d5d557151e5939065fa0a06
Fix Issue #12877: allow uniform() to handle dchar variates

Since [dchar.min, dchar.max] does not cover the full bit range of dchar,
optimizations that work for other integral types will fail here, and
result in illegal unicode points being generated.

This fix avoids breaking any existing calls to uniform() via a twofold
approach:

   * uniform!"[]"(T.min, T.max) will only call uniform!ResultType if
     T is not a dchar;

   * uniform!dchar will not try and use the entire bit range but will
     instead call uniform!"[]"(dchar.min, dchar.max).

Unittests have been added that should prevent such issues from arising
again.

https://github.com/D-Programming-Language/phobos/commit/240a11ebe31ca9980d951553c2dac3fd62070b56
Merge pull request #2235 from WebDrake/uniform-dchar

Fix Issue #12877: allow uniform() to handle dchar variates