Add Path.relative for lexical relative-path computation#9
Draft
carpentry-agent[bot] wants to merge 1 commit into
Draft
Add Path.relative for lexical relative-path computation#9carpentry-agent[bot] wants to merge 1 commit into
carpentry-agent[bot] wants to merge 1 commit into
Conversation
`relative` computes the path to a target relative to a base, purely lexically (no filesystem access): it normalizes both inputs, finds their common leading component prefix, then walks up with `..` for each remaining base component before descending into the target's remainder. It returns a `(Maybe String)`: `Nothing` when the paths cannot be related lexically (one absolute and one relative, or different Windows drive roots) and `Just "."` for equal paths. Named `relative` rather than `relative-to` because the core `defndynamic relative-to` (the load-path helper in Dynamic.carp) shadows that name for unqualified calls, which would break the module's `use`-based convention. It pairs naturally with the existing `absolute`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
Path.relative, the lexical companion toabsolute: it computes the path to atargetrelative to abasewithout touching the filesystem — the staple operation behind Python'sos.path.relpath, Rust'spathdiff, and Java'sPath.relativize.Behaviour
(relative target base)returns a(Maybe String):(relative "/a/b/c" "/a/b")Just "c"(relative "/a/b" "/a/b/c")Just ".."(relative "/a/x" "/a/b")Just "../x"(relative "/a/b" "/a/b")Just "."(relative "/a/b/c" "/x/y")Just "../../a/b/c"(relative "/a/b" "a/b")Nothing(absolute vs relative)It composes the existing primitives: both inputs run through
normalize(so./../redundant separators are resolved first), are split into components, stripped of the empty-string artifactssplityields at roots, then reduced to a common leading prefix plus one..per leftover base component followed by the target's remainder.Nothingis returned when the paths cannot be related lexically — one absolute and one relative, or (on Windows) different drive roots.Naming
The natural name would be
relative-to, but core'sDynamic.carpalready defines adefndynamic relative-to(the load-path helper behindadd-c/add-cflags). That dynamic binding shadows the name for unqualified calls, so(relative-to …)after(use Path)expands the macro instead of calling the function and fails to type-check. Since the module is built for unqualified use (use-all Paththroughout the tests), I named itrelative, which also pairs cleanly with the existingabsolute. Happy to switch to a qualified-onlyrelative-to, or another name, if you'd rather.Tests / CI
test/path.carpgains nineassert-equalcases covering every example above plus the relative→relative and reverse-mismatch cases. Local run: 56 passed, 0 failed.carp-fmt --checkandanglerare clean; committed docs regenerated viagendocs.carp.Opened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.