1use anyhow::Context;
2use globset::{Glob, GlobSet, GlobSetBuilder};
3use itertools::Itertools;
4use regex::Regex;
5use serde::{Deserialize, Serialize};
6use std::cmp::Ordering;
7use std::error::Error;
8use std::fmt::{Display, Formatter};
9use std::mem;
10use std::path::StripPrefixError;
11use std::sync::{Arc, OnceLock};
12use std::{
13 ffi::OsStr,
14 path::{Path, PathBuf},
15 sync::LazyLock,
16};
17
18use crate::rel_path::RelPath;
19
20static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
21
22pub fn home_dir() -> &'static PathBuf {
24 HOME_DIR.get_or_init(|| {
25 if cfg!(any(test, feature = "test-support")) {
26 if cfg!(target_os = "macos") {
27 PathBuf::from("/Users/zed")
28 } else if cfg!(target_os = "windows") {
29 PathBuf::from("C:\\Users\\zed")
30 } else {
31 PathBuf::from("/home/zed")
32 }
33 } else {
34 dirs::home_dir().expect("failed to determine home directory")
35 }
36 })
37}
38
39pub trait PathExt {
40 fn compact(&self) -> PathBuf;
49
50 fn extension_or_hidden_file_name(&self) -> Option<&str>;
52
53 fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
54 where
55 Self: From<&'a Path>,
56 {
57 #[cfg(unix)]
58 {
59 use std::os::unix::prelude::OsStrExt;
60 Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))
61 }
62 #[cfg(windows)]
63 {
64 use tendril::fmt::{Format, WTF8};
65 WTF8::validate(bytes)
66 .then(|| {
67 Self::from(Path::new(unsafe {
69 OsStr::from_encoded_bytes_unchecked(bytes)
70 }))
71 })
72 .with_context(|| format!("Invalid WTF-8 sequence: {bytes:?}"))
73 }
74 }
75
76 fn local_to_wsl(&self) -> Option<PathBuf>;
79
80 fn multiple_extensions(&self) -> Option<String>;
85
86 fn try_shell_safe(&self) -> anyhow::Result<String>;
90}
91
92impl<T: AsRef<Path>> PathExt for T {
93 fn compact(&self) -> PathBuf {
94 if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
95 match self.as_ref().strip_prefix(home_dir().as_path()) {
96 Ok(relative_path) => {
97 let mut shortened_path = PathBuf::new();
98 shortened_path.push("~");
99 shortened_path.push(relative_path);
100 shortened_path
101 }
102 Err(_) => self.as_ref().to_path_buf(),
103 }
104 } else {
105 self.as_ref().to_path_buf()
106 }
107 }
108
109 fn extension_or_hidden_file_name(&self) -> Option<&str> {
110 let path = self.as_ref();
111 let file_name = path.file_name()?.to_str()?;
112 if file_name.starts_with('.') {
113 return file_name.strip_prefix('.');
114 }
115
116 path.extension()
117 .and_then(|e| e.to_str())
118 .or_else(|| path.file_stem()?.to_str())
119 }
120
121 fn local_to_wsl(&self) -> Option<PathBuf> {
122 let mut new_path = std::ffi::OsString::new();
125 for component in self.as_ref().components() {
126 match component {
127 std::path::Component::Prefix(prefix) => {
128 let drive_letter = prefix.as_os_str().to_string_lossy().to_lowercase();
129 let drive_letter = drive_letter.strip_suffix(':')?;
130
131 new_path.push(format!("/mnt/{}", drive_letter));
132 }
133 std::path::Component::RootDir => {}
134 std::path::Component::CurDir => {
135 new_path.push("/.");
136 }
137 std::path::Component::ParentDir => {
138 new_path.push("/..");
139 }
140 std::path::Component::Normal(os_str) => {
141 new_path.push("/");
142 new_path.push(os_str);
143 }
144 }
145 }
146
147 Some(new_path.into())
148 }
149
150 fn multiple_extensions(&self) -> Option<String> {
151 let path = self.as_ref();
152 let file_name = path.file_name()?.to_str()?;
153
154 let parts: Vec<&str> = file_name
155 .split('.')
156 .skip(1)
158 .collect();
159
160 if parts.len() < 2 {
161 return None;
162 }
163
164 Some(parts.into_iter().join("."))
165 }
166
167 fn try_shell_safe(&self) -> anyhow::Result<String> {
168 #[cfg(target_os = "windows")]
169 {
170 Ok(self.as_ref().to_string_lossy().to_string())
171 }
172
173 #[cfg(not(target_os = "windows"))]
174 {
175 let path_str = self
176 .as_ref()
177 .to_str()
178 .with_context(|| "Path contains invalid UTF-8")?;
179
180 Ok(shlex::try_quote(path_str)?.into_owned())
184 }
185 }
186}
187
188pub fn path_ends_with(base: &Path, suffix: &Path) -> bool {
189 strip_path_suffix(base, suffix).is_some()
190}
191
192pub fn strip_path_suffix<'a>(base: &'a Path, suffix: &Path) -> Option<&'a Path> {
193 if let Some(remainder) = base
194 .as_os_str()
195 .as_encoded_bytes()
196 .strip_suffix(suffix.as_os_str().as_encoded_bytes())
197 {
198 if remainder
199 .last()
200 .is_none_or(|last_byte| std::path::is_separator(*last_byte as char))
201 {
202 let os_str = unsafe {
203 OsStr::from_encoded_bytes_unchecked(
204 &remainder[0..remainder.len().saturating_sub(1)],
205 )
206 };
207 return Some(Path::new(os_str));
208 }
209 }
210 None
211}
212
213#[derive(Eq, PartialEq, Hash, Ord, PartialOrd)]
216#[repr(transparent)]
217pub struct SanitizedPath(Path);
218
219impl SanitizedPath {
220 pub fn new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
221 #[cfg(not(target_os = "windows"))]
222 return Self::unchecked_new(path.as_ref());
223
224 #[cfg(target_os = "windows")]
225 return Self::unchecked_new(dunce::simplified(path.as_ref()));
226 }
227
228 pub fn unchecked_new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
229 unsafe { mem::transmute::<&Path, &Self>(path.as_ref()) }
231 }
232
233 pub fn from_arc(path: Arc<Path>) -> Arc<Self> {
234 #[cfg(not(target_os = "windows"))]
236 return unsafe { mem::transmute::<Arc<Path>, Arc<Self>>(path) };
237
238 #[cfg(target_os = "windows")]
240 return Self::new(&path).into();
241 }
242
243 pub fn new_arc<T: AsRef<Path> + ?Sized>(path: &T) -> Arc<Self> {
244 Self::new(path).into()
245 }
246
247 pub fn cast_arc(path: Arc<Self>) -> Arc<Path> {
248 unsafe { mem::transmute::<Arc<Self>, Arc<Path>>(path) }
250 }
251
252 pub fn cast_arc_ref(path: &Arc<Self>) -> &Arc<Path> {
253 unsafe { mem::transmute::<&Arc<Self>, &Arc<Path>>(path) }
255 }
256
257 pub fn starts_with(&self, prefix: &Self) -> bool {
258 self.0.starts_with(&prefix.0)
259 }
260
261 pub fn as_path(&self) -> &Path {
262 &self.0
263 }
264
265 pub fn file_name(&self) -> Option<&std::ffi::OsStr> {
266 self.0.file_name()
267 }
268
269 pub fn extension(&self) -> Option<&std::ffi::OsStr> {
270 self.0.extension()
271 }
272
273 pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
274 self.0.join(path)
275 }
276
277 pub fn parent(&self) -> Option<&Self> {
278 self.0.parent().map(Self::unchecked_new)
279 }
280
281 pub fn strip_prefix(&self, base: &Self) -> Result<&Path, StripPrefixError> {
282 self.0.strip_prefix(base.as_path())
283 }
284
285 pub fn to_str(&self) -> Option<&str> {
286 self.0.to_str()
287 }
288
289 pub fn to_path_buf(&self) -> PathBuf {
290 self.0.to_path_buf()
291 }
292}
293
294impl std::fmt::Debug for SanitizedPath {
295 fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
296 std::fmt::Debug::fmt(&self.0, formatter)
297 }
298}
299
300impl Display for SanitizedPath {
301 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
302 write!(f, "{}", self.0.display())
303 }
304}
305
306impl From<&SanitizedPath> for Arc<SanitizedPath> {
307 fn from(sanitized_path: &SanitizedPath) -> Self {
308 let path: Arc<Path> = sanitized_path.0.into();
309 unsafe { mem::transmute(path) }
311 }
312}
313
314impl From<&SanitizedPath> for PathBuf {
315 fn from(sanitized_path: &SanitizedPath) -> Self {
316 sanitized_path.as_path().into()
317 }
318}
319
320impl AsRef<Path> for SanitizedPath {
321 fn as_ref(&self) -> &Path {
322 &self.0
323 }
324}
325
326#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
327pub enum PathStyle {
328 Posix,
329 Windows,
330}
331
332impl PathStyle {
333 #[cfg(target_os = "windows")]
334 pub const fn local() -> Self {
335 PathStyle::Windows
336 }
337
338 #[cfg(not(target_os = "windows"))]
339 pub const fn local() -> Self {
340 PathStyle::Posix
341 }
342
343 #[inline]
344 pub fn separator(&self) -> &'static str {
345 match self {
346 PathStyle::Posix => "/",
347 PathStyle::Windows => "\\",
348 }
349 }
350
351 pub fn is_windows(&self) -> bool {
352 *self == PathStyle::Windows
353 }
354
355 pub fn join(self, left: impl AsRef<Path>, right: impl AsRef<Path>) -> Option<String> {
356 let right = right.as_ref().to_str()?;
357 if is_absolute(right, self) {
358 return None;
359 }
360 let left = left.as_ref().to_str()?;
361 if left.is_empty() {
362 Some(right.into())
363 } else {
364 Some(format!(
365 "{left}{}{right}",
366 if left.ends_with(self.separator()) {
367 ""
368 } else {
369 self.separator()
370 }
371 ))
372 }
373 }
374
375 pub fn split(self, path_like: &str) -> (Option<&str>, &str) {
376 let Some(pos) = path_like.rfind(self.separator()) else {
377 return (None, path_like);
378 };
379 let filename_start = pos + self.separator().len();
380 (
381 Some(&path_like[..filename_start]),
382 &path_like[filename_start..],
383 )
384 }
385}
386
387#[derive(Debug, Clone)]
388pub struct RemotePathBuf {
389 style: PathStyle,
390 string: String,
391}
392
393impl RemotePathBuf {
394 pub fn new(string: String, style: PathStyle) -> Self {
395 Self { style, string }
396 }
397
398 pub fn from_str(path: &str, style: PathStyle) -> Self {
399 Self::new(path.to_string(), style)
400 }
401
402 pub fn path_style(&self) -> PathStyle {
403 self.style
404 }
405
406 pub fn to_proto(self) -> String {
407 self.string
408 }
409}
410
411impl Display for RemotePathBuf {
412 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413 write!(f, "{}", self.string)
414 }
415}
416
417pub fn is_absolute(path_like: &str, path_style: PathStyle) -> bool {
418 path_like.starts_with('/')
419 || path_style == PathStyle::Windows
420 && (path_like.starts_with('\\')
421 || path_like
422 .chars()
423 .next()
424 .is_some_and(|c| c.is_ascii_alphabetic())
425 && path_like[1..]
426 .strip_prefix(':')
427 .is_some_and(|path| path.starts_with('/') || path.starts_with('\\')))
428}
429
430#[derive(Debug, PartialEq)]
431#[non_exhaustive]
432pub struct NormalizeError;
433
434impl Error for NormalizeError {}
435
436impl std::fmt::Display for NormalizeError {
437 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
438 f.write_str("parent reference `..` points outside of base directory")
439 }
440}
441
442pub fn normalize_lexically(path: &Path) -> Result<PathBuf, NormalizeError> {
459 use std::path::Component;
460
461 let mut lexical = PathBuf::new();
462 let mut iter = path.components().peekable();
463
464 let root = match iter.peek() {
468 Some(Component::ParentDir) => return Err(NormalizeError),
469 Some(p @ Component::RootDir) | Some(p @ Component::CurDir) => {
470 lexical.push(p);
471 iter.next();
472 lexical.as_os_str().len()
473 }
474 Some(Component::Prefix(prefix)) => {
475 lexical.push(prefix.as_os_str());
476 iter.next();
477 if let Some(p @ Component::RootDir) = iter.peek() {
478 lexical.push(p);
479 iter.next();
480 }
481 lexical.as_os_str().len()
482 }
483 None => return Ok(PathBuf::new()),
484 Some(Component::Normal(_)) => 0,
485 };
486
487 for component in iter {
488 match component {
489 Component::RootDir => unreachable!(),
490 Component::Prefix(_) => return Err(NormalizeError),
491 Component::CurDir => continue,
492 Component::ParentDir => {
493 if lexical.as_os_str().len() == root {
495 return Err(NormalizeError);
496 } else {
497 lexical.pop();
498 }
499 }
500 Component::Normal(path) => lexical.push(path),
501 }
502 }
503 Ok(lexical)
504}
505
506pub const FILE_ROW_COLUMN_DELIMITER: char = ':';
508
509const ROW_COL_CAPTURE_REGEX: &str = r"(?xs)
510 ([^\(]+)\:(?:
511 \((\d+)[,:](\d+)\) # filename:(row,column), filename:(row:column)
512 |
513 \((\d+)\)() # filename:(row)
514 )
515 |
516 ([^\(]+)(?:
517 \((\d+)[,:](\d+)\) # filename(row,column), filename(row:column)
518 |
519 \((\d+)\)() # filename(row)
520 )
521 |
522 (.+?)(?:
523 \:+(\d+)\:(\d+)\:*$ # filename:row:column
524 |
525 \:+(\d+)\:*()$ # filename:row
526 |
527 \:+()()$
528 )";
529
530#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
533pub struct PathWithPosition {
534 pub path: PathBuf,
535 pub row: Option<u32>,
536 pub column: Option<u32>,
538}
539
540impl PathWithPosition {
541 pub fn from_path(path: PathBuf) -> Self {
543 Self {
544 path,
545 row: None,
546 column: None,
547 }
548 }
549
550 pub fn parse_str(s: &str) -> Self {
636 let trimmed = s.trim();
637 let path = Path::new(trimmed);
638 let maybe_file_name_with_row_col = path.file_name().unwrap_or_default().to_string_lossy();
639 if maybe_file_name_with_row_col.is_empty() {
640 return Self {
641 path: Path::new(s).to_path_buf(),
642 row: None,
643 column: None,
644 };
645 }
646
647 static SUFFIX_RE: LazyLock<Regex> =
651 LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());
652 match SUFFIX_RE
653 .captures(&maybe_file_name_with_row_col)
654 .map(|caps| caps.extract())
655 {
656 Some((_, [file_name, maybe_row, maybe_column])) => {
657 let row = maybe_row.parse::<u32>().ok();
658 let column = maybe_column.parse::<u32>().ok();
659
660 let suffix_length = maybe_file_name_with_row_col.len() - file_name.len();
661 let path_without_suffix = &trimmed[..trimmed.len() - suffix_length];
662
663 Self {
664 path: Path::new(path_without_suffix).to_path_buf(),
665 row,
666 column,
667 }
668 }
669 None => {
670 let delimiter = ':';
674 let mut path_parts = s
675 .rsplitn(3, delimiter)
676 .collect::<Vec<_>>()
677 .into_iter()
678 .rev()
679 .fuse();
680 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();
681 let mut row = None;
682 let mut column = None;
683 if let Some(maybe_row) = path_parts.next() {
684 if let Ok(parsed_row) = maybe_row.parse::<u32>() {
685 row = Some(parsed_row);
686 if let Some(parsed_column) = path_parts
687 .next()
688 .and_then(|maybe_col| maybe_col.parse::<u32>().ok())
689 {
690 column = Some(parsed_column);
691 }
692 } else {
693 path_string.push(delimiter);
694 path_string.push_str(maybe_row);
695 }
696 }
697 for split in path_parts {
698 path_string.push(delimiter);
699 path_string.push_str(split);
700 }
701
702 Self {
703 path: PathBuf::from(path_string),
704 row,
705 column,
706 }
707 }
708 }
709 }
710
711 pub fn map_path<E>(
712 self,
713 mapping: impl FnOnce(PathBuf) -> Result<PathBuf, E>,
714 ) -> Result<PathWithPosition, E> {
715 Ok(PathWithPosition {
716 path: mapping(self.path)?,
717 row: self.row,
718 column: self.column,
719 })
720 }
721
722 pub fn to_string(&self, path_to_string: impl Fn(&PathBuf) -> String) -> String {
723 let path_string = path_to_string(&self.path);
724 if let Some(row) = self.row {
725 if let Some(column) = self.column {
726 format!("{path_string}:{row}:{column}")
727 } else {
728 format!("{path_string}:{row}")
729 }
730 } else {
731 path_string
732 }
733 }
734}
735
736#[derive(Clone, Debug)]
737pub struct PathMatcher {
738 sources: Vec<String>,
739 glob: GlobSet,
740 path_style: PathStyle,
741}
742
743impl PartialEq for PathMatcher {
750 fn eq(&self, other: &Self) -> bool {
751 self.sources.eq(&other.sources)
752 }
753}
754
755impl Eq for PathMatcher {}
756
757impl PathMatcher {
758 pub fn new(
759 globs: impl IntoIterator<Item = impl AsRef<str>>,
760 path_style: PathStyle,
761 ) -> Result<Self, globset::Error> {
762 let globs = globs
763 .into_iter()
764 .map(|as_str| Glob::new(as_str.as_ref()))
765 .collect::<Result<Vec<_>, _>>()?;
766 let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
767 let mut glob_builder = GlobSetBuilder::new();
768 for single_glob in globs {
769 glob_builder.add(single_glob);
770 }
771 let glob = glob_builder.build()?;
772 Ok(PathMatcher {
773 glob,
774 sources,
775 path_style,
776 })
777 }
778
779 pub fn sources(&self) -> &[String] {
780 &self.sources
781 }
782
783 pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
784 let other_path = other.as_ref();
785 self.sources.iter().any(|source| {
786 let as_bytes = other_path.as_os_str().as_encoded_bytes();
787 as_bytes.starts_with(source.as_bytes()) || as_bytes.ends_with(source.as_bytes())
788 }) || self.glob.is_match(other_path)
789 || self.check_with_end_separator(other_path)
790 }
791
792 fn check_with_end_separator(&self, path: &Path) -> bool {
793 let path_str = path.to_string_lossy();
794 let separator = self.path_style.separator();
795 if path_str.ends_with(separator) {
796 false
797 } else {
798 self.glob.is_match(path_str.to_string() + separator)
799 }
800 }
801}
802
803impl Default for PathMatcher {
804 fn default() -> Self {
805 Self {
806 path_style: PathStyle::local(),
807 glob: GlobSet::empty(),
808 sources: vec![],
809 }
810 }
811}
812
813fn compare_chars(a: char, b: char) -> Ordering {
815 match a.to_ascii_lowercase().cmp(&b.to_ascii_lowercase()) {
817 Ordering::Equal => {
818 match (a.is_ascii_lowercase(), b.is_ascii_lowercase()) {
820 (true, false) => Ordering::Less, (false, true) => Ordering::Greater, _ => Ordering::Equal, }
824 }
825 other => other,
826 }
827}
828
829fn compare_numeric_segments<I>(
864 a_iter: &mut std::iter::Peekable<I>,
865 b_iter: &mut std::iter::Peekable<I>,
866) -> Ordering
867where
868 I: Iterator<Item = char>,
869{
870 let mut a_num_str = String::new();
872 let mut b_num_str = String::new();
873
874 while let Some(&c) = a_iter.peek() {
875 if !c.is_ascii_digit() {
876 break;
877 }
878
879 a_num_str.push(c);
880 a_iter.next();
881 }
882
883 while let Some(&c) = b_iter.peek() {
884 if !c.is_ascii_digit() {
885 break;
886 }
887
888 b_num_str.push(c);
889 b_iter.next();
890 }
891
892 match a_num_str.len().cmp(&b_num_str.len()) {
894 Ordering::Equal => {
895 match a_num_str.cmp(&b_num_str) {
897 Ordering::Equal => Ordering::Equal,
898 ordering => ordering,
899 }
900 }
901
902 ordering => {
904 if let (Ok(a_val), Ok(b_val)) = (a_num_str.parse::<u128>(), b_num_str.parse::<u128>()) {
906 match a_val.cmp(&b_val) {
907 Ordering::Equal => ordering, ord => ord,
909 }
910 } else {
911 a_num_str.cmp(&b_num_str)
913 }
914 }
915 }
916}
917
918fn natural_sort(a: &str, b: &str) -> Ordering {
938 let mut a_iter = a.chars().peekable();
939 let mut b_iter = b.chars().peekable();
940
941 loop {
942 match (a_iter.peek(), b_iter.peek()) {
943 (None, None) => return Ordering::Equal,
944 (None, _) => return Ordering::Less,
945 (_, None) => return Ordering::Greater,
946 (Some(&a_char), Some(&b_char)) => {
947 if a_char.is_ascii_digit() && b_char.is_ascii_digit() {
948 match compare_numeric_segments(&mut a_iter, &mut b_iter) {
949 Ordering::Equal => continue,
950 ordering => return ordering,
951 }
952 } else {
953 match compare_chars(a_char, b_char) {
954 Ordering::Equal => {
955 a_iter.next();
956 b_iter.next();
957 }
958 ordering => return ordering,
959 }
960 }
961 }
962 }
963 }
964}
965pub fn compare_rel_paths(
966 (path_a, a_is_file): (&RelPath, bool),
967 (path_b, b_is_file): (&RelPath, bool),
968) -> Ordering {
969 let mut components_a = path_a.components();
970 let mut components_b = path_b.components();
971
972 fn stem_and_extension(filename: &str) -> (Option<&str>, Option<&str>) {
973 if filename.is_empty() {
974 return (None, None);
975 }
976
977 match filename.rsplit_once('.') {
978 None => (Some(filename), None),
980
981 Some((before, after)) => {
983 if before.is_empty() {
987 (Some(filename), None)
988 } else {
989 (Some(before), Some(after))
991 }
992 }
993 }
994 }
995 loop {
996 match (components_a.next(), components_b.next()) {
997 (Some(component_a), Some(component_b)) => {
998 let a_is_file = a_is_file && components_a.rest().is_empty();
999 let b_is_file = b_is_file && components_b.rest().is_empty();
1000
1001 let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
1002 let (a_stem, a_extension) = a_is_file
1003 .then(|| stem_and_extension(component_a))
1004 .unwrap_or_default();
1005 let path_string_a = if a_is_file { a_stem } else { Some(component_a) };
1006
1007 let (b_stem, b_extension) = b_is_file
1008 .then(|| stem_and_extension(component_b))
1009 .unwrap_or_default();
1010 let path_string_b = if b_is_file { b_stem } else { Some(component_b) };
1011
1012 let compare_components = match (path_string_a, path_string_b) {
1013 (Some(a), Some(b)) => natural_sort(&a, &b),
1014 (Some(_), None) => Ordering::Greater,
1015 (None, Some(_)) => Ordering::Less,
1016 (None, None) => Ordering::Equal,
1017 };
1018
1019 compare_components.then_with(|| {
1020 if a_is_file && b_is_file {
1021 let ext_a = a_extension.unwrap_or_default();
1022 let ext_b = b_extension.unwrap_or_default();
1023 ext_a.cmp(ext_b)
1024 } else {
1025 Ordering::Equal
1026 }
1027 })
1028 });
1029
1030 if !ordering.is_eq() {
1031 return ordering;
1032 }
1033 }
1034 (Some(_), None) => break Ordering::Greater,
1035 (None, Some(_)) => break Ordering::Less,
1036 (None, None) => break Ordering::Equal,
1037 }
1038 }
1039}
1040
1041pub fn compare_paths(
1042 (path_a, a_is_file): (&Path, bool),
1043 (path_b, b_is_file): (&Path, bool),
1044) -> Ordering {
1045 let mut components_a = path_a.components().peekable();
1046 let mut components_b = path_b.components().peekable();
1047
1048 loop {
1049 match (components_a.next(), components_b.next()) {
1050 (Some(component_a), Some(component_b)) => {
1051 let a_is_file = components_a.peek().is_none() && a_is_file;
1052 let b_is_file = components_b.peek().is_none() && b_is_file;
1053
1054 let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
1055 let path_a = Path::new(component_a.as_os_str());
1056 let path_string_a = if a_is_file {
1057 path_a.file_stem()
1058 } else {
1059 path_a.file_name()
1060 }
1061 .map(|s| s.to_string_lossy());
1062
1063 let path_b = Path::new(component_b.as_os_str());
1064 let path_string_b = if b_is_file {
1065 path_b.file_stem()
1066 } else {
1067 path_b.file_name()
1068 }
1069 .map(|s| s.to_string_lossy());
1070
1071 let compare_components = match (path_string_a, path_string_b) {
1072 (Some(a), Some(b)) => natural_sort(&a, &b),
1073 (Some(_), None) => Ordering::Greater,
1074 (None, Some(_)) => Ordering::Less,
1075 (None, None) => Ordering::Equal,
1076 };
1077
1078 compare_components.then_with(|| {
1079 if a_is_file && b_is_file {
1080 let ext_a = path_a.extension().unwrap_or_default();
1081 let ext_b = path_b.extension().unwrap_or_default();
1082 ext_a.cmp(ext_b)
1083 } else {
1084 Ordering::Equal
1085 }
1086 })
1087 });
1088
1089 if !ordering.is_eq() {
1090 return ordering;
1091 }
1092 }
1093 (Some(_), None) => break Ordering::Greater,
1094 (None, Some(_)) => break Ordering::Less,
1095 (None, None) => break Ordering::Equal,
1096 }
1097 }
1098}
1099
1100#[cfg(test)]
1101mod tests {
1102 use super::*;
1103 use util_macros::perf;
1104
1105 #[perf]
1106 fn compare_paths_with_dots() {
1107 let mut paths = vec![
1108 (Path::new("test_dirs"), false),
1109 (Path::new("test_dirs/1.46"), false),
1110 (Path::new("test_dirs/1.46/bar_1"), true),
1111 (Path::new("test_dirs/1.46/bar_2"), true),
1112 (Path::new("test_dirs/1.45"), false),
1113 (Path::new("test_dirs/1.45/foo_2"), true),
1114 (Path::new("test_dirs/1.45/foo_1"), true),
1115 ];
1116 paths.sort_by(|&a, &b| compare_paths(a, b));
1117 assert_eq!(
1118 paths,
1119 vec![
1120 (Path::new("test_dirs"), false),
1121 (Path::new("test_dirs/1.45"), false),
1122 (Path::new("test_dirs/1.45/foo_1"), true),
1123 (Path::new("test_dirs/1.45/foo_2"), true),
1124 (Path::new("test_dirs/1.46"), false),
1125 (Path::new("test_dirs/1.46/bar_1"), true),
1126 (Path::new("test_dirs/1.46/bar_2"), true),
1127 ]
1128 );
1129 let mut paths = vec![
1130 (Path::new("root1/one.txt"), true),
1131 (Path::new("root1/one.two.txt"), true),
1132 ];
1133 paths.sort_by(|&a, &b| compare_paths(a, b));
1134 assert_eq!(
1135 paths,
1136 vec![
1137 (Path::new("root1/one.txt"), true),
1138 (Path::new("root1/one.two.txt"), true),
1139 ]
1140 );
1141 }
1142
1143 #[perf]
1144 fn compare_paths_with_same_name_different_extensions() {
1145 let mut paths = vec![
1146 (Path::new("test_dirs/file.rs"), true),
1147 (Path::new("test_dirs/file.txt"), true),
1148 (Path::new("test_dirs/file.md"), true),
1149 (Path::new("test_dirs/file"), true),
1150 (Path::new("test_dirs/file.a"), true),
1151 ];
1152 paths.sort_by(|&a, &b| compare_paths(a, b));
1153 assert_eq!(
1154 paths,
1155 vec![
1156 (Path::new("test_dirs/file"), true),
1157 (Path::new("test_dirs/file.a"), true),
1158 (Path::new("test_dirs/file.md"), true),
1159 (Path::new("test_dirs/file.rs"), true),
1160 (Path::new("test_dirs/file.txt"), true),
1161 ]
1162 );
1163 }
1164
1165 #[perf]
1166 fn compare_paths_case_semi_sensitive() {
1167 let mut paths = vec![
1168 (Path::new("test_DIRS"), false),
1169 (Path::new("test_DIRS/foo_1"), true),
1170 (Path::new("test_DIRS/foo_2"), true),
1171 (Path::new("test_DIRS/bar"), true),
1172 (Path::new("test_DIRS/BAR"), true),
1173 (Path::new("test_dirs"), false),
1174 (Path::new("test_dirs/foo_1"), true),
1175 (Path::new("test_dirs/foo_2"), true),
1176 (Path::new("test_dirs/bar"), true),
1177 (Path::new("test_dirs/BAR"), true),
1178 ];
1179 paths.sort_by(|&a, &b| compare_paths(a, b));
1180 assert_eq!(
1181 paths,
1182 vec![
1183 (Path::new("test_dirs"), false),
1184 (Path::new("test_dirs/bar"), true),
1185 (Path::new("test_dirs/BAR"), true),
1186 (Path::new("test_dirs/foo_1"), true),
1187 (Path::new("test_dirs/foo_2"), true),
1188 (Path::new("test_DIRS"), false),
1189 (Path::new("test_DIRS/bar"), true),
1190 (Path::new("test_DIRS/BAR"), true),
1191 (Path::new("test_DIRS/foo_1"), true),
1192 (Path::new("test_DIRS/foo_2"), true),
1193 ]
1194 );
1195 }
1196
1197 #[perf]
1198 fn path_with_position_parse_posix_path() {
1199 assert_eq!(
1202 PathWithPosition::parse_str("test_file"),
1203 PathWithPosition {
1204 path: PathBuf::from("test_file"),
1205 row: None,
1206 column: None
1207 }
1208 );
1209
1210 assert_eq!(
1211 PathWithPosition::parse_str("a:bc:.zip:1"),
1212 PathWithPosition {
1213 path: PathBuf::from("a:bc:.zip"),
1214 row: Some(1),
1215 column: None
1216 }
1217 );
1218
1219 assert_eq!(
1220 PathWithPosition::parse_str("one.second.zip:1"),
1221 PathWithPosition {
1222 path: PathBuf::from("one.second.zip"),
1223 row: Some(1),
1224 column: None
1225 }
1226 );
1227
1228 assert_eq!(
1230 PathWithPosition::parse_str("test_file:10:1:"),
1231 PathWithPosition {
1232 path: PathBuf::from("test_file"),
1233 row: Some(10),
1234 column: Some(1)
1235 }
1236 );
1237
1238 assert_eq!(
1239 PathWithPosition::parse_str("test_file.rs:"),
1240 PathWithPosition {
1241 path: PathBuf::from("test_file.rs"),
1242 row: None,
1243 column: None
1244 }
1245 );
1246
1247 assert_eq!(
1248 PathWithPosition::parse_str("test_file.rs:1:"),
1249 PathWithPosition {
1250 path: PathBuf::from("test_file.rs"),
1251 row: Some(1),
1252 column: None
1253 }
1254 );
1255
1256 assert_eq!(
1257 PathWithPosition::parse_str("ab\ncd"),
1258 PathWithPosition {
1259 path: PathBuf::from("ab\ncd"),
1260 row: None,
1261 column: None
1262 }
1263 );
1264
1265 assert_eq!(
1266 PathWithPosition::parse_str("👋\nab"),
1267 PathWithPosition {
1268 path: PathBuf::from("👋\nab"),
1269 row: None,
1270 column: None
1271 }
1272 );
1273
1274 assert_eq!(
1275 PathWithPosition::parse_str("Types.hs:(617,9)-(670,28):"),
1276 PathWithPosition {
1277 path: PathBuf::from("Types.hs"),
1278 row: Some(617),
1279 column: Some(9),
1280 }
1281 );
1282 }
1283
1284 #[perf]
1285 #[cfg(not(target_os = "windows"))]
1286 fn path_with_position_parse_posix_path_with_suffix() {
1287 assert_eq!(
1288 PathWithPosition::parse_str("foo/bar:34:in"),
1289 PathWithPosition {
1290 path: PathBuf::from("foo/bar"),
1291 row: Some(34),
1292 column: None,
1293 }
1294 );
1295 assert_eq!(
1296 PathWithPosition::parse_str("foo/bar.rs:1902:::15:"),
1297 PathWithPosition {
1298 path: PathBuf::from("foo/bar.rs:1902"),
1299 row: Some(15),
1300 column: None
1301 }
1302 );
1303
1304 assert_eq!(
1305 PathWithPosition::parse_str("app-editors:zed-0.143.6:20240710-201212.log:34:"),
1306 PathWithPosition {
1307 path: PathBuf::from("app-editors:zed-0.143.6:20240710-201212.log"),
1308 row: Some(34),
1309 column: None,
1310 }
1311 );
1312
1313 assert_eq!(
1314 PathWithPosition::parse_str("crates/file_finder/src/file_finder.rs:1902:13:"),
1315 PathWithPosition {
1316 path: PathBuf::from("crates/file_finder/src/file_finder.rs"),
1317 row: Some(1902),
1318 column: Some(13),
1319 }
1320 );
1321
1322 assert_eq!(
1323 PathWithPosition::parse_str("crate/utils/src/test:today.log:34"),
1324 PathWithPosition {
1325 path: PathBuf::from("crate/utils/src/test:today.log"),
1326 row: Some(34),
1327 column: None,
1328 }
1329 );
1330 assert_eq!(
1331 PathWithPosition::parse_str("/testing/out/src/file_finder.odin(7:15)"),
1332 PathWithPosition {
1333 path: PathBuf::from("/testing/out/src/file_finder.odin"),
1334 row: Some(7),
1335 column: Some(15),
1336 }
1337 );
1338 }
1339
1340 #[perf]
1341 #[cfg(target_os = "windows")]
1342 fn path_with_position_parse_windows_path() {
1343 assert_eq!(
1344 PathWithPosition::parse_str("crates\\utils\\paths.rs"),
1345 PathWithPosition {
1346 path: PathBuf::from("crates\\utils\\paths.rs"),
1347 row: None,
1348 column: None
1349 }
1350 );
1351
1352 assert_eq!(
1353 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs"),
1354 PathWithPosition {
1355 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1356 row: None,
1357 column: None
1358 }
1359 );
1360 }
1361
1362 #[perf]
1363 #[cfg(target_os = "windows")]
1364 fn path_with_position_parse_windows_path_with_suffix() {
1365 assert_eq!(
1366 PathWithPosition::parse_str("crates\\utils\\paths.rs:101"),
1367 PathWithPosition {
1368 path: PathBuf::from("crates\\utils\\paths.rs"),
1369 row: Some(101),
1370 column: None
1371 }
1372 );
1373
1374 assert_eq!(
1375 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1:20"),
1376 PathWithPosition {
1377 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1378 row: Some(1),
1379 column: Some(20)
1380 }
1381 );
1382
1383 assert_eq!(
1384 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13)"),
1385 PathWithPosition {
1386 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1387 row: Some(1902),
1388 column: Some(13)
1389 }
1390 );
1391
1392 assert_eq!(
1394 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:"),
1395 PathWithPosition {
1396 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1397 row: Some(1902),
1398 column: Some(13)
1399 }
1400 );
1401
1402 assert_eq!(
1403 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:"),
1404 PathWithPosition {
1405 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1406 row: Some(13),
1407 column: Some(15)
1408 }
1409 );
1410
1411 assert_eq!(
1412 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:"),
1413 PathWithPosition {
1414 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1415 row: Some(15),
1416 column: None
1417 }
1418 );
1419
1420 assert_eq!(
1421 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902,13):"),
1422 PathWithPosition {
1423 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1424 row: Some(1902),
1425 column: Some(13),
1426 }
1427 );
1428
1429 assert_eq!(
1430 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902):"),
1431 PathWithPosition {
1432 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1433 row: Some(1902),
1434 column: None,
1435 }
1436 );
1437
1438 assert_eq!(
1439 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs:1902:13:"),
1440 PathWithPosition {
1441 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1442 row: Some(1902),
1443 column: Some(13),
1444 }
1445 );
1446
1447 assert_eq!(
1448 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13):"),
1449 PathWithPosition {
1450 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1451 row: Some(1902),
1452 column: Some(13),
1453 }
1454 );
1455
1456 assert_eq!(
1457 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902):"),
1458 PathWithPosition {
1459 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1460 row: Some(1902),
1461 column: None,
1462 }
1463 );
1464
1465 assert_eq!(
1466 PathWithPosition::parse_str("crates/utils/paths.rs:101"),
1467 PathWithPosition {
1468 path: PathBuf::from("crates\\utils\\paths.rs"),
1469 row: Some(101),
1470 column: None,
1471 }
1472 );
1473 }
1474
1475 #[perf]
1476 fn test_path_compact() {
1477 let path: PathBuf = [
1478 home_dir().to_string_lossy().into_owned(),
1479 "some_file.txt".to_string(),
1480 ]
1481 .iter()
1482 .collect();
1483 if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
1484 assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
1485 } else {
1486 assert_eq!(path.compact().to_str(), path.to_str());
1487 }
1488 }
1489
1490 #[perf]
1491 fn test_extension_or_hidden_file_name() {
1492 let path = Path::new("/a/b/c/file_name.rs");
1494 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1495
1496 let path = Path::new("/a/b/c/file.name.rs");
1498 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1499
1500 let path = Path::new("/a/b/c/long.file.name.rs");
1502 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1503
1504 let path = Path::new("/a/b/c/.gitignore");
1506 assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
1507
1508 let path = Path::new("/a/b/c/.eslintrc.js");
1510 assert_eq!(path.extension_or_hidden_file_name(), Some("eslintrc.js"));
1511 }
1512
1513 #[perf]
1514 fn edge_of_glob() {
1515 let path = Path::new("/work/node_modules");
1516 let path_matcher =
1517 PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
1518 assert!(
1519 path_matcher.is_match(path),
1520 "Path matcher should match {path:?}"
1521 );
1522 }
1523
1524 #[perf]
1525 fn project_search() {
1526 let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
1527 let path_matcher =
1528 PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
1529 assert!(
1530 path_matcher.is_match(path),
1531 "Path matcher should match {path:?}"
1532 );
1533 }
1534
1535 #[perf]
1536 #[cfg(target_os = "windows")]
1537 fn test_sanitized_path() {
1538 let path = Path::new("C:\\Users\\someone\\test_file.rs");
1539 let sanitized_path = SanitizedPath::new(path);
1540 assert_eq!(
1541 sanitized_path.to_string(),
1542 "C:\\Users\\someone\\test_file.rs"
1543 );
1544
1545 let path = Path::new("\\\\?\\C:\\Users\\someone\\test_file.rs");
1546 let sanitized_path = SanitizedPath::new(path);
1547 assert_eq!(
1548 sanitized_path.to_string(),
1549 "C:\\Users\\someone\\test_file.rs"
1550 );
1551 }
1552
1553 #[perf]
1554 fn test_compare_numeric_segments() {
1555 fn compare(a: &str, b: &str) -> Ordering {
1557 let mut a_iter = a.chars().peekable();
1558 let mut b_iter = b.chars().peekable();
1559
1560 let result = compare_numeric_segments(&mut a_iter, &mut b_iter);
1561
1562 assert!(
1564 !a_iter.next().is_some_and(|c| c.is_ascii_digit()),
1565 "Iterator a should have consumed all digits"
1566 );
1567 assert!(
1568 !b_iter.next().is_some_and(|c| c.is_ascii_digit()),
1569 "Iterator b should have consumed all digits"
1570 );
1571
1572 result
1573 }
1574
1575 assert_eq!(compare("0", "0"), Ordering::Equal);
1577 assert_eq!(compare("1", "2"), Ordering::Less);
1578 assert_eq!(compare("9", "10"), Ordering::Less);
1579 assert_eq!(compare("10", "9"), Ordering::Greater);
1580 assert_eq!(compare("99", "100"), Ordering::Less);
1581
1582 assert_eq!(compare("0", "00"), Ordering::Less);
1584 assert_eq!(compare("00", "0"), Ordering::Greater);
1585 assert_eq!(compare("01", "1"), Ordering::Greater);
1586 assert_eq!(compare("001", "1"), Ordering::Greater);
1587 assert_eq!(compare("001", "01"), Ordering::Greater);
1588
1589 assert_eq!(compare("000100", "100"), Ordering::Greater);
1591 assert_eq!(compare("100", "0100"), Ordering::Less);
1592 assert_eq!(compare("0100", "00100"), Ordering::Less);
1593
1594 assert_eq!(compare("9999999999", "10000000000"), Ordering::Less);
1596 assert_eq!(
1597 compare(
1598 "340282366920938463463374607431768211455", "340282366920938463463374607431768211456"
1600 ),
1601 Ordering::Less
1602 );
1603 assert_eq!(
1604 compare(
1605 "340282366920938463463374607431768211456", "340282366920938463463374607431768211455"
1607 ),
1608 Ordering::Greater
1609 );
1610
1611 let mut a_iter = "123abc".chars().peekable();
1613 let mut b_iter = "456def".chars().peekable();
1614
1615 compare_numeric_segments(&mut a_iter, &mut b_iter);
1616
1617 assert_eq!(a_iter.collect::<String>(), "abc");
1618 assert_eq!(b_iter.collect::<String>(), "def");
1619 }
1620
1621 #[perf]
1622 fn test_natural_sort() {
1623 assert_eq!(natural_sort("a", "b"), Ordering::Less);
1625 assert_eq!(natural_sort("b", "a"), Ordering::Greater);
1626 assert_eq!(natural_sort("a", "a"), Ordering::Equal);
1627
1628 assert_eq!(natural_sort("a", "A"), Ordering::Less);
1630 assert_eq!(natural_sort("A", "a"), Ordering::Greater);
1631 assert_eq!(natural_sort("aA", "aa"), Ordering::Greater);
1632 assert_eq!(natural_sort("aa", "aA"), Ordering::Less);
1633
1634 assert_eq!(natural_sort("1", "2"), Ordering::Less);
1636 assert_eq!(natural_sort("2", "10"), Ordering::Less);
1637 assert_eq!(natural_sort("02", "10"), Ordering::Less);
1638 assert_eq!(natural_sort("02", "2"), Ordering::Greater);
1639
1640 assert_eq!(natural_sort("a1", "a2"), Ordering::Less);
1642 assert_eq!(natural_sort("a2", "a10"), Ordering::Less);
1643 assert_eq!(natural_sort("a02", "a2"), Ordering::Greater);
1644 assert_eq!(natural_sort("a1b", "a1c"), Ordering::Less);
1645
1646 assert_eq!(natural_sort("1a2", "1a10"), Ordering::Less);
1648 assert_eq!(natural_sort("1a10", "1a2"), Ordering::Greater);
1649 assert_eq!(natural_sort("2a1", "10a1"), Ordering::Less);
1650
1651 assert_eq!(natural_sort("a-1", "a-2"), Ordering::Less);
1653 assert_eq!(natural_sort("a_1", "a_2"), Ordering::Less);
1654 assert_eq!(natural_sort("a.1", "a.2"), Ordering::Less);
1655
1656 assert_eq!(natural_sort("文1", "文2"), Ordering::Less);
1658 assert_eq!(natural_sort("文2", "文10"), Ordering::Less);
1659 assert_eq!(natural_sort("🔤1", "🔤2"), Ordering::Less);
1660
1661 assert_eq!(natural_sort("", ""), Ordering::Equal);
1663 assert_eq!(natural_sort("", "a"), Ordering::Less);
1664 assert_eq!(natural_sort("a", ""), Ordering::Greater);
1665 assert_eq!(natural_sort(" ", " "), Ordering::Less);
1666
1667 assert_eq!(natural_sort("File-1.txt", "File-2.txt"), Ordering::Less);
1669 assert_eq!(natural_sort("File-02.txt", "File-2.txt"), Ordering::Greater);
1670 assert_eq!(natural_sort("File-2.txt", "File-10.txt"), Ordering::Less);
1671 assert_eq!(natural_sort("File_A1", "File_A2"), Ordering::Less);
1672 assert_eq!(natural_sort("File_a1", "File_A1"), Ordering::Less);
1673 }
1674
1675 #[perf]
1676 fn test_compare_paths() {
1677 fn compare(a: &str, is_a_file: bool, b: &str, is_b_file: bool) -> Ordering {
1679 compare_paths((Path::new(a), is_a_file), (Path::new(b), is_b_file))
1680 }
1681
1682 assert_eq!(compare("a", true, "b", true), Ordering::Less);
1684 assert_eq!(compare("b", true, "a", true), Ordering::Greater);
1685 assert_eq!(compare("a", true, "a", true), Ordering::Equal);
1686
1687 assert_eq!(compare("a", true, "a", false), Ordering::Greater);
1689 assert_eq!(compare("a", false, "a", true), Ordering::Less);
1690 assert_eq!(compare("b", false, "a", true), Ordering::Less);
1691
1692 assert_eq!(compare("a.txt", true, "a.md", true), Ordering::Greater);
1694 assert_eq!(compare("a.md", true, "a.txt", true), Ordering::Less);
1695 assert_eq!(compare("a", true, "a.txt", true), Ordering::Less);
1696
1697 assert_eq!(compare("dir/a", true, "dir/b", true), Ordering::Less);
1699 assert_eq!(compare("dir1/a", true, "dir2/a", true), Ordering::Less);
1700 assert_eq!(compare("dir/sub/a", true, "dir/a", true), Ordering::Less);
1701
1702 assert_eq!(
1704 compare("Dir/file", true, "dir/file", true),
1705 Ordering::Greater
1706 );
1707 assert_eq!(
1708 compare("dir/File", true, "dir/file", true),
1709 Ordering::Greater
1710 );
1711 assert_eq!(compare("dir/file", true, "Dir/File", true), Ordering::Less);
1712
1713 assert_eq!(compare(".hidden", true, "visible", true), Ordering::Less);
1715 assert_eq!(compare("_special", true, "normal", true), Ordering::Less);
1716 assert_eq!(compare(".config", false, ".data", false), Ordering::Less);
1717
1718 assert_eq!(
1720 compare("dir1/file", true, "dir2/file", true),
1721 Ordering::Less
1722 );
1723 assert_eq!(
1724 compare("dir2/file", true, "dir10/file", true),
1725 Ordering::Less
1726 );
1727 assert_eq!(
1728 compare("dir02/file", true, "dir2/file", true),
1729 Ordering::Greater
1730 );
1731
1732 assert_eq!(compare("/a", true, "/b", true), Ordering::Less);
1734 assert_eq!(compare("/", false, "/a", true), Ordering::Less);
1735
1736 assert_eq!(
1738 compare("project/src/main.rs", true, "project/src/lib.rs", true),
1739 Ordering::Greater
1740 );
1741 assert_eq!(
1742 compare(
1743 "project/tests/test_1.rs",
1744 true,
1745 "project/tests/test_2.rs",
1746 true
1747 ),
1748 Ordering::Less
1749 );
1750 assert_eq!(
1751 compare(
1752 "project/v1.0.0/README.md",
1753 true,
1754 "project/v1.10.0/README.md",
1755 true
1756 ),
1757 Ordering::Less
1758 );
1759 }
1760
1761 #[perf]
1762 fn test_natural_sort_case_sensitivity() {
1763 std::thread::sleep(std::time::Duration::from_millis(100));
1764 assert_eq!(natural_sort("a", "A"), Ordering::Less);
1766 assert_eq!(natural_sort("A", "a"), Ordering::Greater);
1767 assert_eq!(natural_sort("a", "a"), Ordering::Equal);
1768 assert_eq!(natural_sort("A", "A"), Ordering::Equal);
1769
1770 assert_eq!(natural_sort("aaa", "AAA"), Ordering::Less);
1772 assert_eq!(natural_sort("AAA", "aaa"), Ordering::Greater);
1773 assert_eq!(natural_sort("aAa", "AaA"), Ordering::Less);
1774
1775 assert_eq!(natural_sort("a", "b"), Ordering::Less);
1777 assert_eq!(natural_sort("A", "b"), Ordering::Less);
1778 assert_eq!(natural_sort("a", "B"), Ordering::Less);
1779 }
1780
1781 #[perf]
1782 fn test_natural_sort_with_numbers() {
1783 assert_eq!(natural_sort("file1", "file2"), Ordering::Less);
1785 assert_eq!(natural_sort("file2", "file10"), Ordering::Less);
1786 assert_eq!(natural_sort("file10", "file2"), Ordering::Greater);
1787
1788 assert_eq!(natural_sort("1file", "2file"), Ordering::Less);
1790 assert_eq!(natural_sort("file1text", "file2text"), Ordering::Less);
1791 assert_eq!(natural_sort("text1file", "text2file"), Ordering::Less);
1792
1793 assert_eq!(natural_sort("file1-2", "file1-10"), Ordering::Less);
1795 assert_eq!(natural_sort("2-1file", "10-1file"), Ordering::Less);
1796
1797 assert_eq!(natural_sort("file002", "file2"), Ordering::Greater);
1799 assert_eq!(natural_sort("file002", "file10"), Ordering::Less);
1800
1801 assert_eq!(
1803 natural_sort("file999999999999999999999", "file999999999999999999998"),
1804 Ordering::Greater
1805 );
1806
1807 assert_eq!(
1811 natural_sort(
1812 "file340282366920938463463374607431768211454",
1813 "file340282366920938463463374607431768211455"
1814 ),
1815 Ordering::Less
1816 );
1817
1818 assert_eq!(
1820 natural_sort(
1821 "file340282366920938463463374607431768211456",
1822 "file340282366920938463463374607431768211455"
1823 ),
1824 Ordering::Greater
1825 );
1826
1827 assert_eq!(
1829 natural_sort(
1830 "file3402823669209384634633746074317682114560",
1831 "file340282366920938463463374607431768211455"
1832 ),
1833 Ordering::Greater
1834 );
1835
1836 assert_eq!(
1838 natural_sort(
1839 "file0340282366920938463463374607431768211455",
1840 "file340282366920938463463374607431768211455"
1841 ),
1842 Ordering::Greater
1843 );
1844
1845 assert_eq!(
1847 natural_sort(
1848 "file999999999999999999999999999999999999999999999999",
1849 "file9999999999999999999999999999999999999999999999999"
1850 ),
1851 Ordering::Less
1852 );
1853
1854 assert_eq!(natural_sort("File1", "file2"), Ordering::Greater);
1856 assert_eq!(natural_sort("file1", "File2"), Ordering::Less);
1857 }
1858
1859 #[perf]
1860 fn test_natural_sort_edge_cases() {
1861 assert_eq!(natural_sort("", ""), Ordering::Equal);
1863 assert_eq!(natural_sort("", "a"), Ordering::Less);
1864 assert_eq!(natural_sort("a", ""), Ordering::Greater);
1865
1866 assert_eq!(natural_sort("file-1", "file_1"), Ordering::Less);
1868 assert_eq!(natural_sort("file.1", "file_1"), Ordering::Less);
1869 assert_eq!(natural_sort("file 1", "file_1"), Ordering::Less);
1870
1871 assert_eq!(natural_sort("file①", "file②"), Ordering::Less);
1874 assert_eq!(natural_sort("file⑩", "file②"), Ordering::Greater);
1876 assert_eq!(natural_sort("file漢", "file字"), Ordering::Greater);
1878
1879 assert_eq!(natural_sort("file-1a", "file-1b"), Ordering::Less);
1881 assert_eq!(natural_sort("file-1.2", "file-1.10"), Ordering::Less);
1882 assert_eq!(natural_sort("file-1.10", "file-1.2"), Ordering::Greater);
1883 }
1884
1885 #[test]
1886 fn test_multiple_extensions() {
1887 let path = Path::new("/a/b/c/file_name");
1889 assert_eq!(path.multiple_extensions(), None);
1890
1891 let path = Path::new("/a/b/c/file_name.tsx");
1893 assert_eq!(path.multiple_extensions(), None);
1894
1895 let path = Path::new("/a/b/c/file_name.stories.tsx");
1897 assert_eq!(path.multiple_extensions(), Some("stories.tsx".to_string()));
1898
1899 let path = Path::new("/a/b/c/long.app.tar.gz");
1901 assert_eq!(path.multiple_extensions(), Some("app.tar.gz".to_string()));
1902 }
1903
1904 #[test]
1905 fn test_strip_path_suffix() {
1906 let base = Path::new("/a/b/c/file_name");
1907 let suffix = Path::new("file_name");
1908 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
1909
1910 let base = Path::new("/a/b/c/file_name.tsx");
1911 let suffix = Path::new("file_name.tsx");
1912 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
1913
1914 let base = Path::new("/a/b/c/file_name.stories.tsx");
1915 let suffix = Path::new("c/file_name.stories.tsx");
1916 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b")));
1917
1918 let base = Path::new("/a/b/c/long.app.tar.gz");
1919 let suffix = Path::new("b/c/long.app.tar.gz");
1920 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a")));
1921
1922 let base = Path::new("/a/b/c/long.app.tar.gz");
1923 let suffix = Path::new("/a/b/c/long.app.tar.gz");
1924 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("")));
1925
1926 let base = Path::new("/a/b/c/long.app.tar.gz");
1927 let suffix = Path::new("/a/b/c/no_match.app.tar.gz");
1928 assert_eq!(strip_path_suffix(base, suffix), None);
1929
1930 let base = Path::new("/a/b/c/long.app.tar.gz");
1931 let suffix = Path::new("app.tar.gz");
1932 assert_eq!(strip_path_suffix(base, suffix), None);
1933 }
1934}