Skip to content

[API Proposal]: Normalize() for purely textual path cleanup #132

Description

@dennisdoomen

Background and motivation

ToAbsolute() resolves a relative path against the current directory or a given base. There is no way to simply clean up a path that already contains . or .. segments without changing whether it is relative or absolute, and without consulting the current working directory. This matters when you are producing a path for display, for a manifest, or for a system that will resolve it later in a different context.

API Proposal

namespace Pathy
{
    public readonly struct ChainablePath
    {
        public ChainablePath Normalize();
    }
}

The operation is purely textual. It does not touch the file system and does not resolve symbolic links.

API Usage

ChainablePath.From("src/../docs/./readme.md").Normalize();
// docs/readme.md, still relative

ChainablePath.From("c:/work/repo/src/../docs").Normalize();
// c:/work/repo/docs

ChainablePath.From("../../shared").Normalize();
// ../../shared, leading parent segments are kept because they cannot be resolved

Alternative Designs

Make ToString() normalise by default. That would be a behavioural break for anyone relying on the current output, and it removes the ability to keep a path exactly as the user typed it.

Risks

The behaviour of leading .. segments in a relative path has to be documented, as does the behaviour of .. at the root of an absolute path. Callers must understand that this is not the same as resolving through symbolic links, so the result can differ from what the operating system would resolve.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions