

//
// trim, trimBegin, trimEnd
//

trim(seq, pred) = Vector(trimmed(seq, pred));

trimBegin(seq, pred) = Vector(beginTrimmed(seq, pred));
trimEnd(seq, pred) = Vector(endTrimmed(seq, pred));


[S | String?(S)]
overload trim(seq:S) = trim(seq, space?);

[S | String?(S)]
overload trimBegin(seq:S) = trimBegin(seq, space?);

[S | String?(S)]
overload trimEnd(seq:S) = trimEnd(seq, space?);



//
// trimmed, beginTrimmed, endTrimmed
//

trimmed(seq, pred) = beginTrimmed(endTrimmed(seq, pred), pred);

beginTrimmed(seq, pred) {
    var b, e = begin(seq), end(seq);
    while ((b != e) and pred(b^))
        inc(b);
    return coordinateRange(b, e);
}

endTrimmed(seq, pred) {
    var b, e = begin(seq), end(seq);
    while ((b != e) and pred((e-1)^))
        dec(e);
    return coordinateRange(b, e);
}


[S | String?(S)]
overload trimmed(seq:S) = trimmed(seq, space?);

[S | String?(S)]
overload beginTrimmed(seq:S) = beginTrimmed(seq, space?);

[S | String?(S)]
overload endTrimmed(seq:S) = endTrimmed(seq, space?);
