MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / posix_normalize

Function posix_normalize

codegraph-kernel/src/ruby.rs:59–87  ·  view source on GitHub ↗

Node `path.posix.normalize` for the require_relative path join (emitRubyRequireRefs): resolve `.`/`..` lexically, collapse `//`, keep unresolvable leading `..`s, preserve a trailing slash.

(p: &str)

Source from the content-addressed store, hash-verified

57/// (emitRubyRequireRefs): resolve `.`/`..` lexically, collapse `//`, keep
58/// unresolvable leading `..`s, preserve a trailing slash.
59fn posix_normalize(p: &str) -> String {
60 let is_abs = p.starts_with('/');
61 let had_trailing = p.len() > 1 && p.ends_with('/');
62 let mut out: Vec<&str> = Vec::new();
63 for seg in p.split('/') {
64 match seg {
65 "" | "." => {}
66 ".." => {
67 if matches!(out.last(), Some(&last) if last != "..") {
68 out.pop();
69 } else if !is_abs {
70 out.push("..");
71 }
72 }
73 s => out.push(s),
74 }
75 }
76 let mut joined = out.join("/");
77 if is_abs {
78 joined = format!("/{joined}");
79 }
80 if joined.is_empty() || joined == "/" {
81 return if is_abs { "/".to_string() } else { ".".to_string() };
82 }
83 if had_trailing {
84 joined.push('/');
85 }
86 joined
87}
88
89struct Scope {
90 row: u32,

Callers 1

extract_importMethod · 0.85

Calls 2

lenMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected