Skip to content

Instantly share code, notes, and snippets.

@AtkinsSJ
Created June 6, 2023 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtkinsSJ/b0363b9672abbf25566b1a2f1f5da2bd to your computer and use it in GitHub Desktop.
Save AtkinsSJ/b0363b9672abbf25566b1a2f1f5da2bd to your computer and use it in GitHub Desktop.
New Path class

New Path class

Paths may be arbitrary sequences of bytes, so we cannot guarantee that they are utf8 or even convertible to utf8 and back. However, we do want to be able to convert the path to utf8, and vice versa.

  • LexicalPath currently deals with manipulating paths and getting their parts. Do we roll this into Path?
  • Core::Directory deals with iteration, stat and open. Do we roll this into Path?
  • Core::DeprecatedFile has methods for other filesystem checks and operations. Do we roll this into Path?
  • Core::StandardPaths provides getters for the home directory path, and friends. Do we roll this into Path?

Rust has a Path class for a path, allowing you to access its components and query if it's a directory etc; and PathBuf class as a kind of StringBuilder thing?

Python distinguishes between "pure" and "concrete" paths. Pure paths are only hypothetical and don't include any functionality requiring filesystem access. Paths extends them with filesystem functionality.

Python has a couple of neat helper functions:

  • relative_to(other): produces a path that's this/other, normalised.
  • with_name(name): produces a path that is this, but with the filename changed.
  • with_stem(stem) and with_suffix(suffix): produce paths that are this, but with the file basename or extension changed.
  • expanduser(): Replaces ~/ with the path to the user's directory.

We also have a Jakt Path. It's basically a LexicalPath? But produces the components on demand instead of storing a dozen Strings inside.

POSIX has some things to say about paths.

Windows has some... unfortunate complications with paths:

  • \ as a separator. (Not that big a deal probably.)
  • Case insensitivity
  • Drive letters
  • \foo is a rooted path, but not an absolute one. (Do we care about this distinction?)

So, rough idea:

  • Construct from StringView or Span<StringView>
  • StringView raw_path()
  • String to_string()
  • Return a new path:
    • relative_to(Path)
    • with_filename(StringView)
    • with_file_extension(StringView)
    • with_file_basename(StringView)
  • Static
    • Path cwd()
    • All the getters from Core::StandardPaths
    • absolutize(StringView)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment