diff --git a/core.d b/core.d index 02869e2..5d32925 100644 --- a/core.d +++ b/core.d @@ -900,11 +900,17 @@ unittest { +/ nothrow @safe @nogc pure inout(char)[] stripInternal(return inout(char)[] s) { + bool isAllWhitespace = true; foreach(i, char c; s) if(c != ' ' && c != '\t' && c != '\n' && c != '\r') { s = s[i .. $]; + isAllWhitespace = false; break; } + + if(isAllWhitespace) + return s[$..$]; + for(int a = cast(int)(s.length - 1); a > 0; a--) { char c = s[a]; if(c != ' ' && c != '\t' && c != '\n' && c != '\r') { @@ -916,15 +922,19 @@ inout(char)[] stripInternal(return inout(char)[] s) { return s; } +/// ditto nothrow @safe @nogc pure inout(char)[] stripRightInternal(return inout(char)[] s) { - for(int a = cast(int)(s.length - 1); a > 0; a--) { - char c = s[a]; + bool isAllWhitespace = true; + foreach_reverse(a, c; s) { if(c != ' ' && c != '\t' && c != '\n' && c != '\r') { s = s[0 .. a + 1]; + isAllWhitespace = false; break; } } + if(isAllWhitespace) + s = s[0..0]; return s;