Skip to content

Commit

Permalink
Merge pull request #979 from 9rnsr/use_opDollar
Browse files Browse the repository at this point in the history
Add `alias opDollar` and use $ in range slicing
  • Loading branch information
andralex committed Dec 9, 2012
2 parents d6100b4 + 07104b5 commit 8953d52
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 78 deletions.
57 changes: 27 additions & 30 deletions std/algorithm.d
Expand Up @@ -534,7 +534,7 @@ unittest
assert(squares[3] == 16);

// Test slicing.
auto squareSlice = squares[1..squares.length - 1];
auto squareSlice = squares[1..$-1];
assert(equal(squareSlice, [4, 9][]));
assert(squareSlice.back == 9);
assert(squareSlice[1] == 9);
Expand Down Expand Up @@ -2111,7 +2111,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
}
else
{
_input = _input[_frontLength .. _input.length];
_input = _input[_frontLength .. $];
skipOver(_input, _separator) || assert(false);
_frontLength = _unComputed;
}
Expand Down Expand Up @@ -2144,7 +2144,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
_backLength = _input.length - lastIndex - 1;
}
}
return _input[_input.length - _backLength .. _input.length];
return _input[$ - _backLength .. $];
}

void popBack()
Expand All @@ -2164,7 +2164,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
}
else
{
_input = _input[0 .. _input.length - _backLength];
_input = _input[0 .. $ - _backLength];
if (!_input.empty && _input.back == _separator)
{
_input.popBack();
Expand Down Expand Up @@ -2342,15 +2342,15 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
{
// Special case: popping the first-to-last item; there is
// an empty item right after this.
_input = _input[_input.length .. _input.length];
_input = _input[$ .. $];
_frontLength = 0;
static if (isBidirectionalRange!Range)
_backLength = 0;
return;
}
// Normal case, pop one item and the separator, get ready for
// reading the next item
_input = _input[_frontLength + separatorLength .. _input.length];
_input = _input[_frontLength + separatorLength .. $];
// mark _frontLength as uninitialized
_frontLength = _frontLength.max;
}
Expand All @@ -2371,7 +2371,7 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
@property Range back()
{
ensureBackLength();
return _input[_input.length - _backLength .. _input.length];
return _input[$ - _backLength .. $];
}

void popBack()
Expand All @@ -2395,7 +2395,7 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
return;
}
// Normal case
_input = _input[0 .. _input.length - _backLength - separatorLength];
_input = _input[0 .. $ - _backLength - separatorLength];
_backLength = _backLength.max;
}
}
Expand Down Expand Up @@ -2511,7 +2511,7 @@ private struct SplitterResult(alias isTerminator, Range)
return;
}
// Skip over existing word
_input = _input[_end .. _input.length];
_input = _input[_end .. $];
// Skip terminator
for (;;)
{
Expand Down Expand Up @@ -3299,9 +3299,7 @@ if (isRandomAccessRange!R1 && isBidirectionalRange!R2
const needleLength = walkLength(needle.save);
if (needleLength > haystack.length)
{
// @@@BUG@@@
//return haystack[$ .. $];
return haystack[haystack.length .. haystack.length];
return haystack[$ .. $];
}
// @@@BUG@@@
// auto needleBack = moveBack(needle);
Expand All @@ -3319,15 +3317,15 @@ if (isRandomAccessRange!R1 && isBidirectionalRange!R2
{
if (scout >= haystack.length)
{
return haystack[haystack.length .. haystack.length];
return haystack[$ .. $];
}
if (!binaryFun!pred(haystack[scout], needleBack))
{
++scout;
continue;
}
// Found a match with the last element in the needle
auto cand = haystack[scout + 1 - needleLength .. haystack.length];
auto cand = haystack[scout + 1 - needleLength .. $];
if (startsWith!pred(cand, needle))
{
// found
Expand Down Expand Up @@ -3476,9 +3474,8 @@ unittest
// Failed search
static if (hasLength!R1)
{
static if (is(typeof(haystack[haystack.length ..
haystack.length]) : R1))
return haystack[haystack.length .. haystack.length];
static if (is(typeof(haystack[$ .. $]) : R1))
return haystack[$ .. $];
else
return R1.init;
}
Expand Down Expand Up @@ -3718,7 +3715,7 @@ is ignored.
return 0;

immutable delta = portion - ignore;
return equal(needle[needle.length - delta .. needle.length],
return equal(needle[$ - delta .. $],
needle[virtual_begin .. virtual_begin + delta]);
}

Expand Down Expand Up @@ -3948,7 +3945,7 @@ if (isForwardRange!R1 && isForwardRange!R2)
immutable pos2 = balance.empty ? pos1 : pos1 + needle.length;
return tuple(haystack[0 .. pos1],
haystack[pos1 .. pos2],
haystack[pos2 .. haystack.length]);
haystack[pos2 .. $]);
}
else
{
Expand Down Expand Up @@ -3987,7 +3984,7 @@ if (isForwardRange!R1 && isForwardRange!R2)
{
auto balance = find!pred(haystack, needle);
immutable pos = haystack.length - balance.length;
return tuple(haystack[0 .. pos], haystack[pos .. haystack.length]);
return tuple(haystack[0 .. pos], haystack[pos .. $]);
}
else
{
Expand Down Expand Up @@ -4023,7 +4020,7 @@ if (isForwardRange!R1 && isForwardRange!R2)
{
auto balance = find!pred(haystack, needle);
immutable pos = balance.empty ? 0 : haystack.length - balance.length + needle.length;
return tuple(haystack[0 .. pos], haystack[pos .. haystack.length]);
return tuple(haystack[0 .. pos], haystack[pos .. $]);
}
else
{
Expand Down Expand Up @@ -7024,8 +7021,8 @@ Range partition(alias predicate,
alias .partition!(pred, ss, Range) recurse;
auto lower = recurse(r[0 .. middle]);
auto upper = recurse(r[middle .. $]);
bringToFront(lower, r[middle .. r.length - upper.length]);
return r[r.length - lower.length - upper.length .. r.length];
bringToFront(lower, r[middle .. $ - upper.length]);
return r[$ - lower.length - upper.length .. $];
}
else static if (ss == SwapStrategy.semistable)
{
Expand Down Expand Up @@ -7226,10 +7223,10 @@ if (ss == SwapStrategy.unstable && isRandomAccessRange!Range
auto swapLen = min(i, strictlyLess);
swapRanges(r[0 .. swapLen], r[j - swapLen .. j]);
swapLen = min(r.length - l, strictlyGreater);
swapRanges(r[k .. k + swapLen], r[r.length - swapLen .. r.length]);
swapRanges(r[k .. k + swapLen], r[$ - swapLen .. $]);
return tuple(r[0 .. strictlyLess],
r[strictlyLess .. r.length - strictlyGreater],
r[r.length - strictlyGreater .. r.length]);
r[strictlyLess .. $ - strictlyGreater],
r[$ - strictlyGreater .. $]);
}

unittest
Expand Down Expand Up @@ -7486,7 +7483,7 @@ sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
enum maxLen = 8;
assert(isSorted!lessFun(r), text("Failed to sort range of type ",
Range.stringof, ". Actual result is: ",
r[0 .. r.length > maxLen ? maxLen : r.length ],
r[0 .. $ > maxLen ? maxLen : $],
r.length > maxLen ? "..." : ""));
}
else
Expand Down Expand Up @@ -7762,7 +7759,7 @@ private void sortImpl(alias less, SwapStrategy ss, Range)(Range r)
}

swapAt(r, r.length - 1, lessI);
auto right = r[lessI + 1..r.length];
auto right = r[lessI + 1 .. $];

auto left = r[0..min(lessI, greaterI + 1)];
if (right.length > left.length)
Expand Down Expand Up @@ -7798,7 +7795,7 @@ private void sortImpl(alias less, SwapStrategy ss, Range)(Range r)
}
else
{
.sortImpl!(less, ss, Range)(r[0 .. r.length - right.length]);
.sortImpl!(less, ss, Range)(r[0 .. $ - right.length]);
r = right;
}
}
Expand Down Expand Up @@ -7925,7 +7922,7 @@ unittest
/**
Reorders the random-access range $(D r) such that the range $(D r[0
.. mid]) is the same as if the entire $(D r) were sorted, and leaves
the range $(D r[mid .. r.length]) in no particular order. Performs
the range $(D r[mid .. $(DOLLAR)]) in no particular order. Performs
$(BIGOH r.length * log(mid)) evaluations of $(D pred). The
implementation simply calls $(D topN!(less, ss)(r, n)) and then $(D
sort!(less, ss)(r[0 .. n])).
Expand Down
25 changes: 12 additions & 13 deletions std/array.d
Expand Up @@ -495,7 +495,7 @@ unittest
{
assert(a.length, "Attempting to popBack() past the front of an array of " ~
typeof(a[0]).stringof);
a = a[0 .. $ - std.utf.strideBack(a, a.length)];
a = a[0 .. $ - std.utf.strideBack(a, $)];
}

unittest
Expand Down Expand Up @@ -1235,12 +1235,12 @@ unittest
assert(cmp(words[3], "jerry") == 0);
assert(cmp(words[4], "") == 0);

auto s1 = s[0 .. s.length - 1]; // lop off trailing ','
auto s1 = s[0 .. $ - 1]; // lop off trailing ','
words = split(s1, ",");
assert(words.length == 4);
assert(cmp(words[3], "jerry") == 0);

auto s2 = s1[1 .. s1.length]; // lop off leading ','
auto s2 = s1[1 .. $]; // lop off leading ','
words = split(s2, ",");
assert(words.length == 3);
assert(cmp(words[0], "peter") == 0);
Expand All @@ -1255,12 +1255,12 @@ unittest
assert(cmp(words[3], "jerry") == 0);
assert(cmp(words[4], "") == 0);

auto s4 = s3[0 .. s3.length - 2]; // lop off trailing ',,'
auto s4 = s3[0 .. $ - 2]; // lop off trailing ',,'
words = split(s4, ",,");
assert(words.length == 4);
assert(cmp(words[3], "jerry") == 0);

auto s5 = s4[2 .. s4.length]; // lop off leading ',,'
auto s5 = s4[2 .. $]; // lop off leading ',,'
words = split(s5, ",,");
assert(words.length == 3);
assert(cmp(words[0], "peter") == 0);
Expand Down Expand Up @@ -1561,7 +1561,7 @@ if (isDynamicArray!(E[]) && isForwardRange!R1 && isForwardRange!R2
return subject;

auto app = appender!(E[])();
app.put(subject[0 .. subject.length - balance.length]);
app.put(subject[0 .. $ - balance.length]);
app.put(to.save);
replaceInto(app, balance[from.length .. $], from, to);

Expand Down Expand Up @@ -1590,7 +1590,7 @@ if (isOutputRange!(Sink, E) && isDynamicArray!(E[])
sink.put(subject);
break;
}
sink.put(subject[0 .. subject.length - balance.length]);
sink.put(subject[0 .. $ - balance.length]);
sink.put(to.save);
subject = balance[from.length .. $];
}
Expand Down Expand Up @@ -1901,7 +1901,7 @@ if (isDynamicArray!(E[]) &&
auto balance = std.algorithm.find(subject, from.save);
if (balance.empty) return subject;
auto app = appender!(E[])();
app.put(subject[0 .. subject.length - balance.length]);
app.put(subject[0 .. $ - balance.length]);
app.put(to.save);
app.put(balance[from.length .. $]);

Expand Down Expand Up @@ -1958,8 +1958,7 @@ body
immutable so = slice.ptr - s.ptr;
result[0 .. so] = s[0 .. so];
result[so .. so + replacement.length] = replacement[];
result[so + replacement.length .. result.length] =
s[so + slice.length .. s.length];
result[so + replacement.length .. $] = s[so + slice.length .. $];

return cast(inout(T)[]) result;
}
Expand Down Expand Up @@ -2555,7 +2554,7 @@ struct SimpleSlice(T)
core.memory.GC.malloc(newLen * T.sizeof);
result._e = result._b + newLen;
result[0 .. this.length] = this;
result[this.length .. result.length] = another;
result[this.length .. $] = another;
return result;
}

Expand Down Expand Up @@ -2609,8 +2608,8 @@ unittest
// assert(s[2] == 6);

// assert(s[] == s);
// assert(s[0 .. s.length] == s);
// assert(equal(s[0 .. s.length - 1], [4, 5][]));
// assert(s[0 .. $] == s);
// assert(equal(s[0 .. $ - 1], [4, 5][]));

// auto s1 = s ~ s[0 .. 1];
// assert(equal(s1, [4, 5, 6, 4][]));
Expand Down

0 comments on commit 8953d52

Please sign in to comment.