Skip to content

Commit

Permalink
perf: add #[inline] to stdx::path functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RoloEdits committed Jan 1, 2025
1 parent 7895da6 commit 0b7f285
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions helix-stdx/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::env::current_working_dir;

/// Replaces users home directory from `path` with tilde `~` if the directory
/// is available, otherwise returns the path unchanged.
#[inline]
pub fn fold_home_dir<'a, P>(path: P) -> Cow<'a, Path>
where
P: Into<Cow<'a, Path>>,
Expand All @@ -33,8 +34,11 @@ where
}

/// Expands tilde `~` into users home directory if available, otherwise returns the path
/// unchanged. The tilde will only be expanded when present as the first component of the path
/// unchanged.
///
/// The tilde will only be expanded when present as the first component of the path
/// and only slash follows it.
#[inline]
pub fn expand_tilde<'a, P>(path: P) -> Cow<'a, Path>
where
P: Into<Cow<'a, Path>>,
Expand All @@ -54,11 +58,12 @@ where
}

/// Normalize a path without resolving symlinks.
// Strategy: start from the first component and move up. Cannonicalize previous path,
// Strategy: start from the first component and move up. Canonicalize previous path,
// join component, canonicalize new path, strip prefix and join to the final result.
#[inline]
pub fn normalize(path: impl AsRef<Path>) -> PathBuf {
let mut components = path.as_ref().components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().copied() {
components.next();
PathBuf::from(c.as_os_str())
} else {
Expand Down Expand Up @@ -127,6 +132,7 @@ pub fn normalize(path: impl AsRef<Path>) -> PathBuf {
///
/// This function is used instead of [`std::fs::canonicalize`] because we don't want to verify
/// here if the path exists, just normalize it's components.
#[inline]
pub fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
let path = expand_tilde(path.as_ref());
let path = if path.is_relative() {
Expand All @@ -138,6 +144,7 @@ pub fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
normalize(path)
}

#[inline]
pub fn get_relative_path<'a, P>(path: P) -> Cow<'a, Path>
where
P: Into<Cow<'a, Path>>,
Expand Down Expand Up @@ -184,6 +191,7 @@ where
/// assert_eq!(get_truncated_path("").as_path(), Path::new(""));
/// ```
///
#[inline]
pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf {
let cwd = current_working_dir();
let path = path.as_ref();
Expand All @@ -205,6 +213,7 @@ pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf {
ret
}

#[inline]
fn path_component_regex(windows: bool) -> String {
// TODO: support backslash path escape on windows (when using git bash for example)
let space_escape = if windows { r"[\^`]\s" } else { r"[\\]\s" };
Expand All @@ -214,10 +223,12 @@ fn path_component_regex(windows: bool) -> String {
}

/// Regex for delimited environment captures like `${HOME}`.
#[inline]
fn braced_env_regex(windows: bool) -> String {
r"\$\{(?:".to_owned() + &path_component_regex(windows) + r"|[/:=])+\}"
}

#[inline]
fn compile_path_regex(
prefix: &str,
postfix: &str,
Expand Down Expand Up @@ -256,6 +267,7 @@ fn compile_path_regex(
}

/// If `src` ends with a path then this function returns the part of the slice.
#[inline]
pub fn get_path_suffix(src: RopeSlice<'_>, match_single_file: bool) -> Option<RopeSlice<'_>> {
let regex = if match_single_file {
static REGEX: Lazy<Regex> = Lazy::new(|| compile_path_regex("", "$", true, cfg!(windows)));
Expand All @@ -271,6 +283,7 @@ pub fn get_path_suffix(src: RopeSlice<'_>, match_single_file: bool) -> Option<Ro
}

/// Returns an iterator of the **byte** ranges in src that contain a path.
#[inline]
pub fn find_paths(
src: RopeSlice<'_>,
match_single_file: bool,
Expand All @@ -286,6 +299,7 @@ pub fn find_paths(
}

/// Performs substitution of `~` and environment variables, see [`env::expand`](crate::env::expand) and [`expand_tilde`]
#[inline]
pub fn expand<T: AsRef<Path> + ?Sized>(path: &T) -> Cow<'_, Path> {
let path = path.as_ref();
let path = expand_tilde(path);
Expand Down

0 comments on commit 0b7f285

Please sign in to comment.