diff options
| author | Eugen Wissner <belka@caraus.de> | 2017-05-12 20:23:16 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2017-05-12 20:23:16 +0200 |
| commit | d4ab339feb1dd2ad23b374d44ba9c031ac80cdd6 (patch) | |
| tree | d4693a36a58ab7f13d74e78e6f130f13cc27b32d /source | |
| parent | 847731276920bbde9b3f14e7b0ebd927e0484cd6 (diff) | |
| download | tanya-d4ab339feb1dd2ad23b374d44ba9c031ac80cdd6.tar.gz | |
Add String.remove
Diffstat (limited to 'source')
| -rw-r--r-- | source/tanya/container/string.d | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d index be11492..5cf44c1 100644 --- a/source/tanya/container/string.d +++ b/source/tanya/container/string.d @@ -1357,5 +1357,53 @@ struct String return opSliceAssign(value, 0, length); } + /** + * Remove all characters beloning to $(D_PARAM r). + * + * Params: + * r = Range originally obtained from this string. + * + * Returns: A range spanning the remaining characters in the string that + * initially were right after $(D_PARAM r). + * + * Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this). + */ + R remove(R)(R r) pure nothrow @trusted @nogc + if (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)) + in + { + assert(r.container is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + auto end = this.data + this.length; + copy(ByCodeUnit!char(this, r.end, end), ByCodeUnit!char(this, r.begin, end)); + this.length_ = length - (r.end - r.begin); + return R(this, r.begin, this.data + length); + } + + /// + @nogc @safe unittest + { + auto s = String("Из пословицы слова не выкинешь"); + + assert(s.remove(s[5 .. 24]).length == 32); + assert(s == "Из слова не выкинешь"); + assert(s.length == 37); + + auto byCodePoint = s.byCodePoint(); + byCodePoint.popFrontN(8); + + assert(s.remove(byCodePoint).count == 0); + assert(s == "Из слова"); + + assert(s.remove(s[]).length == 0); + assert(s.length == 0); + + assert(s.remove(s[]).length == 0); + } + mixin DefaultAllocator; } |
