1use globset::{Glob, GlobSet, GlobSetBuilder};
2use regex::Regex;
3use serde::{Deserialize, Serialize};
4use std::cmp::Ordering;
5use std::fmt::{Display, Formatter};
6use std::mem;
7use std::path::StripPrefixError;
8use std::sync::{Arc, OnceLock};
9use std::{
10 ffi::OsStr,
11 path::{Path, PathBuf},
12 sync::LazyLock,
13};
14
15static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
16
17/// Returns the path to the user's home directory.
18pub fn home_dir() -> &'static PathBuf {
19 HOME_DIR.get_or_init(|| {
20 if cfg!(any(test, feature = "test-support")) {
21 if cfg!(target_os = "macos") {
22 PathBuf::from("/Users/zed")
23 } else if cfg!(target_os = "windows") {
24 PathBuf::from("C:\\Users\\zed")
25 } else {
26 PathBuf::from("/home/zed")
27 }
28 } else {
29 dirs::home_dir().expect("failed to determine home directory")
30 }
31 })
32}
33
34#[cfg(any(test, feature = "test-support"))]
35pub fn set_home_dir(path: PathBuf) {
36 HOME_DIR
37 .set(path)
38 .expect("set_home_dir called after home_dir was already accessed");
39}
40
41pub trait PathExt {
42 fn compact(&self) -> PathBuf;
43 fn extension_or_hidden_file_name(&self) -> Option<&str>;
44 fn to_sanitized_string(&self) -> String;
45 fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
46 where
47 Self: From<&'a Path>,
48 {
49 #[cfg(unix)]
50 {
51 use std::os::unix::prelude::OsStrExt;
52 Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))
53 }
54 #[cfg(windows)]
55 {
56 use anyhow::Context as _;
57 use tendril::fmt::{Format, WTF8};
58 WTF8::validate(bytes)
59 .then(|| {
60 // Safety: bytes are valid WTF-8 sequence.
61 Self::from(Path::new(unsafe {
62 OsStr::from_encoded_bytes_unchecked(bytes)
63 }))
64 })
65 .with_context(|| format!("Invalid WTF-8 sequence: {bytes:?}"))
66 }
67 }
68 fn local_to_wsl(&self) -> Option<PathBuf>;
69}
70
71impl<T: AsRef<Path>> PathExt for T {
72 /// Compacts a given file path by replacing the user's home directory
73 /// prefix with a tilde (`~`).
74 ///
75 /// # Returns
76 ///
77 /// * A `PathBuf` containing the compacted file path. If the input path
78 /// does not have the user's home directory prefix, or if we are not on
79 /// Linux or macOS, the original path is returned unchanged.
80 fn compact(&self) -> PathBuf {
81 if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
82 match self.as_ref().strip_prefix(home_dir().as_path()) {
83 Ok(relative_path) => {
84 let mut shortened_path = PathBuf::new();
85 shortened_path.push("~");
86 shortened_path.push(relative_path);
87 shortened_path
88 }
89 Err(_) => self.as_ref().to_path_buf(),
90 }
91 } else {
92 self.as_ref().to_path_buf()
93 }
94 }
95
96 /// Returns a file's extension or, if the file is hidden, its name without the leading dot
97 fn extension_or_hidden_file_name(&self) -> Option<&str> {
98 let path = self.as_ref();
99 let file_name = path.file_name()?.to_str()?;
100 if file_name.starts_with('.') {
101 return file_name.strip_prefix('.');
102 }
103
104 path.extension()
105 .and_then(|e| e.to_str())
106 .or_else(|| path.file_stem()?.to_str())
107 }
108
109 /// Returns a sanitized string representation of the path.
110 /// Note, on Windows, this assumes that the path is a valid UTF-8 string and
111 /// is not a UNC path.
112 fn to_sanitized_string(&self) -> String {
113 #[cfg(target_os = "windows")]
114 {
115 self.as_ref().to_string_lossy().replace("/", "\\")
116 }
117 #[cfg(not(target_os = "windows"))]
118 {
119 self.as_ref().to_string_lossy().to_string()
120 }
121 }
122
123 /// Converts a local path to one that can be used inside of WSL.
124 /// Returns `None` if the path cannot be converted into a WSL one (network share).
125 fn local_to_wsl(&self) -> Option<PathBuf> {
126 let mut new_path = PathBuf::new();
127 for component in self.as_ref().components() {
128 match component {
129 std::path::Component::Prefix(prefix) => {
130 let drive_letter = prefix.as_os_str().to_string_lossy().to_lowercase();
131 let drive_letter = drive_letter.strip_suffix(':')?;
132
133 new_path.push(format!("/mnt/{}", drive_letter));
134 }
135 std::path::Component::RootDir => {}
136 _ => new_path.push(component),
137 }
138 }
139
140 Some(new_path)
141 }
142}
143
144/// In memory, this is identical to `Path`. On non-Windows conversions to this type are no-ops. On
145/// windows, these conversions sanitize UNC paths by removing the `\\\\?\\` prefix.
146#[derive(Eq, PartialEq, Hash, Ord, PartialOrd)]
147#[repr(transparent)]
148pub struct SanitizedPath(Path);
149
150impl SanitizedPath {
151 pub fn new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
152 #[cfg(not(target_os = "windows"))]
153 return Self::unchecked_new(path.as_ref());
154
155 #[cfg(target_os = "windows")]
156 return Self::unchecked_new(dunce::simplified(path.as_ref()));
157 }
158
159 pub fn unchecked_new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
160 // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
161 unsafe { mem::transmute::<&Path, &Self>(path.as_ref()) }
162 }
163
164 pub fn from_arc(path: Arc<Path>) -> Arc<Self> {
165 // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
166 #[cfg(not(target_os = "windows"))]
167 return unsafe { mem::transmute::<Arc<Path>, Arc<Self>>(path) };
168
169 // TODO: could avoid allocating here if dunce::simplified results in the same path
170 #[cfg(target_os = "windows")]
171 return Self::new(&path).into();
172 }
173
174 pub fn new_arc<T: AsRef<Path> + ?Sized>(path: &T) -> Arc<Self> {
175 Self::new(path).into()
176 }
177
178 pub fn cast_arc(path: Arc<Self>) -> Arc<Path> {
179 // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
180 unsafe { mem::transmute::<Arc<Self>, Arc<Path>>(path) }
181 }
182
183 pub fn cast_arc_ref(path: &Arc<Self>) -> &Arc<Path> {
184 // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
185 unsafe { mem::transmute::<&Arc<Self>, &Arc<Path>>(path) }
186 }
187
188 pub fn starts_with(&self, prefix: &Self) -> bool {
189 self.0.starts_with(&prefix.0)
190 }
191
192 pub fn as_path(&self) -> &Path {
193 &self.0
194 }
195
196 pub fn file_name(&self) -> Option<&std::ffi::OsStr> {
197 self.0.file_name()
198 }
199
200 pub fn extension(&self) -> Option<&std::ffi::OsStr> {
201 self.0.extension()
202 }
203
204 pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
205 self.0.join(path)
206 }
207
208 pub fn parent(&self) -> Option<&Self> {
209 self.0.parent().map(Self::unchecked_new)
210 }
211
212 pub fn strip_prefix(&self, base: &Self) -> Result<&Path, StripPrefixError> {
213 self.0.strip_prefix(base.as_path())
214 }
215
216 pub fn to_str(&self) -> Option<&str> {
217 self.0.to_str()
218 }
219
220 pub fn to_path_buf(&self) -> PathBuf {
221 self.0.to_path_buf()
222 }
223
224 pub fn to_glob_string(&self) -> String {
225 #[cfg(target_os = "windows")]
226 {
227 self.0.to_string_lossy().replace("/", "\\")
228 }
229 #[cfg(not(target_os = "windows"))]
230 {
231 self.0.to_string_lossy().to_string()
232 }
233 }
234}
235
236impl std::fmt::Debug for SanitizedPath {
237 fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
238 std::fmt::Debug::fmt(&self.0, formatter)
239 }
240}
241
242impl Display for SanitizedPath {
243 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
244 write!(f, "{}", self.0.display())
245 }
246}
247
248impl From<&SanitizedPath> for Arc<SanitizedPath> {
249 fn from(sanitized_path: &SanitizedPath) -> Self {
250 let path: Arc<Path> = sanitized_path.0.into();
251 // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
252 unsafe { mem::transmute(path) }
253 }
254}
255
256impl From<&SanitizedPath> for PathBuf {
257 fn from(sanitized_path: &SanitizedPath) -> Self {
258 sanitized_path.as_path().into()
259 }
260}
261
262impl AsRef<Path> for SanitizedPath {
263 fn as_ref(&self) -> &Path {
264 &self.0
265 }
266}
267
268#[derive(Debug, Clone, Copy, PartialEq, Eq)]
269pub enum PathStyle {
270 Posix,
271 Windows,
272}
273
274impl PathStyle {
275 #[cfg(target_os = "windows")]
276 pub const fn current() -> Self {
277 PathStyle::Windows
278 }
279
280 #[cfg(not(target_os = "windows"))]
281 pub const fn current() -> Self {
282 PathStyle::Posix
283 }
284
285 #[inline]
286 pub fn separator(&self) -> &str {
287 match self {
288 PathStyle::Posix => "/",
289 PathStyle::Windows => "\\",
290 }
291 }
292}
293
294#[derive(Debug, Clone)]
295pub struct RemotePathBuf {
296 inner: PathBuf,
297 style: PathStyle,
298 string: String, // Cached string representation
299}
300
301impl RemotePathBuf {
302 pub fn new(path: PathBuf, style: PathStyle) -> Self {
303 #[cfg(target_os = "windows")]
304 let string = match style {
305 PathStyle::Posix => path.to_string_lossy().replace('\\', "/"),
306 PathStyle::Windows => path.to_string_lossy().into(),
307 };
308 #[cfg(not(target_os = "windows"))]
309 let string = match style {
310 PathStyle::Posix => path.to_string_lossy().to_string(),
311 PathStyle::Windows => path.to_string_lossy().replace('/', "\\"),
312 };
313 Self {
314 inner: path,
315 style,
316 string,
317 }
318 }
319
320 pub fn from_str(path: &str, style: PathStyle) -> Self {
321 let path_buf = PathBuf::from(path);
322 Self::new(path_buf, style)
323 }
324
325 #[cfg(target_os = "windows")]
326 pub fn to_proto(&self) -> String {
327 match self.path_style() {
328 PathStyle::Posix => self.to_string(),
329 PathStyle::Windows => self.inner.to_string_lossy().replace('\\', "/"),
330 }
331 }
332
333 #[cfg(not(target_os = "windows"))]
334 pub fn to_proto(&self) -> String {
335 match self.path_style() {
336 PathStyle::Posix => self.inner.to_string_lossy().to_string(),
337 PathStyle::Windows => self.to_string(),
338 }
339 }
340
341 pub fn as_path(&self) -> &Path {
342 &self.inner
343 }
344
345 pub fn path_style(&self) -> PathStyle {
346 self.style
347 }
348
349 pub fn parent(&self) -> Option<RemotePathBuf> {
350 self.inner
351 .parent()
352 .map(|p| RemotePathBuf::new(p.to_path_buf(), self.style))
353 }
354}
355
356impl Display for RemotePathBuf {
357 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
358 write!(f, "{}", self.string)
359 }
360}
361
362/// A delimiter to use in `path_query:row_number:column_number` strings parsing.
363pub const FILE_ROW_COLUMN_DELIMITER: char = ':';
364
365const ROW_COL_CAPTURE_REGEX: &str = r"(?xs)
366 ([^\(]+)\:(?:
367 \((\d+)[,:](\d+)\) # filename:(row,column), filename:(row:column)
368 |
369 \((\d+)\)() # filename:(row)
370 )
371 |
372 ([^\(]+)(?:
373 \((\d+)[,:](\d+)\) # filename(row,column), filename(row:column)
374 |
375 \((\d+)\)() # filename(row)
376 )
377 |
378 (.+?)(?:
379 \:+(\d+)\:(\d+)\:*$ # filename:row:column
380 |
381 \:+(\d+)\:*()$ # filename:row
382 )";
383
384/// A representation of a path-like string with optional row and column numbers.
385/// Matching values example: `te`, `test.rs:22`, `te:22:5`, `test.c(22)`, `test.c(22,5)`etc.
386#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
387pub struct PathWithPosition {
388 pub path: PathBuf,
389 pub row: Option<u32>,
390 // Absent if row is absent.
391 pub column: Option<u32>,
392}
393
394impl PathWithPosition {
395 /// Returns a PathWithPosition from a path.
396 pub fn from_path(path: PathBuf) -> Self {
397 Self {
398 path,
399 row: None,
400 column: None,
401 }
402 }
403
404 /// Parses a string that possibly has `:row:column` or `(row, column)` suffix.
405 /// Parenthesis format is used by [MSBuild](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-diagnostic-format-for-tasks) compatible tools
406 /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
407 /// If the suffix parsing fails, the whole string is parsed as a path.
408 ///
409 /// Be mindful that `test_file:10:1:` is a valid posix filename.
410 /// `PathWithPosition` class assumes that the ending position-like suffix is **not** part of the filename.
411 ///
412 /// # Examples
413 ///
414 /// ```
415 /// # use util::paths::PathWithPosition;
416 /// # use std::path::PathBuf;
417 /// assert_eq!(PathWithPosition::parse_str("test_file"), PathWithPosition {
418 /// path: PathBuf::from("test_file"),
419 /// row: None,
420 /// column: None,
421 /// });
422 /// assert_eq!(PathWithPosition::parse_str("test_file:10"), PathWithPosition {
423 /// path: PathBuf::from("test_file"),
424 /// row: Some(10),
425 /// column: None,
426 /// });
427 /// assert_eq!(PathWithPosition::parse_str("test_file.rs"), PathWithPosition {
428 /// path: PathBuf::from("test_file.rs"),
429 /// row: None,
430 /// column: None,
431 /// });
432 /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1"), PathWithPosition {
433 /// path: PathBuf::from("test_file.rs"),
434 /// row: Some(1),
435 /// column: None,
436 /// });
437 /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2"), PathWithPosition {
438 /// path: PathBuf::from("test_file.rs"),
439 /// row: Some(1),
440 /// column: Some(2),
441 /// });
442 /// ```
443 ///
444 /// # Expected parsing results when encounter ill-formatted inputs.
445 /// ```
446 /// # use util::paths::PathWithPosition;
447 /// # use std::path::PathBuf;
448 /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a"), PathWithPosition {
449 /// path: PathBuf::from("test_file.rs:a"),
450 /// row: None,
451 /// column: None,
452 /// });
453 /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a:b"), PathWithPosition {
454 /// path: PathBuf::from("test_file.rs:a:b"),
455 /// row: None,
456 /// column: None,
457 /// });
458 /// assert_eq!(PathWithPosition::parse_str("test_file.rs::"), PathWithPosition {
459 /// path: PathBuf::from("test_file.rs::"),
460 /// row: None,
461 /// column: None,
462 /// });
463 /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1"), PathWithPosition {
464 /// path: PathBuf::from("test_file.rs"),
465 /// row: Some(1),
466 /// column: None,
467 /// });
468 /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::"), PathWithPosition {
469 /// path: PathBuf::from("test_file.rs"),
470 /// row: Some(1),
471 /// column: None,
472 /// });
473 /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1:2"), PathWithPosition {
474 /// path: PathBuf::from("test_file.rs"),
475 /// row: Some(1),
476 /// column: Some(2),
477 /// });
478 /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::2"), PathWithPosition {
479 /// path: PathBuf::from("test_file.rs:1"),
480 /// row: Some(2),
481 /// column: None,
482 /// });
483 /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2:3"), PathWithPosition {
484 /// path: PathBuf::from("test_file.rs:1"),
485 /// row: Some(2),
486 /// column: Some(3),
487 /// });
488 /// ```
489 pub fn parse_str(s: &str) -> Self {
490 let trimmed = s.trim();
491 let path = Path::new(trimmed);
492 let maybe_file_name_with_row_col = path.file_name().unwrap_or_default().to_string_lossy();
493 if maybe_file_name_with_row_col.is_empty() {
494 return Self {
495 path: Path::new(s).to_path_buf(),
496 row: None,
497 column: None,
498 };
499 }
500
501 // Let's avoid repeated init cost on this. It is subject to thread contention, but
502 // so far this code isn't called from multiple hot paths. Getting contention here
503 // in the future seems unlikely.
504 static SUFFIX_RE: LazyLock<Regex> =
505 LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());
506 match SUFFIX_RE
507 .captures(&maybe_file_name_with_row_col)
508 .map(|caps| caps.extract())
509 {
510 Some((_, [file_name, maybe_row, maybe_column])) => {
511 let row = maybe_row.parse::<u32>().ok();
512 let column = maybe_column.parse::<u32>().ok();
513
514 let suffix_length = maybe_file_name_with_row_col.len() - file_name.len();
515 let path_without_suffix = &trimmed[..trimmed.len() - suffix_length];
516
517 Self {
518 path: Path::new(path_without_suffix).to_path_buf(),
519 row,
520 column,
521 }
522 }
523 None => {
524 // The `ROW_COL_CAPTURE_REGEX` deals with separated digits only,
525 // but in reality there could be `foo/bar.py:22:in` inputs which we want to match too.
526 // The regex mentioned is not very extendable with "digit or random string" checks, so do this here instead.
527 let delimiter = ':';
528 let mut path_parts = s
529 .rsplitn(3, delimiter)
530 .collect::<Vec<_>>()
531 .into_iter()
532 .rev()
533 .fuse();
534 let mut path_string = path_parts.next().expect("rsplitn should have the rest of the string as its last parameter that we reversed").to_owned();
535 let mut row = None;
536 let mut column = None;
537 if let Some(maybe_row) = path_parts.next() {
538 if let Ok(parsed_row) = maybe_row.parse::<u32>() {
539 row = Some(parsed_row);
540 if let Some(parsed_column) = path_parts
541 .next()
542 .and_then(|maybe_col| maybe_col.parse::<u32>().ok())
543 {
544 column = Some(parsed_column);
545 }
546 } else {
547 path_string.push(delimiter);
548 path_string.push_str(maybe_row);
549 }
550 }
551 for split in path_parts {
552 path_string.push(delimiter);
553 path_string.push_str(split);
554 }
555
556 Self {
557 path: PathBuf::from(path_string),
558 row,
559 column,
560 }
561 }
562 }
563 }
564
565 pub fn map_path<E>(
566 self,
567 mapping: impl FnOnce(PathBuf) -> Result<PathBuf, E>,
568 ) -> Result<PathWithPosition, E> {
569 Ok(PathWithPosition {
570 path: mapping(self.path)?,
571 row: self.row,
572 column: self.column,
573 })
574 }
575
576 pub fn to_string(&self, path_to_string: impl Fn(&PathBuf) -> String) -> String {
577 let path_string = path_to_string(&self.path);
578 if let Some(row) = self.row {
579 if let Some(column) = self.column {
580 format!("{path_string}:{row}:{column}")
581 } else {
582 format!("{path_string}:{row}")
583 }
584 } else {
585 path_string
586 }
587 }
588}
589
590#[derive(Clone, Debug, Default)]
591pub struct PathMatcher {
592 sources: Vec<String>,
593 glob: GlobSet,
594}
595
596// impl std::fmt::Display for PathMatcher {
597// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
598// self.sources.fmt(f)
599// }
600// }
601
602impl PartialEq for PathMatcher {
603 fn eq(&self, other: &Self) -> bool {
604 self.sources.eq(&other.sources)
605 }
606}
607
608impl Eq for PathMatcher {}
609
610impl PathMatcher {
611 pub fn new(globs: impl IntoIterator<Item = impl AsRef<str>>) -> Result<Self, globset::Error> {
612 let globs = globs
613 .into_iter()
614 .map(|as_str| Glob::new(as_str.as_ref()))
615 .collect::<Result<Vec<_>, _>>()?;
616 let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
617 let mut glob_builder = GlobSetBuilder::new();
618 for single_glob in globs {
619 glob_builder.add(single_glob);
620 }
621 let glob = glob_builder.build()?;
622 Ok(PathMatcher { glob, sources })
623 }
624
625 pub fn sources(&self) -> &[String] {
626 &self.sources
627 }
628
629 pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
630 let other_path = other.as_ref();
631 self.sources.iter().any(|source| {
632 let as_bytes = other_path.as_os_str().as_encoded_bytes();
633 as_bytes.starts_with(source.as_bytes()) || as_bytes.ends_with(source.as_bytes())
634 }) || self.glob.is_match(other_path)
635 || self.check_with_end_separator(other_path)
636 }
637
638 fn check_with_end_separator(&self, path: &Path) -> bool {
639 let path_str = path.to_string_lossy();
640 let separator = std::path::MAIN_SEPARATOR_STR;
641 if path_str.ends_with(separator) {
642 false
643 } else {
644 self.glob.is_match(path_str.to_string() + separator)
645 }
646 }
647}
648
649/// Custom character comparison that prioritizes lowercase for same letters
650fn compare_chars(a: char, b: char) -> Ordering {
651 // First compare case-insensitive
652 match a.to_ascii_lowercase().cmp(&b.to_ascii_lowercase()) {
653 Ordering::Equal => {
654 // If same letter, prioritize lowercase (lowercase < uppercase)
655 match (a.is_ascii_lowercase(), b.is_ascii_lowercase()) {
656 (true, false) => Ordering::Less, // lowercase comes first
657 (false, true) => Ordering::Greater, // uppercase comes after
658 _ => Ordering::Equal, // both same case or both non-ascii
659 }
660 }
661 other => other,
662 }
663}
664
665/// Compares two sequences of consecutive digits for natural sorting.
666///
667/// This function is a core component of natural sorting that handles numeric comparison
668/// in a way that feels natural to humans. It extracts and compares consecutive digit
669/// sequences from two iterators, handling various cases like leading zeros and very large numbers.
670///
671/// # Behavior
672///
673/// The function implements the following comparison rules:
674/// 1. Different numeric values: Compares by actual numeric value (e.g., "2" < "10")
675/// 2. Leading zeros: When values are equal, longer sequence wins (e.g., "002" > "2")
676/// 3. Large numbers: Falls back to string comparison for numbers that would overflow u128
677///
678/// # Examples
679///
680/// ```text
681/// "1" vs "2" -> Less (different values)
682/// "2" vs "10" -> Less (numeric comparison)
683/// "002" vs "2" -> Greater (leading zeros)
684/// "10" vs "010" -> Less (leading zeros)
685/// "999..." vs "1000..." -> Less (large number comparison)
686/// ```
687///
688/// # Implementation Details
689///
690/// 1. Extracts consecutive digits into strings
691/// 2. Compares sequence lengths for leading zero handling
692/// 3. For equal lengths, compares digit by digit
693/// 4. For different lengths:
694/// - Attempts numeric comparison first (for numbers up to 2^128 - 1)
695/// - Falls back to string comparison if numbers would overflow
696///
697/// The function advances both iterators past their respective numeric sequences,
698/// regardless of the comparison result.
699fn compare_numeric_segments<I>(
700 a_iter: &mut std::iter::Peekable<I>,
701 b_iter: &mut std::iter::Peekable<I>,
702) -> Ordering
703where
704 I: Iterator<Item = char>,
705{
706 // Collect all consecutive digits into strings
707 let mut a_num_str = String::new();
708 let mut b_num_str = String::new();
709
710 while let Some(&c) = a_iter.peek() {
711 if !c.is_ascii_digit() {
712 break;
713 }
714
715 a_num_str.push(c);
716 a_iter.next();
717 }
718
719 while let Some(&c) = b_iter.peek() {
720 if !c.is_ascii_digit() {
721 break;
722 }
723
724 b_num_str.push(c);
725 b_iter.next();
726 }
727
728 // First compare lengths (handle leading zeros)
729 match a_num_str.len().cmp(&b_num_str.len()) {
730 Ordering::Equal => {
731 // Same length, compare digit by digit
732 match a_num_str.cmp(&b_num_str) {
733 Ordering::Equal => Ordering::Equal,
734 ordering => ordering,
735 }
736 }
737
738 // Different lengths but same value means leading zeros
739 ordering => {
740 // Try parsing as numbers first
741 if let (Ok(a_val), Ok(b_val)) = (a_num_str.parse::<u128>(), b_num_str.parse::<u128>()) {
742 match a_val.cmp(&b_val) {
743 Ordering::Equal => ordering, // Same value, longer one is greater (leading zeros)
744 ord => ord,
745 }
746 } else {
747 // If parsing fails (overflow), compare as strings
748 a_num_str.cmp(&b_num_str)
749 }
750 }
751 }
752}
753
754/// Performs natural sorting comparison between two strings.
755///
756/// Natural sorting is an ordering that handles numeric sequences in a way that matches human expectations.
757/// For example, "file2" comes before "file10" (unlike standard lexicographic sorting).
758///
759/// # Characteristics
760///
761/// * Case-sensitive with lowercase priority: When comparing same letters, lowercase comes before uppercase
762/// * Numbers are compared by numeric value, not character by character
763/// * Leading zeros affect ordering when numeric values are equal
764/// * Can handle numbers larger than u128::MAX (falls back to string comparison)
765///
766/// # Algorithm
767///
768/// The function works by:
769/// 1. Processing strings character by character
770/// 2. When encountering digits, treating consecutive digits as a single number
771/// 3. Comparing numbers by their numeric value rather than lexicographically
772/// 4. For non-numeric characters, using case-sensitive comparison with lowercase priority
773fn natural_sort(a: &str, b: &str) -> Ordering {
774 let mut a_iter = a.chars().peekable();
775 let mut b_iter = b.chars().peekable();
776
777 loop {
778 match (a_iter.peek(), b_iter.peek()) {
779 (None, None) => return Ordering::Equal,
780 (None, _) => return Ordering::Less,
781 (_, None) => return Ordering::Greater,
782 (Some(&a_char), Some(&b_char)) => {
783 if a_char.is_ascii_digit() && b_char.is_ascii_digit() {
784 match compare_numeric_segments(&mut a_iter, &mut b_iter) {
785 Ordering::Equal => continue,
786 ordering => return ordering,
787 }
788 } else {
789 match compare_chars(a_char, b_char) {
790 Ordering::Equal => {
791 a_iter.next();
792 b_iter.next();
793 }
794 ordering => return ordering,
795 }
796 }
797 }
798 }
799 }
800}
801
802pub fn compare_paths(
803 (path_a, a_is_file): (&Path, bool),
804 (path_b, b_is_file): (&Path, bool),
805) -> Ordering {
806 let mut components_a = path_a.components().peekable();
807 let mut components_b = path_b.components().peekable();
808
809 loop {
810 match (components_a.next(), components_b.next()) {
811 (Some(component_a), Some(component_b)) => {
812 let a_is_file = components_a.peek().is_none() && a_is_file;
813 let b_is_file = components_b.peek().is_none() && b_is_file;
814
815 let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
816 let path_a = Path::new(component_a.as_os_str());
817 let path_string_a = if a_is_file {
818 path_a.file_stem()
819 } else {
820 path_a.file_name()
821 }
822 .map(|s| s.to_string_lossy());
823
824 let path_b = Path::new(component_b.as_os_str());
825 let path_string_b = if b_is_file {
826 path_b.file_stem()
827 } else {
828 path_b.file_name()
829 }
830 .map(|s| s.to_string_lossy());
831
832 let compare_components = match (path_string_a, path_string_b) {
833 (Some(a), Some(b)) => natural_sort(&a, &b),
834 (Some(_), None) => Ordering::Greater,
835 (None, Some(_)) => Ordering::Less,
836 (None, None) => Ordering::Equal,
837 };
838
839 compare_components.then_with(|| {
840 if a_is_file && b_is_file {
841 let ext_a = path_a.extension().unwrap_or_default();
842 let ext_b = path_b.extension().unwrap_or_default();
843 ext_a.cmp(ext_b)
844 } else {
845 Ordering::Equal
846 }
847 })
848 });
849
850 if !ordering.is_eq() {
851 return ordering;
852 }
853 }
854 (Some(_), None) => break Ordering::Greater,
855 (None, Some(_)) => break Ordering::Less,
856 (None, None) => break Ordering::Equal,
857 }
858 }
859}
860
861#[cfg(test)]
862mod tests {
863 use super::*;
864
865 #[test]
866 fn compare_paths_with_dots() {
867 let mut paths = vec![
868 (Path::new("test_dirs"), false),
869 (Path::new("test_dirs/1.46"), false),
870 (Path::new("test_dirs/1.46/bar_1"), true),
871 (Path::new("test_dirs/1.46/bar_2"), true),
872 (Path::new("test_dirs/1.45"), false),
873 (Path::new("test_dirs/1.45/foo_2"), true),
874 (Path::new("test_dirs/1.45/foo_1"), true),
875 ];
876 paths.sort_by(|&a, &b| compare_paths(a, b));
877 assert_eq!(
878 paths,
879 vec![
880 (Path::new("test_dirs"), false),
881 (Path::new("test_dirs/1.45"), false),
882 (Path::new("test_dirs/1.45/foo_1"), true),
883 (Path::new("test_dirs/1.45/foo_2"), true),
884 (Path::new("test_dirs/1.46"), false),
885 (Path::new("test_dirs/1.46/bar_1"), true),
886 (Path::new("test_dirs/1.46/bar_2"), true),
887 ]
888 );
889 let mut paths = vec![
890 (Path::new("root1/one.txt"), true),
891 (Path::new("root1/one.two.txt"), true),
892 ];
893 paths.sort_by(|&a, &b| compare_paths(a, b));
894 assert_eq!(
895 paths,
896 vec![
897 (Path::new("root1/one.txt"), true),
898 (Path::new("root1/one.two.txt"), true),
899 ]
900 );
901 }
902
903 #[test]
904 fn compare_paths_with_same_name_different_extensions() {
905 let mut paths = vec![
906 (Path::new("test_dirs/file.rs"), true),
907 (Path::new("test_dirs/file.txt"), true),
908 (Path::new("test_dirs/file.md"), true),
909 (Path::new("test_dirs/file"), true),
910 (Path::new("test_dirs/file.a"), true),
911 ];
912 paths.sort_by(|&a, &b| compare_paths(a, b));
913 assert_eq!(
914 paths,
915 vec![
916 (Path::new("test_dirs/file"), true),
917 (Path::new("test_dirs/file.a"), true),
918 (Path::new("test_dirs/file.md"), true),
919 (Path::new("test_dirs/file.rs"), true),
920 (Path::new("test_dirs/file.txt"), true),
921 ]
922 );
923 }
924
925 #[test]
926 fn compare_paths_case_semi_sensitive() {
927 let mut paths = vec![
928 (Path::new("test_DIRS"), false),
929 (Path::new("test_DIRS/foo_1"), true),
930 (Path::new("test_DIRS/foo_2"), true),
931 (Path::new("test_DIRS/bar"), true),
932 (Path::new("test_DIRS/BAR"), true),
933 (Path::new("test_dirs"), false),
934 (Path::new("test_dirs/foo_1"), true),
935 (Path::new("test_dirs/foo_2"), true),
936 (Path::new("test_dirs/bar"), true),
937 (Path::new("test_dirs/BAR"), true),
938 ];
939 paths.sort_by(|&a, &b| compare_paths(a, b));
940 assert_eq!(
941 paths,
942 vec![
943 (Path::new("test_dirs"), false),
944 (Path::new("test_dirs/bar"), true),
945 (Path::new("test_dirs/BAR"), true),
946 (Path::new("test_dirs/foo_1"), true),
947 (Path::new("test_dirs/foo_2"), true),
948 (Path::new("test_DIRS"), false),
949 (Path::new("test_DIRS/bar"), true),
950 (Path::new("test_DIRS/BAR"), true),
951 (Path::new("test_DIRS/foo_1"), true),
952 (Path::new("test_DIRS/foo_2"), true),
953 ]
954 );
955 }
956
957 #[test]
958 fn path_with_position_parse_posix_path() {
959 // Test POSIX filename edge cases
960 // Read more at https://en.wikipedia.org/wiki/Filename
961 assert_eq!(
962 PathWithPosition::parse_str("test_file"),
963 PathWithPosition {
964 path: PathBuf::from("test_file"),
965 row: None,
966 column: None
967 }
968 );
969
970 assert_eq!(
971 PathWithPosition::parse_str("a:bc:.zip:1"),
972 PathWithPosition {
973 path: PathBuf::from("a:bc:.zip"),
974 row: Some(1),
975 column: None
976 }
977 );
978
979 assert_eq!(
980 PathWithPosition::parse_str("one.second.zip:1"),
981 PathWithPosition {
982 path: PathBuf::from("one.second.zip"),
983 row: Some(1),
984 column: None
985 }
986 );
987
988 // Trim off trailing `:`s for otherwise valid input.
989 assert_eq!(
990 PathWithPosition::parse_str("test_file:10:1:"),
991 PathWithPosition {
992 path: PathBuf::from("test_file"),
993 row: Some(10),
994 column: Some(1)
995 }
996 );
997
998 assert_eq!(
999 PathWithPosition::parse_str("test_file.rs:"),
1000 PathWithPosition {
1001 path: PathBuf::from("test_file.rs:"),
1002 row: None,
1003 column: None
1004 }
1005 );
1006
1007 assert_eq!(
1008 PathWithPosition::parse_str("test_file.rs:1:"),
1009 PathWithPosition {
1010 path: PathBuf::from("test_file.rs"),
1011 row: Some(1),
1012 column: None
1013 }
1014 );
1015
1016 assert_eq!(
1017 PathWithPosition::parse_str("ab\ncd"),
1018 PathWithPosition {
1019 path: PathBuf::from("ab\ncd"),
1020 row: None,
1021 column: None
1022 }
1023 );
1024
1025 assert_eq!(
1026 PathWithPosition::parse_str("👋\nab"),
1027 PathWithPosition {
1028 path: PathBuf::from("👋\nab"),
1029 row: None,
1030 column: None
1031 }
1032 );
1033
1034 assert_eq!(
1035 PathWithPosition::parse_str("Types.hs:(617,9)-(670,28):"),
1036 PathWithPosition {
1037 path: PathBuf::from("Types.hs"),
1038 row: Some(617),
1039 column: Some(9),
1040 }
1041 );
1042 }
1043
1044 #[test]
1045 #[cfg(not(target_os = "windows"))]
1046 fn path_with_position_parse_posix_path_with_suffix() {
1047 assert_eq!(
1048 PathWithPosition::parse_str("foo/bar:34:in"),
1049 PathWithPosition {
1050 path: PathBuf::from("foo/bar"),
1051 row: Some(34),
1052 column: None,
1053 }
1054 );
1055 assert_eq!(
1056 PathWithPosition::parse_str("foo/bar.rs:1902:::15:"),
1057 PathWithPosition {
1058 path: PathBuf::from("foo/bar.rs:1902"),
1059 row: Some(15),
1060 column: None
1061 }
1062 );
1063
1064 assert_eq!(
1065 PathWithPosition::parse_str("app-editors:zed-0.143.6:20240710-201212.log:34:"),
1066 PathWithPosition {
1067 path: PathBuf::from("app-editors:zed-0.143.6:20240710-201212.log"),
1068 row: Some(34),
1069 column: None,
1070 }
1071 );
1072
1073 assert_eq!(
1074 PathWithPosition::parse_str("crates/file_finder/src/file_finder.rs:1902:13:"),
1075 PathWithPosition {
1076 path: PathBuf::from("crates/file_finder/src/file_finder.rs"),
1077 row: Some(1902),
1078 column: Some(13),
1079 }
1080 );
1081
1082 assert_eq!(
1083 PathWithPosition::parse_str("crate/utils/src/test:today.log:34"),
1084 PathWithPosition {
1085 path: PathBuf::from("crate/utils/src/test:today.log"),
1086 row: Some(34),
1087 column: None,
1088 }
1089 );
1090 assert_eq!(
1091 PathWithPosition::parse_str("/testing/out/src/file_finder.odin(7:15)"),
1092 PathWithPosition {
1093 path: PathBuf::from("/testing/out/src/file_finder.odin"),
1094 row: Some(7),
1095 column: Some(15),
1096 }
1097 );
1098 }
1099
1100 #[test]
1101 #[cfg(target_os = "windows")]
1102 fn path_with_position_parse_windows_path() {
1103 assert_eq!(
1104 PathWithPosition::parse_str("crates\\utils\\paths.rs"),
1105 PathWithPosition {
1106 path: PathBuf::from("crates\\utils\\paths.rs"),
1107 row: None,
1108 column: None
1109 }
1110 );
1111
1112 assert_eq!(
1113 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs"),
1114 PathWithPosition {
1115 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1116 row: None,
1117 column: None
1118 }
1119 );
1120 }
1121
1122 #[test]
1123 #[cfg(target_os = "windows")]
1124 fn path_with_position_parse_windows_path_with_suffix() {
1125 assert_eq!(
1126 PathWithPosition::parse_str("crates\\utils\\paths.rs:101"),
1127 PathWithPosition {
1128 path: PathBuf::from("crates\\utils\\paths.rs"),
1129 row: Some(101),
1130 column: None
1131 }
1132 );
1133
1134 assert_eq!(
1135 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1:20"),
1136 PathWithPosition {
1137 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1138 row: Some(1),
1139 column: Some(20)
1140 }
1141 );
1142
1143 assert_eq!(
1144 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13)"),
1145 PathWithPosition {
1146 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1147 row: Some(1902),
1148 column: Some(13)
1149 }
1150 );
1151
1152 // Trim off trailing `:`s for otherwise valid input.
1153 assert_eq!(
1154 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:"),
1155 PathWithPosition {
1156 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1157 row: Some(1902),
1158 column: Some(13)
1159 }
1160 );
1161
1162 assert_eq!(
1163 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:"),
1164 PathWithPosition {
1165 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1166 row: Some(13),
1167 column: Some(15)
1168 }
1169 );
1170
1171 assert_eq!(
1172 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:"),
1173 PathWithPosition {
1174 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1175 row: Some(15),
1176 column: None
1177 }
1178 );
1179
1180 assert_eq!(
1181 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902,13):"),
1182 PathWithPosition {
1183 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1184 row: Some(1902),
1185 column: Some(13),
1186 }
1187 );
1188
1189 assert_eq!(
1190 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902):"),
1191 PathWithPosition {
1192 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1193 row: Some(1902),
1194 column: None,
1195 }
1196 );
1197
1198 assert_eq!(
1199 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs:1902:13:"),
1200 PathWithPosition {
1201 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1202 row: Some(1902),
1203 column: Some(13),
1204 }
1205 );
1206
1207 assert_eq!(
1208 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13):"),
1209 PathWithPosition {
1210 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1211 row: Some(1902),
1212 column: Some(13),
1213 }
1214 );
1215
1216 assert_eq!(
1217 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902):"),
1218 PathWithPosition {
1219 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1220 row: Some(1902),
1221 column: None,
1222 }
1223 );
1224
1225 assert_eq!(
1226 PathWithPosition::parse_str("crates/utils/paths.rs:101"),
1227 PathWithPosition {
1228 path: PathBuf::from("crates\\utils\\paths.rs"),
1229 row: Some(101),
1230 column: None,
1231 }
1232 );
1233 }
1234
1235 #[test]
1236 fn test_path_compact() {
1237 let path: PathBuf = [
1238 home_dir().to_string_lossy().to_string(),
1239 "some_file.txt".to_string(),
1240 ]
1241 .iter()
1242 .collect();
1243 if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
1244 assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
1245 } else {
1246 assert_eq!(path.compact().to_str(), path.to_str());
1247 }
1248 }
1249
1250 #[test]
1251 fn test_extension_or_hidden_file_name() {
1252 // No dots in name
1253 let path = Path::new("/a/b/c/file_name.rs");
1254 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1255
1256 // Single dot in name
1257 let path = Path::new("/a/b/c/file.name.rs");
1258 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1259
1260 // Multiple dots in name
1261 let path = Path::new("/a/b/c/long.file.name.rs");
1262 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1263
1264 // Hidden file, no extension
1265 let path = Path::new("/a/b/c/.gitignore");
1266 assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
1267
1268 // Hidden file, with extension
1269 let path = Path::new("/a/b/c/.eslintrc.js");
1270 assert_eq!(path.extension_or_hidden_file_name(), Some("eslintrc.js"));
1271 }
1272
1273 #[test]
1274 fn edge_of_glob() {
1275 let path = Path::new("/work/node_modules");
1276 let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
1277 assert!(
1278 path_matcher.is_match(path),
1279 "Path matcher should match {path:?}"
1280 );
1281 }
1282
1283 #[test]
1284 fn project_search() {
1285 let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
1286 let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
1287 assert!(
1288 path_matcher.is_match(path),
1289 "Path matcher should match {path:?}"
1290 );
1291 }
1292
1293 #[test]
1294 #[cfg(target_os = "windows")]
1295 fn test_sanitized_path() {
1296 let path = Path::new("C:\\Users\\someone\\test_file.rs");
1297 let sanitized_path = SanitizedPath::new(path);
1298 assert_eq!(
1299 sanitized_path.to_string(),
1300 "C:\\Users\\someone\\test_file.rs"
1301 );
1302
1303 let path = Path::new("\\\\?\\C:\\Users\\someone\\test_file.rs");
1304 let sanitized_path = SanitizedPath::new(path);
1305 assert_eq!(
1306 sanitized_path.to_string(),
1307 "C:\\Users\\someone\\test_file.rs"
1308 );
1309 }
1310
1311 #[test]
1312 fn test_compare_numeric_segments() {
1313 // Helper function to create peekable iterators and test
1314 fn compare(a: &str, b: &str) -> Ordering {
1315 let mut a_iter = a.chars().peekable();
1316 let mut b_iter = b.chars().peekable();
1317
1318 let result = compare_numeric_segments(&mut a_iter, &mut b_iter);
1319
1320 // Verify iterators advanced correctly
1321 assert!(
1322 !a_iter.next().is_some_and(|c| c.is_ascii_digit()),
1323 "Iterator a should have consumed all digits"
1324 );
1325 assert!(
1326 !b_iter.next().is_some_and(|c| c.is_ascii_digit()),
1327 "Iterator b should have consumed all digits"
1328 );
1329
1330 result
1331 }
1332
1333 // Basic numeric comparisons
1334 assert_eq!(compare("0", "0"), Ordering::Equal);
1335 assert_eq!(compare("1", "2"), Ordering::Less);
1336 assert_eq!(compare("9", "10"), Ordering::Less);
1337 assert_eq!(compare("10", "9"), Ordering::Greater);
1338 assert_eq!(compare("99", "100"), Ordering::Less);
1339
1340 // Leading zeros
1341 assert_eq!(compare("0", "00"), Ordering::Less);
1342 assert_eq!(compare("00", "0"), Ordering::Greater);
1343 assert_eq!(compare("01", "1"), Ordering::Greater);
1344 assert_eq!(compare("001", "1"), Ordering::Greater);
1345 assert_eq!(compare("001", "01"), Ordering::Greater);
1346
1347 // Same value different representation
1348 assert_eq!(compare("000100", "100"), Ordering::Greater);
1349 assert_eq!(compare("100", "0100"), Ordering::Less);
1350 assert_eq!(compare("0100", "00100"), Ordering::Less);
1351
1352 // Large numbers
1353 assert_eq!(compare("9999999999", "10000000000"), Ordering::Less);
1354 assert_eq!(
1355 compare(
1356 "340282366920938463463374607431768211455", // u128::MAX
1357 "340282366920938463463374607431768211456"
1358 ),
1359 Ordering::Less
1360 );
1361 assert_eq!(
1362 compare(
1363 "340282366920938463463374607431768211456", // > u128::MAX
1364 "340282366920938463463374607431768211455"
1365 ),
1366 Ordering::Greater
1367 );
1368
1369 // Iterator advancement verification
1370 let mut a_iter = "123abc".chars().peekable();
1371 let mut b_iter = "456def".chars().peekable();
1372
1373 compare_numeric_segments(&mut a_iter, &mut b_iter);
1374
1375 assert_eq!(a_iter.collect::<String>(), "abc");
1376 assert_eq!(b_iter.collect::<String>(), "def");
1377 }
1378
1379 #[test]
1380 fn test_natural_sort() {
1381 // Basic alphanumeric
1382 assert_eq!(natural_sort("a", "b"), Ordering::Less);
1383 assert_eq!(natural_sort("b", "a"), Ordering::Greater);
1384 assert_eq!(natural_sort("a", "a"), Ordering::Equal);
1385
1386 // Case sensitivity
1387 assert_eq!(natural_sort("a", "A"), Ordering::Less);
1388 assert_eq!(natural_sort("A", "a"), Ordering::Greater);
1389 assert_eq!(natural_sort("aA", "aa"), Ordering::Greater);
1390 assert_eq!(natural_sort("aa", "aA"), Ordering::Less);
1391
1392 // Numbers
1393 assert_eq!(natural_sort("1", "2"), Ordering::Less);
1394 assert_eq!(natural_sort("2", "10"), Ordering::Less);
1395 assert_eq!(natural_sort("02", "10"), Ordering::Less);
1396 assert_eq!(natural_sort("02", "2"), Ordering::Greater);
1397
1398 // Mixed alphanumeric
1399 assert_eq!(natural_sort("a1", "a2"), Ordering::Less);
1400 assert_eq!(natural_sort("a2", "a10"), Ordering::Less);
1401 assert_eq!(natural_sort("a02", "a2"), Ordering::Greater);
1402 assert_eq!(natural_sort("a1b", "a1c"), Ordering::Less);
1403
1404 // Multiple numeric segments
1405 assert_eq!(natural_sort("1a2", "1a10"), Ordering::Less);
1406 assert_eq!(natural_sort("1a10", "1a2"), Ordering::Greater);
1407 assert_eq!(natural_sort("2a1", "10a1"), Ordering::Less);
1408
1409 // Special characters
1410 assert_eq!(natural_sort("a-1", "a-2"), Ordering::Less);
1411 assert_eq!(natural_sort("a_1", "a_2"), Ordering::Less);
1412 assert_eq!(natural_sort("a.1", "a.2"), Ordering::Less);
1413
1414 // Unicode
1415 assert_eq!(natural_sort("文1", "文2"), Ordering::Less);
1416 assert_eq!(natural_sort("文2", "文10"), Ordering::Less);
1417 assert_eq!(natural_sort("🔤1", "🔤2"), Ordering::Less);
1418
1419 // Empty and special cases
1420 assert_eq!(natural_sort("", ""), Ordering::Equal);
1421 assert_eq!(natural_sort("", "a"), Ordering::Less);
1422 assert_eq!(natural_sort("a", ""), Ordering::Greater);
1423 assert_eq!(natural_sort(" ", " "), Ordering::Less);
1424
1425 // Mixed everything
1426 assert_eq!(natural_sort("File-1.txt", "File-2.txt"), Ordering::Less);
1427 assert_eq!(natural_sort("File-02.txt", "File-2.txt"), Ordering::Greater);
1428 assert_eq!(natural_sort("File-2.txt", "File-10.txt"), Ordering::Less);
1429 assert_eq!(natural_sort("File_A1", "File_A2"), Ordering::Less);
1430 assert_eq!(natural_sort("File_a1", "File_A1"), Ordering::Less);
1431 }
1432
1433 #[test]
1434 fn test_compare_paths() {
1435 // Helper function for cleaner tests
1436 fn compare(a: &str, is_a_file: bool, b: &str, is_b_file: bool) -> Ordering {
1437 compare_paths((Path::new(a), is_a_file), (Path::new(b), is_b_file))
1438 }
1439
1440 // Basic path comparison
1441 assert_eq!(compare("a", true, "b", true), Ordering::Less);
1442 assert_eq!(compare("b", true, "a", true), Ordering::Greater);
1443 assert_eq!(compare("a", true, "a", true), Ordering::Equal);
1444
1445 // Files vs Directories
1446 assert_eq!(compare("a", true, "a", false), Ordering::Greater);
1447 assert_eq!(compare("a", false, "a", true), Ordering::Less);
1448 assert_eq!(compare("b", false, "a", true), Ordering::Less);
1449
1450 // Extensions
1451 assert_eq!(compare("a.txt", true, "a.md", true), Ordering::Greater);
1452 assert_eq!(compare("a.md", true, "a.txt", true), Ordering::Less);
1453 assert_eq!(compare("a", true, "a.txt", true), Ordering::Less);
1454
1455 // Nested paths
1456 assert_eq!(compare("dir/a", true, "dir/b", true), Ordering::Less);
1457 assert_eq!(compare("dir1/a", true, "dir2/a", true), Ordering::Less);
1458 assert_eq!(compare("dir/sub/a", true, "dir/a", true), Ordering::Less);
1459
1460 // Case sensitivity in paths
1461 assert_eq!(
1462 compare("Dir/file", true, "dir/file", true),
1463 Ordering::Greater
1464 );
1465 assert_eq!(
1466 compare("dir/File", true, "dir/file", true),
1467 Ordering::Greater
1468 );
1469 assert_eq!(compare("dir/file", true, "Dir/File", true), Ordering::Less);
1470
1471 // Hidden files and special names
1472 assert_eq!(compare(".hidden", true, "visible", true), Ordering::Less);
1473 assert_eq!(compare("_special", true, "normal", true), Ordering::Less);
1474 assert_eq!(compare(".config", false, ".data", false), Ordering::Less);
1475
1476 // Mixed numeric paths
1477 assert_eq!(
1478 compare("dir1/file", true, "dir2/file", true),
1479 Ordering::Less
1480 );
1481 assert_eq!(
1482 compare("dir2/file", true, "dir10/file", true),
1483 Ordering::Less
1484 );
1485 assert_eq!(
1486 compare("dir02/file", true, "dir2/file", true),
1487 Ordering::Greater
1488 );
1489
1490 // Root paths
1491 assert_eq!(compare("/a", true, "/b", true), Ordering::Less);
1492 assert_eq!(compare("/", false, "/a", true), Ordering::Less);
1493
1494 // Complex real-world examples
1495 assert_eq!(
1496 compare("project/src/main.rs", true, "project/src/lib.rs", true),
1497 Ordering::Greater
1498 );
1499 assert_eq!(
1500 compare(
1501 "project/tests/test_1.rs",
1502 true,
1503 "project/tests/test_2.rs",
1504 true
1505 ),
1506 Ordering::Less
1507 );
1508 assert_eq!(
1509 compare(
1510 "project/v1.0.0/README.md",
1511 true,
1512 "project/v1.10.0/README.md",
1513 true
1514 ),
1515 Ordering::Less
1516 );
1517 }
1518
1519 #[test]
1520 fn test_natural_sort_case_sensitivity() {
1521 // Same letter different case - lowercase should come first
1522 assert_eq!(natural_sort("a", "A"), Ordering::Less);
1523 assert_eq!(natural_sort("A", "a"), Ordering::Greater);
1524 assert_eq!(natural_sort("a", "a"), Ordering::Equal);
1525 assert_eq!(natural_sort("A", "A"), Ordering::Equal);
1526
1527 // Mixed case strings
1528 assert_eq!(natural_sort("aaa", "AAA"), Ordering::Less);
1529 assert_eq!(natural_sort("AAA", "aaa"), Ordering::Greater);
1530 assert_eq!(natural_sort("aAa", "AaA"), Ordering::Less);
1531
1532 // Different letters
1533 assert_eq!(natural_sort("a", "b"), Ordering::Less);
1534 assert_eq!(natural_sort("A", "b"), Ordering::Less);
1535 assert_eq!(natural_sort("a", "B"), Ordering::Less);
1536 }
1537
1538 #[test]
1539 fn test_natural_sort_with_numbers() {
1540 // Basic number ordering
1541 assert_eq!(natural_sort("file1", "file2"), Ordering::Less);
1542 assert_eq!(natural_sort("file2", "file10"), Ordering::Less);
1543 assert_eq!(natural_sort("file10", "file2"), Ordering::Greater);
1544
1545 // Numbers in different positions
1546 assert_eq!(natural_sort("1file", "2file"), Ordering::Less);
1547 assert_eq!(natural_sort("file1text", "file2text"), Ordering::Less);
1548 assert_eq!(natural_sort("text1file", "text2file"), Ordering::Less);
1549
1550 // Multiple numbers in string
1551 assert_eq!(natural_sort("file1-2", "file1-10"), Ordering::Less);
1552 assert_eq!(natural_sort("2-1file", "10-1file"), Ordering::Less);
1553
1554 // Leading zeros
1555 assert_eq!(natural_sort("file002", "file2"), Ordering::Greater);
1556 assert_eq!(natural_sort("file002", "file10"), Ordering::Less);
1557
1558 // Very large numbers
1559 assert_eq!(
1560 natural_sort("file999999999999999999999", "file999999999999999999998"),
1561 Ordering::Greater
1562 );
1563
1564 // u128 edge cases
1565
1566 // Numbers near u128::MAX (340,282,366,920,938,463,463,374,607,431,768,211,455)
1567 assert_eq!(
1568 natural_sort(
1569 "file340282366920938463463374607431768211454",
1570 "file340282366920938463463374607431768211455"
1571 ),
1572 Ordering::Less
1573 );
1574
1575 // Equal length numbers that overflow u128
1576 assert_eq!(
1577 natural_sort(
1578 "file340282366920938463463374607431768211456",
1579 "file340282366920938463463374607431768211455"
1580 ),
1581 Ordering::Greater
1582 );
1583
1584 // Different length numbers that overflow u128
1585 assert_eq!(
1586 natural_sort(
1587 "file3402823669209384634633746074317682114560",
1588 "file340282366920938463463374607431768211455"
1589 ),
1590 Ordering::Greater
1591 );
1592
1593 // Leading zeros with numbers near u128::MAX
1594 assert_eq!(
1595 natural_sort(
1596 "file0340282366920938463463374607431768211455",
1597 "file340282366920938463463374607431768211455"
1598 ),
1599 Ordering::Greater
1600 );
1601
1602 // Very large numbers with different lengths (both overflow u128)
1603 assert_eq!(
1604 natural_sort(
1605 "file999999999999999999999999999999999999999999999999",
1606 "file9999999999999999999999999999999999999999999999999"
1607 ),
1608 Ordering::Less
1609 );
1610
1611 // Mixed case with numbers
1612 assert_eq!(natural_sort("File1", "file2"), Ordering::Greater);
1613 assert_eq!(natural_sort("file1", "File2"), Ordering::Less);
1614 }
1615
1616 #[test]
1617 fn test_natural_sort_edge_cases() {
1618 // Empty strings
1619 assert_eq!(natural_sort("", ""), Ordering::Equal);
1620 assert_eq!(natural_sort("", "a"), Ordering::Less);
1621 assert_eq!(natural_sort("a", ""), Ordering::Greater);
1622
1623 // Special characters
1624 assert_eq!(natural_sort("file-1", "file_1"), Ordering::Less);
1625 assert_eq!(natural_sort("file.1", "file_1"), Ordering::Less);
1626 assert_eq!(natural_sort("file 1", "file_1"), Ordering::Less);
1627
1628 // Unicode characters
1629 // 9312 vs 9313
1630 assert_eq!(natural_sort("file①", "file②"), Ordering::Less);
1631 // 9321 vs 9313
1632 assert_eq!(natural_sort("file⑩", "file②"), Ordering::Greater);
1633 // 28450 vs 23383
1634 assert_eq!(natural_sort("file漢", "file字"), Ordering::Greater);
1635
1636 // Mixed alphanumeric with special chars
1637 assert_eq!(natural_sort("file-1a", "file-1b"), Ordering::Less);
1638 assert_eq!(natural_sort("file-1.2", "file-1.10"), Ordering::Less);
1639 assert_eq!(natural_sort("file-1.10", "file-1.2"), Ordering::Greater);
1640 }
1641}