Skip to content

Commit

Permalink
Merge pull request #4932 from dlang/stable
Browse files Browse the repository at this point in the history
merge stable into master
  • Loading branch information
9il authored Dec 7, 2016
2 parents 9eb35e1 + 68cf6e8 commit 9046849
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 91 deletions.
2 changes: 1 addition & 1 deletion std/algorithm/setops.d
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ Returns:
A range containing the unique union of the given ranges.
See_Also:
$(XREF algorithm, sorting, merge)
$(REF merge, std,algorithm,sorting)
*/
auto setUnion(alias less = "a < b", Rs...)
(Rs rs)
Expand Down
11 changes: 4 additions & 7 deletions std/algorithm/sorting.d
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ Checks whether a forward range is sorted according to the comparison
operation $(D less). Performs $(BIGOH r.length) evaluations of $(D
less).
Unlike $(LREF isSorted), $(LREF isStrictlyMonotonic) does not allow for equal values,
Unlike `isSorted`, `isStrictlyMonotonic` does not allow for equal values,
i.e. values for which both `less(a, b)` and `less(b, a)` are false.
With either function, the predicate must be a strict ordering just like with
$(LREF isSorted). For example, using `"a <= b"` instead of `"a < b"` is
`isSorted`. For example, using `"a <= b"` instead of `"a < b"` is
incorrect and will cause failed assertions.
Params:
less = Predicate the range should be sorted by.
r = Forward range to check for sortedness.
Returns:
`true` if the range is sorted, false otherwise. $(LREF isSorted) allows
duplicates, $(LREF isStrictlyMonotonic) not.
`true` if the range is sorted, false otherwise. `isSorted` allows
duplicates, `isStrictlyMonotonic` not.
*/
bool isSorted(alias less = "a < b", Range)(Range r) if (isForwardRange!(Range))
{
Expand Down Expand Up @@ -1061,9 +1061,6 @@ Params:
Returns:
A range containing the union of the given ranges.
See_Also:
$(XREF algorithm, setops, SetUnion)
*/
struct Merge(alias less = "a < b", Rs...) if (allSatisfy!(isInputRange, Rs))
{
Expand Down
61 changes: 34 additions & 27 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -5541,42 +5541,49 @@ template castFrom(From)

return cast(To) value;
}
}

///
@safe unittest
///
@system unittest
{
// Regular cast, which has been verified to be legal by the programmer:
{
// Regular cast, which has been verified to be legal by the programmer:
{
long x;
auto y = cast(int) x;
}
long x;
auto y = cast(int) x;
}

// However this will still compile if 'x' is changed to be a pointer:
{
long* x;
auto y = cast(int) x;
}
// However this will still compile if 'x' is changed to be a pointer:
{
long* x;
auto y = cast(int) x;
}

// castFrom provides a more reliable alternative to casting:
{
long x;
auto y = castFrom!long.to!int(x);
}
// castFrom provides a more reliable alternative to casting:
{
long x;
auto y = castFrom!long.to!int(x);
}

// Changing the type of 'x' will now issue a compiler error,
// allowing bad casts to be caught before it's too late:
{
long* x;
static assert (
!__traits(compiles, castFrom!long.to!int(x))
);
// Changing the type of 'x' will now issue a compiler error,
// allowing bad casts to be caught before it's too late:
{
long* x;
static assert (
!__traits(compiles, castFrom!long.to!int(x))
);

// if cast is still needed, must be changed to:
auto y = castFrom!(long*).to!int(x);
}
// if cast is still needed, must be changed to:
auto y = castFrom!(long*).to!int(x);
}
}

// https://issues.dlang.org/show_bug.cgi?id=16667
unittest
{
ubyte[] a = ['a', 'b', 'c'];
assert(castFrom!(ubyte[]).to!(string)(a) == "abc");
}

/**
Check the correctness of a string for $(D hexString).
The result is true if and only if the input string is composed of whitespace
Expand Down
9 changes: 8 additions & 1 deletion std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -6479,6 +6479,13 @@ immutable(Char)[] format(Char, Args...)(in Char[] fmt, Args args) if (isSomeChar
assert(is(typeof(format("happy"d)) == dstring));
}

// https://issues.dlang.org/show_bug.cgi?id=16661
@safe unittest
{
assert(format("%.2f"d, 0.4) == "0.40");
assert("%02d"d.format(1) == "01"d);
}

/*****************************************************
* Format arguments into buffer $(I buf) which must be large
* enough to hold the result. Throws RangeError if it is not.
Expand Down Expand Up @@ -6570,7 +6577,7 @@ char[] sformat(Char, Args...)(char[] buf, in Char[] fmt, Args args)
* the difference between the starts of the arrays
*/
@trusted private pure nothrow @nogc
ptrdiff_t arrayPtrDiff(const void[] array1, const void[] array2)
ptrdiff_t arrayPtrDiff(T)(const T[] array1, const T[] array2)
{
return array1.ptr - array2.ptr;
}
46 changes: 24 additions & 22 deletions std/parallelism.d
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ Warning: Unless marked as $(D @trusted) or $(D @safe), artifacts in
this module allow implicit data sharing between threads and cannot
guarantee that client code is free from low level data races.
Synopsis:
Source: $(PHOBOSSRC std/_parallelism.d)
Author: David Simcha
Copyright: Copyright (c) 2009-2011, David Simcha.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0)
*/
module std.parallelism;

---
import std.algorithm, std.parallelism, std.range;
///
unittest
{
import std.algorithm : map;
import std.range : iota;
import std.math : approxEqual;
import std.parallelism : taskPool;

void main() {
// Parallel reduce can be combined with
// std.algorithm.map to interesting effect.
// The following example (thanks to Russel Winder)
Expand All @@ -47,32 +56,25 @@ void main() {
// getTerm is evaluated in parallel as needed by
// TaskPool.reduce.
//
// Timings on an Athlon 64 X2 dual core machine:
// Timings on an Intel i5-3450 quad core machine
// for n = 1_000_000_000:
//
// TaskPool.reduce: 12.170 s
// std.algorithm.reduce: 24.065 s
// TaskPool.reduce: 1.067 s
// std.algorithm.reduce: 4.011 s

immutable n = 1_000_000_000;
immutable delta = 1.0 / n;
enum n = 1_000_000;
enum delta = 1.0 / n;

real getTerm(int i)
alias getTerm = (int i)
{
immutable x = ( i - 0.5 ) * delta;
return delta / ( 1.0 + x * x ) ;
}
};

immutable pi = 4.0 * taskPool.reduce!"a + b"(
std.algorithm.map!getTerm(iota(n))
);
}
---
immutable pi = 4.0 * taskPool.reduce!"a + b"(n.iota.map!getTerm);

Source: $(PHOBOSSRC std/_parallelism.d)
Author: David Simcha
Copyright: Copyright (c) 2009-2011, David Simcha.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0)
*/
module std.parallelism;
assert(pi.approxEqual(3.1415926));
}

import core.atomic;
import core.exception;
Expand Down
28 changes: 11 additions & 17 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ else version (Posix)
else
static assert(0);

private:
version(Windows)
{
// core.stdc.stdio.fopen expects file names to be
Expand Down Expand Up @@ -247,7 +246,6 @@ version(HAS_GETDELIM) extern(C) nothrow @nogc
ptrdiff_t getline(char**, size_t*, FILE*);
}


//------------------------------------------------------------------------------
struct ByRecord(Fields...)
{
Expand Down Expand Up @@ -312,7 +310,6 @@ template byRecord(Fields...)
}
}

public:
/**
Encapsulates a $(D FILE*). Generally D does not attempt to provide
thin wrappers over equivalent functions in the C standard library, but
Expand Down Expand Up @@ -650,7 +647,6 @@ Throws: $(D ErrnoException) in case of error.
version(StdDdoc)
void windowsHandleOpen(HANDLE handle, in char[] stdioOpenmode);

/// ditto
version(Windows)
void windowsHandleOpen(HANDLE handle, in char[] stdioOpenmode)
{
Expand Down Expand Up @@ -1863,7 +1859,6 @@ Returns the underlying operating system $(D HANDLE) (Windows only).
version(StdDdoc)
@property HANDLE windowsHandle();

/// ditto
version(Windows)
@property HANDLE windowsHandle()
{
Expand All @@ -1880,7 +1875,7 @@ Range that reads one line at a time. Returned by $(LREF byLine).
Allows to directly use range operations on lines of a file.
*/
private struct ByLine(Char, Terminator)
struct ByLine(Char, Terminator)
{
private:
import std.typecons : RefCounted, RefCountedAutoInitialize;
Expand Down Expand Up @@ -2401,7 +2396,7 @@ $(REF readText, std,file)
/*
* Range that reads a chunk at a time.
*/
private struct ByChunk
struct ByChunk
{
private:
File file_;
Expand Down Expand Up @@ -2603,7 +2598,7 @@ $(D StdioException).
/*
$(D Range) that locks the file and allows fast writing to it.
*/
private struct LockingTextWriter
struct LockingTextWriter
{
private:
import std.range.primitives : ElementType, isInfinite, isInputRange;
Expand Down Expand Up @@ -2780,7 +2775,7 @@ See $(LREF byChunk) for an example.
// An output range which optionally locks the file and puts it into
// binary mode (similar to rawWrite). Because it needs to restore
// the file mode on destruction, it is RefCounted on Windows.
private struct BinaryWriterImpl(bool locking)
struct BinaryWriterImpl(bool locking)
{
import std.traits : hasIndirections;
private:
Expand Down Expand Up @@ -3195,7 +3190,7 @@ enum LockType
readWrite
}

private struct LockingTextReader
struct LockingTextReader
{
private File _f;
private char _front;
Expand Down Expand Up @@ -3915,7 +3910,6 @@ struct lines
this.terminator = terminator;
}

/// Implements `opApply` `foreach` support
int opApply(D)(scope D dg)
{
import std.traits : Parameters;
Expand Down Expand Up @@ -3959,8 +3953,8 @@ struct lines
return opApplyRaw(dg);
}
}

private int opApplyRaw(D)(scope D dg)
// no UTF checking
int opApplyRaw(D)(scope D dg)
{
import std.conv : to;
import std.exception : assumeUnique;
Expand Down Expand Up @@ -4318,7 +4312,7 @@ Initialize with a message and an error code.
}
}

package extern(C) void std_stdio_static_this()
extern(C) void std_stdio_static_this()
{
static import core.stdc.stdio;
//Bind stdin, stdout, stderr
Expand All @@ -4340,7 +4334,7 @@ __gshared
{
/** The standard input stream.
Bugs:
Due to $(WEB https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768),
Due to $(LINK2 https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768),
it is thread un-safe to reassign `stdin` to a different `File` instance
than the default.
*/
Expand All @@ -4364,15 +4358,15 @@ __gshared
/**
The standard output stream.
Bugs:
Due to $(WEB https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768),
Due to $(LINK2 https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768),
it is thread un-safe to reassign `stdout` to a different `File` instance
than the default.
*/
File stdout;
/**
The standard error stream.
Bugs:
Due to $(WEB https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768),
Due to $(LINK2 https://issues.dlang.org/show_bug.cgi?id=15768, bug 15768),
it is thread un-safe to reassign `stderr` to a different `File` instance
than the default.
*/
Expand Down
Loading

0 comments on commit 9046849

Please sign in to comment.