1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
use crate::Parsed;
use super::shared::{
format_inner, is_posix_path_separator, normalize_string, CHAR_DOT, CHAR_FORWARD_SLASH,
};
/// Provides the platform-specific path segment separator:
/// - `\` on Windows
/// - `/` on POSIX
#[allow(non_upper_case_globals)]
pub const sep: char = '/';
/// Provides the platform-specific path delimiter:
/// - `;` for Windows
/// - `:` for POSIX
#[allow(non_upper_case_globals)]
pub const delimiter: char = ':';
///
/// ```rust
/// assert_eq!(nodejs_path::basename_impl("/foo/bar/baz/asdf/quux.html"), "quux.html".to_string());
/// ```
pub fn basename_impl(path: &str) -> String {
parse(path).base
}
/// ```rust
/// assert_eq!(nodejs_path::basename_impl_without_ext("/foo/bar/baz/asdf/quux.html", ".html"), "quux".to_string());
///
/// assert_eq!(nodejs_path::basename_impl_without_ext("/foo/bar/baz/asdf/quux.HTML", ".html"), "quux.HTML".to_string());
/// ```
pub fn basename_impl_without_ext(path: &str, ext: &str) -> String {
let mut base = parse(path).base;
if base.ends_with(ext) {
for _i in 0..ext.chars().collect::<Vec<char>>().len() {
base.pop();
}
}
base
}
/// ```rust
/// assert_eq!(nodejs_path::basename!("/foo/bar/baz/asdf/quux.html"), "quux.html".to_string());
///
/// assert_eq!(nodejs_path::basename!("/foo/bar/baz/asdf/quux.html", ".html"), "quux".to_string());
///
/// assert_eq!(nodejs_path::basename!("/foo/bar/baz/asdf/quux.HTML", ".html"), "quux.HTML".to_string());
/// ```
#[macro_export]
macro_rules! basename {
( $x:expr ) => {{
$crate::posix::basename_impl($x)
}};
( $x:expr, $y:expr ) => {{
$crate::posix::basename_impl_without_ext($x, $y)
}};
}
pub use basename;
/// ```rust
/// assert_eq!(nodejs_path::dirname("/foo/bar/baz/asdf/quux"), "/foo/bar/baz/asdf".to_string());
/// ```
pub fn dirname(path: &str) -> String {
if path.len() == 0 {
".".to_owned()
} else {
let path = path.chars().collect::<Vec<char>>();
let has_root = path
.iter()
.next()
.map(|c| c == &CHAR_FORWARD_SLASH)
.unwrap_or(false);
let mut end = -1;
let mut matched_slash = true;
let mut i = path.len() as i32 - 1;
while i >= 1 {
if path
.get(i as usize)
.map(|c| c == &CHAR_FORWARD_SLASH)
.unwrap_or(false)
{
if !matched_slash {
end = i;
break;
}
} else {
// We saw the first non-path separator
matched_slash = false;
}
i -= 1;
}
if end == -1 {
if has_root {
"/".to_owned()
} else {
".".to_owned()
}
} else if has_root && end == 1 {
"//".to_owned()
} else {
path[0..end as usize].iter().collect()
}
}
}
/// ```rust
/// assert_eq!(nodejs_path::extname("index.html"), ".html".to_string());
///
/// assert_eq!(nodejs_path::extname("index.coffee.md"), ".md".to_string());
///
/// assert_eq!(nodejs_path::extname("index."), ".".to_string());
///
/// assert_eq!(nodejs_path::extname("index"), "".to_string());
///
/// assert_eq!(nodejs_path::extname(".index.md"), ".md".to_string());
/// ```
pub fn extname(path: &str) -> String {
parse(path).ext
}
pub fn format(path_object: Parsed) -> String {
format_inner("/", path_object)
}
pub fn is_absolute(path: &str) -> bool {
path.chars()
.into_iter()
.next()
.map(|c| c == CHAR_FORWARD_SLASH)
.unwrap_or(false)
}
pub fn join() {
todo!()
}
pub fn normalize(path: &str) -> String {
if path.len() == 0 {
return ".".to_string();
}
todo!()
}
/// # Example
/// ```rust
/// let left = nodejs_path::parse("/home/user/dir/file.txt");
/// assert_eq!(left, nodejs_path::Parsed{
/// root: "/".to_string(),
/// dir: "/home/user/dir".to_string(),
/// base: "file.txt".to_string(),
/// ext: ".txt".to_string(),
/// name: "file".to_string(),
/// })
/// ```
///
/// ```plain
/// ┌─────────────────────┬────────────┐
/// │ dir │ base │
/// ├──────┬ ├──────┬─────┤
/// │ root │ │ name │ ext │
/// " / home/user/dir / file .txt "
/// └──────┴──────────────┴──────┴─────┘
/// (All spaces in the "" line should be ignored. They are purely for formatting.)
/// ```
pub fn parse(path: &str) -> Parsed {
let path = path.chars().collect::<Vec<char>>();
let mut ret = Parsed::default();
if path.len() == 0 {
ret
} else {
let is_absolute = path.get(0).map(|c| c == &CHAR_FORWARD_SLASH).unwrap();
let start;
if is_absolute {
ret.root = "/".to_owned();
start = 1;
} else {
start = 0;
}
let mut start_dot = -1;
let mut start_part = 0;
let mut end = -1;
let mut matched_slash = true;
let mut i = (path.len() - 1) as i32;
// Track the state of characters (if any) we see before our first dot and
// after any path separator we find
let mut pre_dot_state = 0;
// Get non-dir info
while i >= start {
println!(
"start {} start_dot {} end {} pre_dot_state {} start_part {}",
start, start_dot, end, pre_dot_state, start_part
);
let code = *path.get(i as usize).unwrap();
if code == CHAR_FORWARD_SLASH {
// If we reached a path separator that was not part of a set of path
// separators at the end of the string, stop now
if !matched_slash {
start_part = i + 1;
// i -= 1;
break;
}
i -= 1;
continue;
}
if end == -1 {
// We saw the first non-path separator, mark this as the end of our
// extension
matched_slash = false;
end = i + 1;
}
if code == CHAR_DOT {
// If this is our first dot, mark it as the start of our extension
if start_dot == -1 {
start_dot = i;
} else if pre_dot_state != 1 {
pre_dot_state = 1;
}
} else if start_dot != -1 {
// We saw a non-dot and non-path separator before our dot, so we should
// have a good chance at having a non-empty extension
pre_dot_state = -1;
}
i -= 1;
}
if end != -1 {
let start = if start_part == 0 && is_absolute {
1
} else {
start_part
};
if start_dot == -1 ||
// We saw a non-dot character immediately before the dot
pre_dot_state == 0 ||
// The (right-most) trimmed path component is exactly '..'
(pre_dot_state == 1 &&
start_dot == end - 1 &&
start_dot == start_part + 1)
{
ret.base = path[start as usize..end as usize].iter().collect();
ret.name = ret.base.clone();
} else {
ret.name = path[start as usize..start_dot as usize].iter().collect();
ret.base = path[start as usize..end as usize].iter().collect();
ret.ext = path[start_dot as usize..end as usize].iter().collect();
}
}
if start_part > 0 {
ret.dir = path[0..(start_part - 1) as usize].iter().collect();
} else if is_absolute {
ret.dir = "/".to_owned();
}
ret
}
}
pub fn relative() {
todo!()
}
pub fn resolve_impl(args: &[&str]) -> String {
let mut resolved_path = "".to_owned();
let mut resolved_absolute = false;
let mut i = args.len() as i32 - 1;
while i >= -1 && !resolved_absolute {
let path = if i >= 0 {
args.get(i.clone() as usize).unwrap().to_string()
} else {
posix_cwd()
};
// Skip empty entries
if path.len() == 0 {
i -= 1;
continue;
}
resolved_path = format!("{}/{}", path, resolved_path);
resolved_absolute = path
.chars()
.next()
.map(|c| c == CHAR_FORWARD_SLASH)
.unwrap_or(false);
i -= 1;
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolved_path = normalize_string(
&resolved_path,
!resolved_absolute,
&sep,
&is_posix_path_separator,
);
if resolved_absolute {
"/".to_owned() + &resolved_path
} else {
if resolved_path.len() > 0 {
resolved_path
} else {
".".to_owned()
}
}
}
/// #Example
///
/// ```rust
/// assert_eq!(nodejs_path::resolve!("/foo/bar", "./baz"), "/foo/bar/baz".to_string());
///
/// assert_eq!(nodejs_path::resolve!("/foo/bar", "/tmp/file/"), "/tmp/file".to_string());
///
/// assert_eq!(nodejs_path::resolve!("/home/myself/node", "wwwroot", "static_files/png/", "../gif/image.gif"), "/home/myself/node/wwwroot/static_files/gif/image.gif".to_string());
///
/// assert_eq!(nodejs_path::resolve!("."), std::env::current_dir().unwrap().to_str().unwrap().to_owned());
///
/// assert_eq!(nodejs_path::resolve!(), std::env::current_dir().unwrap().to_str().unwrap().to_owned());
/// ```
#[macro_export]
macro_rules! resolve {
( $( $x:expr ),* ) => {
{
$crate::posix::resolve_impl(&[
$(
$x,
)*
])
}
};
}
pub use resolve;
pub fn to_namespaced_path() {}
fn posix_cwd() -> String {
let cwd = std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_owned();
if cfg!(target_os = "windows") {
// Converts Windows' backslash path separators to POSIX forward slashes
// and truncates any drive indicator
// const regexp = /\\/g;
// return () => {
// const cwd = StringPrototypeReplace(process.cwd(), regexp, '/');
// return StringPrototypeSlice(cwd, StringPrototypeIndexOf(cwd, '/'));
// };
return cwd
.chars()
.map(|c| if c == '\\' { '/' } else { c })
.take_while(|c| c != &'/')
.collect();
}
// We're already on POSIX, no need for any transformations
return cwd;
}
