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

Function posix_normalize

codegraph-kernel/src/ruby.rs:60–88  ·  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

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

Callers 1

extract_importMethod · 0.85

Calls 3

lenMethod · 0.80
pushMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected