lib/string: add `chomp`
authorJean Privat <jean@pryen.org>
Wed, 3 Dec 2014 15:16:13 +0000 (10:16 -0500)
committerJean Privat <jean@pryen.org>
Wed, 3 Dec 2014 15:33:19 +0000 (10:33 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/string.nit

index e17d2cf..664ecbc 100644 (file)
@@ -385,6 +385,19 @@ abstract class Text
        #     assert "\na\nb\tc\t".trim          == "a\nb\tc"
        fun trim: SELFTYPE do return (self.l_trim).r_trim
 
+       # Returns `self` removed from its last `\n` (if any).
+       #
+       #    assert "Hello\n".chomp == "Hello"
+       #    assert "Hello".chomp   == "Hello"
+       #    assert "\n\n\n".chomp  == "\n\n"
+       #
+       # This method is mainly used to remove the LINE_FEED character from lines of text.
+       fun chomp: SELFTYPE
+       do
+               if self.chars.last != '\n' then return self
+               return substring(0, length-1)
+       end
+
        # Justify a self in a space of `length`
        #
        # `left` is the space ratio on the left side.