diff --git a/std/string.d b/std/string.d index b01d86373..fb13b9d02 100644 --- a/std/string.d +++ b/std/string.d @@ -2943,6 +2943,16 @@ unittest Returns: slice of $(D str) stripped of leading and trailing whitespace. +/ +auto strip(Range)(auto ref Range str) + if (!(isSomeString!Range || + isRandomAccessRange!Range && hasLength!Range && hasSlicing!Range && + isSomeChar!(ElementEncodingType!Range)) + && is(StringTypeOf!Range)) +{ + return stripRight(stripLeft(str)); +} + +/// Ditto auto strip(Range)(Range str) if (isSomeString!Range || isRandomAccessRange!Range && hasLength!Range && hasSlicing!Range && @@ -2967,6 +2977,18 @@ auto strip(Range)(Range str) "hello world"); } +@safe pure unittest +{ + static struct TestStruct + { + string s; + alias s this; + } + + string s = " hello world "; + assert(strip(s) == strip(TestStruct(s))); +} + @safe pure unittest { import std.conv : to;