endsWith

Checks if the given range ends with (one of) the given needle(s). The reciprocal of startsWith.

  1. uint endsWith(Range doesThisEnd, Needles withOneOfThese)
    uint
    endsWith
    (
    alias pred = "a == b"
    Range
    Needles...
    )
    (,)
    if (
    Needles.length > 1
    &&
    is(typeof(.endsWith!pred(doesThisEnd, withOneOfThese[0])) : bool)
    &&
    is(typeof(.endsWith!pred(doesThisEnd, withOneOfThese[1 .. $])) : uint)
    )
  2. bool endsWith(R1 doesThisEnd, R2 withThis)
  3. bool endsWith(R doesThisEnd, E withThis)
  4. bool endsWith(R doesThisEnd)

Parameters

pred

The predicate to use for comparing elements between the range and the needle(s).

doesThisEnd Range

The bidirectional range to check.

withOneOfThese Needles

The needles to check against, which may be single elements, or bidirectional ranges of elements.

Return Value

Type: uint

0 if the needle(s) do not occur at the end of the given range; otherwise the position of the matching needle, that is, 1 if the range ends with withOneOfThese[0], 2 if it ends with withOneOfThese[1], and so on.

In the case when no needle parameters are given, return true iff back of doesThisStart fulfils predicate pred.

Examples

1 import std.ascii : isAlpha;
2 assert("abc".endsWith!(a => a.isAlpha));
3 assert("abc".endsWith!isAlpha);
4 
5 assert(!"ab1".endsWith!(a => a.isAlpha));
6 
7 assert(!"ab1".endsWith!isAlpha);
8 assert(!"".endsWith!(a => a.isAlpha));
9 
10 import std.algorithm.comparison : among;
11 assert("abc".endsWith!(a => a.among('c', 'd') != 0));
12 assert(!"abc".endsWith!(a => a.among('a', 'b') != 0));
13 
14 assert(endsWith("abc", ""));
15 assert(!endsWith("abc", "b"));
16 assert(endsWith("abc", "a", 'c') == 2);
17 assert(endsWith("abc", "c", "a") == 1);
18 assert(endsWith("abc", "c", "c") == 1);
19 assert(endsWith("abc", "bc", "c") == 2);
20 assert(endsWith("abc", "x", "c", "b") == 2);
21 assert(endsWith("abc", "x", "aa", "bc") == 3);
22 assert(endsWith("abc", "x", "aaa", "sab") == 0);
23 assert(endsWith("abc", "x", "aaa", 'c', "sab") == 3);

Meta

Suggestion Box / Bug Report