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