string: add `is_whitespace`
authorJean Privat <jean@pryen.org>
Fri, 27 Feb 2015 05:06:27 +0000 (12:06 +0700)
committerJean Privat <jean@pryen.org>
Fri, 6 Mar 2015 04:33:25 +0000 (11:33 +0700)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/string.nit

index 9724c31..489654e 100644 (file)
@@ -386,6 +386,22 @@ abstract class Text
        # `Char::is_whitespace` determines what is a whitespace.
        fun trim: SELFTYPE do return (self.l_trim).r_trim
 
+       # Is the string non-empty but only made of whitespaces?
+       #
+       #    assert " \n\t ".is_whitespace    == true
+       #    assert "  hello  ".is_whitespace == false
+       #    assert "".is_whitespace          == false
+       #
+       # `Char::is_whitespace` determines what is a whitespace.
+       fun is_whitespace: Bool
+       do
+               if is_empty then return false
+               for c in self.chars do
+                       if not c.is_whitespace then return false
+               end
+               return true
+       end
+
        # Returns `self` removed from its last line terminator (if any).
        #
        #    assert "Hello\n".chomp == "Hello"