lib/core: Added specialized implementation of file_extension and basename on FlatString
authorLucas Bajolet <r4pass@hotmail.com>
Fri, 25 Sep 2015 18:24:57 +0000 (14:24 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 28 Sep 2015 13:54:35 +0000 (09:54 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/file.nit

index aba34e8..03955eb 100644 (file)
@@ -1265,6 +1265,34 @@ redef class FlatString
        do
                s.write_native(items, first_byte, bytelen)
        end
+
+       redef fun file_extension do
+               var its = _items
+               var p = _last_byte
+               var c = its[p]
+               var st = _first_byte
+               while p >= st and c != '.'.ascii do
+                       p -= 1
+                       c = its[p]
+               end
+               if p <= st then return null
+               var ls = _last_byte
+               return new FlatString.with_infos(its, ls - p, p + 1, ls)
+       end
+
+       redef fun basename(extension) do
+               var l = _last_byte
+               var its = _items
+               var min = _first_byte
+               var sl = '/'.ascii
+               while l > min and its[l] == sl do l -= 1
+               if l == min then return "/"
+               var ns = l
+               while ns >= min and its[ns] != sl do ns -= 1
+               var bname = new FlatString.with_infos(its, l - ns, ns + 1, l)
+
+               return if extension != null then bname.strip_extension(extension) else bname
+       end
 end
 
 redef class NativeString