aboutsummaryrefslogtreecommitdiff
path: root/libphobos/src/std/algorithm
diff options
context:
space:
mode:
authorIain Buclaw <ibuclaw@gdcproject.org>2022-09-27 10:43:32 +0200
committerIain Buclaw <ibuclaw@gdcproject.org>2022-09-27 10:50:18 +0200
commitc8dfa79c9948ce09a7b4071f8059294b1972aef6 (patch)
treecb93655417a5475c6baac88691fc92621b3fa7ce /libphobos/src/std/algorithm
parentbe4a6551ed37c1e7dbdfb9400fc2e2b5d40c5be2 (diff)
downloadgcc-c8dfa79c9948ce09a7b4071f8059294b1972aef6.zip
gcc-c8dfa79c9948ce09a7b4071f8059294b1972aef6.tar.gz
gcc-c8dfa79c9948ce09a7b4071f8059294b1972aef6.tar.bz2
d: Merge upstream dmd d579c467c1, phobos 88aa69b14.
D front-end changes: - Throwing from contracts of `nothrow' functions has been deprecated, as this breaks the guarantees of `nothrow'. - Added language support for initializing the interior pointer of associative arrays using `new' keyword. Phobos changes: - The std.digest.digest module has been removed. - The std.xml module has been removed. gcc/d/ChangeLog: * dmd/MERGE: Merge upstream dmd d579c467c1. * decl.cc (layout_struct_initializer): Update for new front-end interface. * expr.cc (ExprVisitor::visit (AssignExp *)): Remove lowering of array assignments. (ExprVisitor::visit (NewExp *)): Add new lowering of new'ing associative arrays to an _aaNew() library call. * runtime.def (ARRAYSETASSIGN): Remove. (AANEW): Define. libphobos/ChangeLog: * libdruntime/MERGE: Merge upstream druntime d579c467c1. * libdruntime/Makefile.am (DRUNTIME_DSOURCES): Remove rt/arrayassign.d. * libdruntime/Makefile.in: Regenerate. * src/MERGE: Merge upstream phobos 88aa69b14. * src/Makefile.am (PHOBOS_DSOURCES): Remove std/digest/digest.d, std/xml.d. * src/Makefile.in: Regenerate.
Diffstat (limited to 'libphobos/src/std/algorithm')
-rw-r--r--libphobos/src/std/algorithm/comparison.d20
-rw-r--r--libphobos/src/std/algorithm/iteration.d17
-rw-r--r--libphobos/src/std/algorithm/searching.d76
-rw-r--r--libphobos/src/std/algorithm/sorting.d4
4 files changed, 108 insertions, 9 deletions
diff --git a/libphobos/src/std/algorithm/comparison.d b/libphobos/src/std/algorithm/comparison.d
index b810fbb..5ecb4f6 100644
--- a/libphobos/src/std/algorithm/comparison.d
+++ b/libphobos/src/std/algorithm/comparison.d
@@ -577,14 +577,24 @@ Returns:
and `T3` are different.
*/
T1 clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper)
-if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val) : T1))
+if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val))
+ && (is(T2 : T1) && is(T3 : T1)))
+// cannot use :
+// `if (is(typeof(val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val) : T1))
+// because of https://issues.dlang.org/show_bug.cgi?id=16235.
+// Once that is fixed, we can simply use the ternary in both the template constraint
+// and the template body
in
{
assert(!lower.greaterThan(upper), "Lower can't be greater than upper.");
}
do
{
- return val.lessThan(lower) ? lower : val.greaterThan(upper) ? upper : val;
+ if (val.lessThan(lower))
+ return lower;
+ else if (val.greaterThan(upper))
+ return upper;
+ return val;
}
///
@@ -637,6 +647,12 @@ do
assert(x.clamp(lo, hi).y == 42);
}
+// https://issues.dlang.org/show_bug.cgi?id=23268
+@safe pure nothrow @nogc unittest
+{
+ static assert(__traits(compiles, clamp(short.init, short.init, cast(const) short.init)));
+}
+
// cmp
/**********************************
Performs a lexicographical comparison on two
diff --git a/libphobos/src/std/algorithm/iteration.d b/libphobos/src/std/algorithm/iteration.d
index 3e828ce..39eff0d 100644
--- a/libphobos/src/std/algorithm/iteration.d
+++ b/libphobos/src/std/algorithm/iteration.d
@@ -771,6 +771,23 @@ private struct MapResult(alias fun, Range)
assert(dd.length == 4);
}
+// Verify fix for: https://issues.dlang.org/show_bug.cgi?id=16034
+@safe unittest
+{
+ struct One
+ {
+ int entry = 1;
+ @disable this(this);
+ }
+
+ One[] ones = [One(), One()];
+
+ import std.algorithm.comparison : equal;
+
+ assert(ones.map!`a.entry + 1`.equal([2, 2]));
+}
+
+
@safe unittest
{
import std.algorithm.comparison : equal;
diff --git a/libphobos/src/std/algorithm/searching.d b/libphobos/src/std/algorithm/searching.d
index 870b1b4..15f7ca9 100644
--- a/libphobos/src/std/algorithm/searching.d
+++ b/libphobos/src/std/algorithm/searching.d
@@ -5002,7 +5002,7 @@ If set to `OpenRight.yes`, then the interval is open to the right
(last element is not included).
Otherwise if set to `OpenRight.no`, then the interval is closed to the right
-(last element included).
+including the entire sentinel.
*/
alias OpenRight = Flag!"openRight";
@@ -5052,6 +5052,7 @@ if (isInputRange!Range)
static if (!is(Sentinel == void))
private Sentinel _sentinel;
private OpenRight _openRight;
+ private bool _matchStarted;
private bool _done;
static if (!is(Sentinel == void))
@@ -5063,7 +5064,19 @@ if (isInputRange!Range)
_input = input;
_sentinel = sentinel;
_openRight = openRight;
- _done = _input.empty || openRight && predSatisfied();
+ static if (isInputRange!Sentinel)
+ {
+ _matchStarted = predSatisfied();
+ _done = _input.empty || _sentinel.empty || openRight && _matchStarted;
+ if (_matchStarted && !_done && !openRight)
+ {
+ _sentinel.popFront;
+ }
+ }
+ else
+ {
+ _done = _input.empty || openRight && predSatisfied();
+ }
}
private this(Range input, Sentinel sentinel, OpenRight openRight,
bool done)
@@ -5118,9 +5131,32 @@ if (isInputRange!Range)
assert(!empty, "Can not popFront of an empty Until");
if (!_openRight)
{
- _done = predSatisfied();
- _input.popFront();
- _done = _done || _input.empty;
+ static if (isInputRange!Sentinel)
+ {
+ _input.popFront();
+ _done = _input.empty || _sentinel.empty;
+ if (!_done)
+ {
+ if (_matchStarted)
+ {
+ _sentinel.popFront;
+ }
+ else
+ {
+ _matchStarted = predSatisfied();
+ if (_matchStarted)
+ {
+ _sentinel.popFront;
+ }
+ }
+ }
+ }
+ else
+ {
+ _done = predSatisfied();
+ _input.popFront();
+ _done = _done || _input.empty;
+ }
}
else
{
@@ -5212,3 +5248,33 @@ pure @safe unittest
assert(equal(r.save, "foo"));
}
}
+// https://issues.dlang.org/show_bug.cgi?id=14543
+pure @safe unittest
+{
+ import std.algorithm.comparison : equal;
+ import std.uni : toUpper;
+ assert("one two three".until("two").equal("one "));
+ assert("one two three".until("two", OpenRight.no).equal("one two"));
+
+ assert("one two three".until("two", No.openRight).equal("one two"));
+ assert("one two three".until("two", Yes.openRight).equal("one "));
+
+ assert("one two three".until('t', Yes.openRight).equal("one "));
+ assert("one two three".until("", Yes.openRight).equal(""));
+ assert("one two three".until("", No.openRight).equal(""));
+
+ assert("one two three".until("three", No.openRight).equal("one two three"));
+ assert("one two three".until("three", Yes.openRight).equal("one two "));
+
+ assert("one two three".until("one", No.openRight).equal("one"));
+ assert("one two three".until("one", Yes.openRight).equal(""));
+
+ assert("one two three".until("o", No.openRight).equal("o"));
+ assert("one two three".until("o", Yes.openRight).equal(""));
+
+ assert("one two three".until("", No.openRight).equal(""));
+ assert("one two three".until("", Yes.openRight).equal(""));
+
+ assert("one two three".until!((a,b)=>a.toUpper == b)("TWO", No.openRight).equal("one two"));
+}
+
diff --git a/libphobos/src/std/algorithm/sorting.d b/libphobos/src/std/algorithm/sorting.d
index ee68b23..4fc7ee9 100644
--- a/libphobos/src/std/algorithm/sorting.d
+++ b/libphobos/src/std/algorithm/sorting.d
@@ -1642,9 +1642,9 @@ private void multiSortImpl(Range, SwapStrategy ss, funs...)(Range r)
}
// https://issues.dlang.org/show_bug.cgi?id=16413 - @system comparison function
-@safe unittest
+@system unittest
{
- bool lt(int a, int b) { return a < b; } static @system
+ static @system bool lt(int a, int b) { return a < b; }
auto a = [2, 1];
a.multiSort!(lt, lt);
assert(a == [1, 2]);