Login
7 branches 0 tags
Ben (X13/Arch) Removed Nujel AVL-Trees, since built-in ones are faster 0d39884 2 years ago 943 Commits
nujel / stdlib / string / path.nuj
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; Some functions for working with paths and file extensions

[defn path/ext?! [ext]
      "Return a predicate that checks if a path ends on EXT"
      [case [type-of ext]
            [:string [fn [path]
                         [= ext [lower-case [path/extension path]]]]]
            [:pair [fn [path]
                       [def cext [lower-case [path/extension path]]]
                     [reduce ext [fn [α β] [or α [= β cext]]]]]]
            [otherwise [exception :type-error "Expected a :string or :list" ext]]]]


[defn path/extension [path]
      "Return the extension of PATH"
      [def last-period [last-index-of path "."]]
      [if [>= last-period 0]
          [string/cut path [+ 1 last-period] [buffer/length path]]
          path]]

[defn path/without-extension [path]
      "Return PATH, but without the extension part"
      [def last-period [last-index-of path "."]]
      [if [>= last-period 0]
          [string/cut path 0 last-period]
          path]]

[defn path/dirname [path]
      "Return the directory part of a PATH"
      [def last-slash [last-index-of path "/"]]
      [if [>= last-slash 0]
          [string/cut path 0 last-slash]
          ""]]

[defn path/basename [path]
      "Return the path without the directory part"
      [def last-slash [last-index-of path "/"]]
      [if [>= last-slash 0]
          [string/cut path [inc last-slash]]
          path]]