CH-J Toolsgestione server su SSH
Menu
Sorgente pubblicata

CH-J Tools

Sfoglia directory e file per una specifica release di applicazioni.

Scarica sorgente ZIP
CH-J / Server Manager 1704 file
0.1.2-beta Aggiunto: · Costruito:
posix.rs Aggiunto: Data non registrata 2,3 KB · 83 righe
Codice Scarica file
1//! POSIX path helpers with Node `path.posix` semantics (normalize/join/basename/dirname).
3pub fn normalize(path: &str) -> String {
4 if path.is_empty() {
5 return ".".into();
6 }
7 let absolute = path.starts_with('/');
8 let trailing = path.ends_with('/');
9 let mut parts: Vec<&str> = Vec::new();
10 for segment in path.split('/') {
11 match segment {
12 "" | "." => {}
13 ".." => {
14 if parts.last().is_some_and(|p| *p != "..") {
15 parts.pop();
16 } else if !absolute {
17 parts.push("..");
18 }
19 }
20 other => parts.push(other),
21 }
22 }
23 let mut out = parts.join("/");
24 if out.is_empty() && !absolute {
25 out = ".".into();
26 }
27 if !out.is_empty() && trailing {
28 out.push('/');
29 }
30 if absolute {
31 format!("/{out}")
32 } else {
33 out
34 }
37pub fn join(base: &str, name: &str) -> String {
38 let joined = match (base.is_empty(), name.is_empty()) {
39 (true, true) => return ".".into(),
40 (true, false) => name.to_string(),
41 (false, true) => base.to_string(),
42 _ => format!("{base}/{name}"),
43 };
44 normalize(&joined)
47pub fn basename(path: &str) -> String {
48 path.trim_end_matches('/').rsplit('/').next().unwrap_or("").to_string()
51/// Directory part of a normalized path (`/a/b` → `/a`, `/a` → `/`).
52pub fn dirname(path: &str) -> String {
53 let trimmed = path.trim_end_matches('/');
54 if trimmed.is_empty() {
55 return if path.starts_with('/') { "/".into() } else { ".".into() };
56 }
57 match trimmed.rfind('/') {
58 Some(0) => "/".into(),
59 Some(i) => {
60 let parent = trimmed[..i].trim_end_matches('/');
61 if parent.is_empty() { "/".into() } else { parent.to_string() }
62 }
63 None => ".".into(),
64 }
67#[cfg(test)]
68mod tests {
69 use super::*;
71 #[test]
72 fn node_compatible() {
73 assert_eq!(normalize("/a//b/../c/./d/"), "/a/c/d/");
74 assert_eq!(normalize("/../.."), "/");
75 assert_eq!(normalize("/"), "/");
76 assert_eq!(join("/", "x"), "/x");
77 assert_eq!(join("/a/b", "c"), "/a/b/c");
78 assert_eq!(basename("/a/b/"), "b");
79 assert_eq!(dirname("/a/b"), "/a");
80 assert_eq!(dirname("/a"), "/");
81 assert_eq!(dirname("/a//b"), "/a");
82 }
SHA-2560ac4db2a018c15e4c1bf9b9676dc5c803cf2652af9f14abf8d8c0ec685cafdec

CH-J Proprietary Software License 1.14 · La sorgente è fornita sotto CH-J Proprietary Software License 1.14. La sua disponibilità non modifica i termini di licenza o concede diritti aggiuntivi.

Archivio SHA-256ac8fe790f4ba60fc098f8c985fc2656cfcc6cdc21fb34b30e308a2b54e62258f
File