From 0cee9c320d5bb2f9f8ccfd629f06498336edc5c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Fri, 17 Jul 2015 09:29:12 -0400 Subject: [PATCH] lib/standard: the single parameter of `String::basename` is optional MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/standard/file.nit | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/standard/file.nit b/lib/standard/file.nit index 6f2160e..bc93bdd 100644 --- a/lib/standard/file.nit +++ b/lib/standard/file.nit @@ -734,17 +734,21 @@ redef class String return self end - # Extract the basename of a path and remove the extension + # Extract the basename of a path and strip the `extension` # - # assert "/path/to/a_file.ext".basename(".ext") == "a_file" - # assert "path/to/a_file.ext".basename(".ext") == "a_file" - # assert "path/to".basename(".ext") == "to" - # assert "path/to/".basename(".ext") == "to" + # The extension is stripped only if `extension != null`. + # + # assert "/path/to/a_file.ext".basename(".ext") == "a_file" + # assert "path/to/a_file.ext".basename(".ext") == "a_file" + # assert "path/to/a_file.ext".basename == "a_file.ext" + # assert "path/to".basename(".ext") == "to" + # assert "path/to/".basename(".ext") == "to" + # assert "path/to".basename == "to" # assert "path".basename("") == "path" # assert "/path".basename("") == "path" # assert "/".basename("") == "/" # assert "".basename("") == "" - fun basename(ext: String): String + fun basename(extension: nullable String): String do var l = length - 1 # Index of the last char while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/` @@ -754,7 +758,10 @@ redef class String if pos >= 0 then n = substring(pos+1, l-pos) end - return n.strip_extension(ext) + + if extension != null then + return n.strip_extension(extension) + else return n end # Extract the dirname of a path -- 1.7.9.5