paths.rs

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