util.rs

   1pub mod archive;
   2pub mod command;
   3pub mod fs;
   4pub mod markdown;
   5pub mod paths;
   6pub mod process;
   7pub mod redact;
   8pub mod rel_path;
   9pub mod schemars;
  10pub mod serde;
  11pub mod shell;
  12pub mod shell_builder;
  13pub mod shell_env;
  14pub mod size;
  15#[cfg(any(test, feature = "test-support"))]
  16pub mod test;
  17pub mod time;
  18
  19use anyhow::Result;
  20use itertools::Either;
  21use regex::Regex;
  22use std::path::{Path, PathBuf};
  23use std::sync::LazyLock;
  24use std::{
  25    borrow::Cow,
  26    cmp::{self, Ordering},
  27    ops::{Range, RangeInclusive},
  28};
  29use unicase::UniCase;
  30
  31pub use gpui_util::*;
  32
  33pub use take_until::*;
  34#[cfg(any(test, feature = "test-support"))]
  35pub use util_macros::{line_endings, path, uri};
  36
  37pub use self::shell::{
  38    get_default_system_shell, get_default_system_shell_preferring_bash, get_system_shell,
  39};
  40
  41#[inline]
  42pub const fn is_utf8_char_boundary(u8: u8) -> bool {
  43    // This is bit magic equivalent to: b < 128 || b >= 192
  44    (u8 as i8) >= -0x40
  45}
  46
  47pub fn truncate(s: &str, max_chars: usize) -> &str {
  48    match s.char_indices().nth(max_chars) {
  49        None => s,
  50        Some((idx, _)) => &s[..idx],
  51    }
  52}
  53
  54/// Removes characters from the end of the string if its length is greater than `max_chars` and
  55/// appends "..." to the string. Returns string unchanged if its length is smaller than max_chars.
  56pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
  57    debug_assert!(max_chars >= 5);
  58
  59    // If the string's byte length is <= max_chars, walking the string can be skipped since the
  60    // number of chars is <= the number of bytes.
  61    if s.len() <= max_chars {
  62        return s.to_string();
  63    }
  64    let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars);
  65    match truncation_ix {
  66        Some(index) => s[..index].to_string() + "…",
  67        _ => s.to_string(),
  68    }
  69}
  70
  71/// Removes characters from the front of the string if its length is greater than `max_chars` and
  72/// prepends the string with "...". Returns string unchanged if its length is smaller than max_chars.
  73pub fn truncate_and_remove_front(s: &str, max_chars: usize) -> String {
  74    debug_assert!(max_chars >= 5);
  75
  76    // If the string's byte length is <= max_chars, walking the string can be skipped since the
  77    // number of chars is <= the number of bytes.
  78    if s.len() <= max_chars {
  79        return s.to_string();
  80    }
  81    let suffix_char_length = max_chars.saturating_sub(1);
  82    let truncation_ix = s
  83        .char_indices()
  84        .map(|(i, _)| i)
  85        .nth_back(suffix_char_length);
  86    match truncation_ix {
  87        Some(index) if index > 0 => "…".to_string() + &s[index..],
  88        _ => s.to_string(),
  89    }
  90}
  91
  92/// Takes only `max_lines` from the string and, if there were more than `max_lines-1`, appends a
  93/// a newline and "..." to the string, so that `max_lines` are returned.
  94/// Returns string unchanged if its length is smaller than max_lines.
  95pub fn truncate_lines_and_trailoff(s: &str, max_lines: usize) -> String {
  96    let mut lines = s.lines().take(max_lines).collect::<Vec<_>>();
  97    if lines.len() > max_lines - 1 {
  98        lines.pop();
  99        lines.join("\n") + "\n…"
 100    } else {
 101        lines.join("\n")
 102    }
 103}
 104
 105/// Truncates the string at a character boundary, such that the result is less than `max_bytes` in
 106/// length.
 107pub fn truncate_to_byte_limit(s: &str, max_bytes: usize) -> &str {
 108    if s.len() < max_bytes {
 109        return s;
 110    }
 111
 112    for i in (0..max_bytes).rev() {
 113        if s.is_char_boundary(i) {
 114            return &s[..i];
 115        }
 116    }
 117
 118    ""
 119}
 120
 121/// Takes a prefix of complete lines which fit within the byte limit. If the first line is longer
 122/// than the limit, truncates at a character boundary.
 123pub fn truncate_lines_to_byte_limit(s: &str, max_bytes: usize) -> &str {
 124    if s.len() < max_bytes {
 125        return s;
 126    }
 127
 128    for i in (0..max_bytes).rev() {
 129        if s.is_char_boundary(i) && s.as_bytes()[i] == b'\n' {
 130            // Since the i-th character is \n, valid to slice at i + 1.
 131            return &s[..i + 1];
 132        }
 133    }
 134
 135    truncate_to_byte_limit(s, max_bytes)
 136}
 137
 138#[test]
 139fn test_truncate_lines_to_byte_limit() {
 140    let text = "Line 1\nLine 2\nLine 3\nLine 4";
 141
 142    // Limit that includes all lines
 143    assert_eq!(truncate_lines_to_byte_limit(text, 100), text);
 144
 145    // Exactly the first line
 146    assert_eq!(truncate_lines_to_byte_limit(text, 7), "Line 1\n");
 147
 148    // Limit between lines
 149    assert_eq!(truncate_lines_to_byte_limit(text, 13), "Line 1\n");
 150    assert_eq!(truncate_lines_to_byte_limit(text, 20), "Line 1\nLine 2\n");
 151
 152    // Limit before first newline
 153    assert_eq!(truncate_lines_to_byte_limit(text, 6), "Line ");
 154
 155    // Test with non-ASCII characters
 156    let text_utf8 = "Line 1\nLΓ­ne 2\nLine 3";
 157    assert_eq!(
 158        truncate_lines_to_byte_limit(text_utf8, 15),
 159        "Line 1\nLΓ­ne 2\n"
 160    );
 161}
 162
 163/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
 164/// enforcing a maximum length. This also de-duplicates items. Sort the items according to the given callback. Before calling this,
 165/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
 166pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
 167where
 168    I: IntoIterator<Item = T>,
 169    F: FnMut(&T, &T) -> Ordering,
 170{
 171    let mut start_index = 0;
 172    for new_item in new_items {
 173        if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
 174            let index = start_index + i;
 175            if vec.len() < limit {
 176                vec.insert(index, new_item);
 177            } else if index < vec.len() {
 178                vec.pop();
 179                vec.insert(index, new_item);
 180            }
 181            start_index = index;
 182        }
 183    }
 184}
 185
 186pub fn truncate_to_bottom_n_sorted_by<T, F>(items: &mut Vec<T>, limit: usize, compare: &F)
 187where
 188    F: Fn(&T, &T) -> Ordering,
 189{
 190    if limit == 0 {
 191        items.truncate(0);
 192    }
 193    if items.len() <= limit {
 194        items.sort_by(compare);
 195        return;
 196    }
 197    // When limit is near to items.len() it may be more efficient to sort the whole list and
 198    // truncate, rather than always doing selection first as is done below. It's hard to analyze
 199    // where the threshold for this should be since the quickselect style algorithm used by
 200    // `select_nth_unstable_by` makes the prefix partially sorted, and so its work is not wasted -
 201    // the expected number of comparisons needed by `sort_by` is less than it is for some arbitrary
 202    // unsorted input.
 203    items.select_nth_unstable_by(limit, compare);
 204    items.truncate(limit);
 205    items.sort_by(compare);
 206}
 207
 208/// Prevents execution of the application with root privileges on Unix systems.
 209///
 210/// This function checks if the current process is running with root privileges
 211/// and terminates the program with an error message unless explicitly allowed via the
 212/// `ZED_ALLOW_ROOT` environment variable.
 213#[cfg(unix)]
 214pub fn prevent_root_execution() {
 215    let is_root = nix::unistd::geteuid().is_root();
 216    let allow_root = std::env::var("ZED_ALLOW_ROOT").is_ok_and(|val| val == "true");
 217
 218    if is_root && !allow_root {
 219        eprintln!(
 220            "\
 221Error: Running Zed as root or via sudo is unsupported.
 222       Doing so (even once) may subtly break things for all subsequent non-root usage of Zed.
 223       It is untested and not recommended, don't complain when things break.
 224       If you wish to proceed anyways, set `ZED_ALLOW_ROOT=true` in your environment."
 225        );
 226        std::process::exit(1);
 227    }
 228}
 229
 230#[cfg(unix)]
 231fn load_shell_from_passwd() -> Result<()> {
 232    let buflen = match unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) } {
 233        n if n < 0 => 1024,
 234        n => n as usize,
 235    };
 236    let mut buffer = Vec::with_capacity(buflen);
 237
 238    let mut pwd: std::mem::MaybeUninit<libc::passwd> = std::mem::MaybeUninit::uninit();
 239    let mut result: *mut libc::passwd = std::ptr::null_mut();
 240
 241    let uid = unsafe { libc::getuid() };
 242    let status = unsafe {
 243        libc::getpwuid_r(
 244            uid,
 245            pwd.as_mut_ptr(),
 246            buffer.as_mut_ptr() as *mut libc::c_char,
 247            buflen,
 248            &mut result,
 249        )
 250    };
 251    anyhow::ensure!(!result.is_null(), "passwd entry for uid {} not found", uid);
 252
 253    // SAFETY: If `getpwuid_r` doesn't error, we have the entry here.
 254    let entry = unsafe { pwd.assume_init() };
 255
 256    anyhow::ensure!(
 257        status == 0,
 258        "call to getpwuid_r failed. uid: {}, status: {}",
 259        uid,
 260        status
 261    );
 262    anyhow::ensure!(
 263        entry.pw_uid == uid,
 264        "passwd entry has different uid ({}) than getuid ({}) returned",
 265        entry.pw_uid,
 266        uid,
 267    );
 268
 269    let shell = unsafe { std::ffi::CStr::from_ptr(entry.pw_shell).to_str().unwrap() };
 270    let should_set_shell = std::env::var("SHELL").map_or(true, |shell_env| {
 271        shell_env != shell && !std::path::Path::new(&shell_env).exists()
 272    });
 273
 274    if should_set_shell {
 275        log::info!(
 276            "updating SHELL environment variable to value from passwd entry: {:?}",
 277            shell,
 278        );
 279        unsafe { std::env::set_var("SHELL", shell) };
 280    }
 281
 282    Ok(())
 283}
 284
 285/// Returns a shell escaped path for the current zed executable
 286pub fn get_shell_safe_zed_path(shell_kind: shell::ShellKind) -> anyhow::Result<String> {
 287    use anyhow::Context as _;
 288    use paths::PathExt;
 289    let mut zed_path =
 290        std::env::current_exe().context("Failed to determine current zed executable path.")?;
 291    if cfg!(target_os = "linux")
 292        && !zed_path.is_file()
 293        && let Some(truncated) = zed_path
 294            .clone()
 295            .file_name()
 296            .and_then(|s| s.to_str())
 297            .and_then(|n| n.strip_suffix(" (deleted)"))
 298    {
 299        // Might have been deleted during update; let's use the new binary if there is one.
 300        zed_path.set_file_name(truncated);
 301    }
 302
 303    zed_path
 304        .try_shell_safe(shell_kind)
 305        .context("Failed to shell-escape Zed executable path.")
 306}
 307
 308/// Returns a path for the zed cli executable, this function
 309/// should be called from the zed executable, not zed-cli.
 310pub fn get_zed_cli_path() -> Result<PathBuf> {
 311    use anyhow::Context as _;
 312    let zed_path =
 313        std::env::current_exe().context("Failed to determine current zed executable path.")?;
 314    let parent = zed_path
 315        .parent()
 316        .context("Failed to determine parent directory of zed executable path.")?;
 317
 318    let possible_locations: &[&str] = if cfg!(target_os = "macos") {
 319        // On macOS, the zed executable and zed-cli are inside the app bundle,
 320        // so here ./cli is for both installed and development builds.
 321        &["./cli"]
 322    } else if cfg!(target_os = "windows") {
 323        // bin/zed.exe is for installed builds, ./cli.exe is for development builds.
 324        &["bin/zed.exe", "./cli.exe"]
 325    } else if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") {
 326        // bin is the standard, ./cli is for the target directory in development builds.
 327        &["../bin/zed", "./cli"]
 328    } else {
 329        anyhow::bail!("unsupported platform for determining zed-cli path");
 330    };
 331
 332    possible_locations
 333        .iter()
 334        .find_map(|p| {
 335            parent
 336                .join(p)
 337                .canonicalize()
 338                .ok()
 339                .filter(|p| p != &zed_path)
 340        })
 341        .with_context(|| {
 342            format!(
 343                "could not find zed-cli from any of: {}",
 344                possible_locations.join(", ")
 345            )
 346        })
 347}
 348
 349#[cfg(unix)]
 350pub async fn load_login_shell_environment() -> Result<()> {
 351    use anyhow::Context as _;
 352
 353    load_shell_from_passwd().log_err();
 354
 355    // If possible, we want to `cd` in the user's `$HOME` to trigger programs
 356    // such as direnv, asdf, mise, ... to adjust the PATH. These tools often hook
 357    // into shell's `cd` command (and hooks) to manipulate env.
 358    // We do this so that we get the env a user would have when spawning a shell
 359    // in home directory.
 360    for (name, value) in shell_env::capture(get_system_shell(), &[], paths::home_dir())
 361        .await
 362        .with_context(|| format!("capturing environment with {:?}", get_system_shell()))?
 363    {
 364        // Skip SHLVL to prevent it from polluting Zed's process environment.
 365        // The login shell used for env capture increments SHLVL, and if we propagate it,
 366        // terminals spawned by Zed will inherit it and increment again, causing SHLVL
 367        // to start at 2 instead of 1 (and increase by 2 on each reload).
 368        if name == "SHLVL" {
 369            continue;
 370        }
 371        unsafe { std::env::set_var(&name, &value) };
 372    }
 373
 374    log::info!(
 375        "set environment variables from shell:{}, path:{}",
 376        std::env::var("SHELL").unwrap_or_default(),
 377        std::env::var("PATH").unwrap_or_default(),
 378    );
 379
 380    Ok(())
 381}
 382
 383/// Configures the process to start a new session, to prevent interactive shells from taking control
 384/// of the terminal.
 385///
 386/// For more details: <https://registerspill.thorstenball.com/p/how-to-lose-control-of-your-shell>
 387pub fn set_pre_exec_to_start_new_session(
 388    command: &mut std::process::Command,
 389) -> &mut std::process::Command {
 390    // safety: code in pre_exec should be signal safe.
 391    // https://man7.org/linux/man-pages/man7/signal-safety.7.html
 392    #[cfg(unix)]
 393    unsafe {
 394        use std::os::unix::process::CommandExt;
 395        command.pre_exec(|| {
 396            libc::setsid();
 397            Ok(())
 398        });
 399    };
 400    command
 401}
 402
 403pub fn merge_json_lenient_value_into(
 404    source: serde_json_lenient::Value,
 405    target: &mut serde_json_lenient::Value,
 406) {
 407    match (source, target) {
 408        (serde_json_lenient::Value::Object(source), serde_json_lenient::Value::Object(target)) => {
 409            for (key, value) in source {
 410                if let Some(target) = target.get_mut(&key) {
 411                    merge_json_lenient_value_into(value, target);
 412                } else {
 413                    target.insert(key, value);
 414                }
 415            }
 416        }
 417
 418        (serde_json_lenient::Value::Array(source), serde_json_lenient::Value::Array(target)) => {
 419            for value in source {
 420                target.push(value);
 421            }
 422        }
 423
 424        (source, target) => *target = source,
 425    }
 426}
 427
 428pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
 429    use serde_json::Value;
 430
 431    match (source, target) {
 432        (Value::Object(source), Value::Object(target)) => {
 433            for (key, value) in source {
 434                if let Some(target) = target.get_mut(&key) {
 435                    merge_json_value_into(value, target);
 436                } else {
 437                    target.insert(key, value);
 438                }
 439            }
 440        }
 441
 442        (Value::Array(source), Value::Array(target)) => {
 443            for value in source {
 444                target.push(value);
 445            }
 446        }
 447
 448        (source, target) => *target = source,
 449    }
 450}
 451
 452pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
 453    use serde_json::Value;
 454    if let Value::Object(source_object) = source {
 455        let target_object = if let Value::Object(target) = target {
 456            target
 457        } else {
 458            *target = Value::Object(Default::default());
 459            target.as_object_mut().unwrap()
 460        };
 461        for (key, value) in source_object {
 462            if let Some(target) = target_object.get_mut(&key) {
 463                merge_non_null_json_value_into(value, target);
 464            } else if !value.is_null() {
 465                target_object.insert(key, value);
 466            }
 467        }
 468    } else if !source.is_null() {
 469        *target = source
 470    }
 471}
 472
 473pub fn expanded_and_wrapped_usize_range(
 474    range: Range<usize>,
 475    additional_before: usize,
 476    additional_after: usize,
 477    wrap_length: usize,
 478) -> impl Iterator<Item = usize> {
 479    let start_wraps = range.start < additional_before;
 480    let end_wraps = wrap_length < range.end + additional_after;
 481    if start_wraps && end_wraps {
 482        Either::Left(0..wrap_length)
 483    } else if start_wraps {
 484        let wrapped_start = (range.start + wrap_length).saturating_sub(additional_before);
 485        if wrapped_start <= range.end {
 486            Either::Left(0..wrap_length)
 487        } else {
 488            Either::Right((0..range.end + additional_after).chain(wrapped_start..wrap_length))
 489        }
 490    } else if end_wraps {
 491        let wrapped_end = range.end + additional_after - wrap_length;
 492        if range.start <= wrapped_end {
 493            Either::Left(0..wrap_length)
 494        } else {
 495            Either::Right((0..wrapped_end).chain(range.start - additional_before..wrap_length))
 496        }
 497    } else {
 498        Either::Left((range.start - additional_before)..(range.end + additional_after))
 499    }
 500}
 501
 502/// Yields `[i, i + 1, i - 1, i + 2, ..]`, each modulo `wrap_length` and bounded by
 503/// `additional_before` and `additional_after`. If the wrapping causes overlap, duplicates are not
 504/// emitted. If wrap_length is 0, nothing is yielded.
 505pub fn wrapped_usize_outward_from(
 506    start: usize,
 507    additional_before: usize,
 508    additional_after: usize,
 509    wrap_length: usize,
 510) -> impl Iterator<Item = usize> {
 511    let mut count = 0;
 512    let mut after_offset = 1;
 513    let mut before_offset = 1;
 514
 515    std::iter::from_fn(move || {
 516        count += 1;
 517        if count > wrap_length {
 518            None
 519        } else if count == 1 {
 520            Some(start % wrap_length)
 521        } else if after_offset <= additional_after && after_offset <= before_offset {
 522            let value = (start + after_offset) % wrap_length;
 523            after_offset += 1;
 524            Some(value)
 525        } else if before_offset <= additional_before {
 526            let value = (start + wrap_length - before_offset) % wrap_length;
 527            before_offset += 1;
 528            Some(value)
 529        } else if after_offset <= additional_after {
 530            let value = (start + after_offset) % wrap_length;
 531            after_offset += 1;
 532            Some(value)
 533        } else {
 534            None
 535        }
 536    })
 537}
 538
 539#[cfg(any(test, feature = "test-support"))]
 540mod rng {
 541    use rand::prelude::*;
 542
 543    pub struct RandomCharIter<T: Rng> {
 544        rng: T,
 545        simple_text: bool,
 546    }
 547
 548    impl<T: Rng> RandomCharIter<T> {
 549        pub fn new(rng: T) -> Self {
 550            Self {
 551                rng,
 552                simple_text: std::env::var("SIMPLE_TEXT").is_ok_and(|v| !v.is_empty()),
 553            }
 554        }
 555
 556        pub fn with_simple_text(mut self) -> Self {
 557            self.simple_text = true;
 558            self
 559        }
 560    }
 561
 562    impl<T: Rng> Iterator for RandomCharIter<T> {
 563        type Item = char;
 564
 565        fn next(&mut self) -> Option<Self::Item> {
 566            if self.simple_text {
 567                return if self.rng.random_range(0..100) < 5 {
 568                    Some('\n')
 569                } else {
 570                    Some(self.rng.random_range(b'a'..b'z' + 1).into())
 571                };
 572            }
 573
 574            match self.rng.random_range(0..100) {
 575                // whitespace
 576                0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
 577                // two-byte greek letters
 578                20..=32 => char::from_u32(self.rng.random_range(('Ξ±' as u32)..('Ο‰' as u32 + 1))),
 579                // // three-byte characters
 580                33..=45 => ['βœ‹', 'βœ…', '❌', '❎', '⭐']
 581                    .choose(&mut self.rng)
 582                    .copied(),
 583                // // four-byte characters
 584                46..=58 => ['🍐', 'πŸ€', 'πŸ—', 'πŸŽ‰'].choose(&mut self.rng).copied(),
 585                // ascii letters
 586                _ => Some(self.rng.random_range(b'a'..b'z' + 1).into()),
 587            }
 588        }
 589    }
 590}
 591#[cfg(any(test, feature = "test-support"))]
 592pub use rng::RandomCharIter;
 593
 594/// Get an embedded file as a string.
 595pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
 596    match A::get(path).expect(path).data {
 597        Cow::Borrowed(bytes) => Cow::Borrowed(std::str::from_utf8(bytes).unwrap()),
 598        Cow::Owned(bytes) => Cow::Owned(String::from_utf8(bytes).unwrap()),
 599    }
 600}
 601
 602pub trait RangeExt<T> {
 603    fn sorted(&self) -> Self;
 604    fn to_inclusive(&self) -> RangeInclusive<T>;
 605    fn overlaps(&self, other: &Range<T>) -> bool;
 606    fn contains_inclusive(&self, other: &Range<T>) -> bool;
 607}
 608
 609impl<T: Ord + Clone> RangeExt<T> for Range<T> {
 610    fn sorted(&self) -> Self {
 611        cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
 612    }
 613
 614    fn to_inclusive(&self) -> RangeInclusive<T> {
 615        self.start.clone()..=self.end.clone()
 616    }
 617
 618    fn overlaps(&self, other: &Range<T>) -> bool {
 619        self.start < other.end && other.start < self.end
 620    }
 621
 622    fn contains_inclusive(&self, other: &Range<T>) -> bool {
 623        self.start <= other.start && other.end <= self.end
 624    }
 625}
 626
 627impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
 628    fn sorted(&self) -> Self {
 629        cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
 630    }
 631
 632    fn to_inclusive(&self) -> RangeInclusive<T> {
 633        self.clone()
 634    }
 635
 636    fn overlaps(&self, other: &Range<T>) -> bool {
 637        self.start() < &other.end && &other.start <= self.end()
 638    }
 639
 640    fn contains_inclusive(&self, other: &Range<T>) -> bool {
 641        self.start() <= &other.start && &other.end <= self.end()
 642    }
 643}
 644
 645/// A way to sort strings with starting numbers numerically first, falling back to alphanumeric one,
 646/// case-insensitive.
 647///
 648/// This is useful for turning regular alphanumerically sorted sequences as `1-abc, 10, 11-def, .., 2, 21-abc`
 649/// into `1-abc, 2, 10, 11-def, .., 21-abc`
 650#[derive(Debug, PartialEq, Eq)]
 651pub struct NumericPrefixWithSuffix<'a>(Option<u64>, &'a str);
 652
 653impl<'a> NumericPrefixWithSuffix<'a> {
 654    pub fn from_numeric_prefixed_str(str: &'a str) -> Self {
 655        let i = str.chars().take_while(|c| c.is_ascii_digit()).count();
 656        let (prefix, remainder) = str.split_at(i);
 657
 658        let prefix = prefix.parse().ok();
 659        Self(prefix, remainder)
 660    }
 661}
 662
 663/// When dealing with equality, we need to consider the case of the strings to achieve strict equality
 664/// to handle cases like "a" < "A" instead of "a" == "A".
 665impl Ord for NumericPrefixWithSuffix<'_> {
 666    fn cmp(&self, other: &Self) -> Ordering {
 667        match (self.0, other.0) {
 668            (None, None) => UniCase::new(self.1)
 669                .cmp(&UniCase::new(other.1))
 670                .then_with(|| self.1.cmp(other.1).reverse()),
 671            (None, Some(_)) => Ordering::Greater,
 672            (Some(_), None) => Ordering::Less,
 673            (Some(a), Some(b)) => a.cmp(&b).then_with(|| {
 674                UniCase::new(self.1)
 675                    .cmp(&UniCase::new(other.1))
 676                    .then_with(|| self.1.cmp(other.1).reverse())
 677            }),
 678        }
 679    }
 680}
 681
 682impl PartialOrd for NumericPrefixWithSuffix<'_> {
 683    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
 684        Some(self.cmp(other))
 685    }
 686}
 687
 688/// Capitalizes the first character of a string.
 689///
 690/// This function takes a string slice as input and returns a new `String` with the first character
 691/// capitalized.
 692///
 693/// # Examples
 694///
 695/// ```
 696/// use util::capitalize;
 697///
 698/// assert_eq!(capitalize("hello"), "Hello");
 699/// assert_eq!(capitalize("WORLD"), "WORLD");
 700/// assert_eq!(capitalize(""), "");
 701/// ```
 702pub fn capitalize(str: &str) -> String {
 703    let mut chars = str.chars();
 704    match chars.next() {
 705        None => String::new(),
 706        Some(first_char) => first_char.to_uppercase().collect::<String>() + chars.as_str(),
 707    }
 708}
 709
 710fn emoji_regex() -> &'static Regex {
 711    static EMOJI_REGEX: LazyLock<Regex> =
 712        LazyLock::new(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap());
 713    &EMOJI_REGEX
 714}
 715
 716/// Returns true if the given string consists of emojis only.
 717/// E.g. "πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘§πŸ‘‹" will return true, but "πŸ‘‹!" will return false.
 718pub fn word_consists_of_emojis(s: &str) -> bool {
 719    let mut prev_end = 0;
 720    for capture in emoji_regex().find_iter(s) {
 721        if capture.start() != prev_end {
 722            return false;
 723        }
 724        prev_end = capture.end();
 725    }
 726    prev_end == s.len()
 727}
 728
 729/// Similar to `str::split`, but also provides byte-offset ranges of the results. Unlike
 730/// `str::split`, this is not generic on pattern types and does not return an `Iterator`.
 731pub fn split_str_with_ranges<'s>(
 732    s: &'s str,
 733    pat: &dyn Fn(char) -> bool,
 734) -> Vec<(Range<usize>, &'s str)> {
 735    let mut result = Vec::new();
 736    let mut start = 0;
 737
 738    for (i, ch) in s.char_indices() {
 739        if pat(ch) {
 740            if i > start {
 741                result.push((start..i, &s[start..i]));
 742            }
 743            start = i + ch.len_utf8();
 744        }
 745    }
 746
 747    if s.len() > start {
 748        result.push((start..s.len(), &s[start..s.len()]));
 749    }
 750
 751    result
 752}
 753
 754pub fn default<D: Default>() -> D {
 755    Default::default()
 756}
 757
 758#[derive(Debug)]
 759pub enum ConnectionResult<O> {
 760    Timeout,
 761    ConnectionReset,
 762    Result(anyhow::Result<O>),
 763}
 764
 765impl<O> ConnectionResult<O> {
 766    pub fn into_response(self) -> anyhow::Result<O> {
 767        match self {
 768            ConnectionResult::Timeout => anyhow::bail!("Request timed out"),
 769            ConnectionResult::ConnectionReset => anyhow::bail!("Server reset the connection"),
 770            ConnectionResult::Result(r) => r,
 771        }
 772    }
 773}
 774
 775impl<O> From<anyhow::Result<O>> for ConnectionResult<O> {
 776    fn from(result: anyhow::Result<O>) -> Self {
 777        ConnectionResult::Result(result)
 778    }
 779}
 780
 781/// Normalizes a path by resolving `.` and `..` components without
 782/// requiring the path to exist on disk (unlike `canonicalize`).
 783pub fn normalize_path(path: &Path) -> PathBuf {
 784    use std::path::Component;
 785    let mut components = path.components().peekable();
 786    let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
 787        components.next();
 788        PathBuf::from(c.as_os_str())
 789    } else {
 790        PathBuf::new()
 791    };
 792
 793    for component in components {
 794        match component {
 795            Component::Prefix(..) => unreachable!(),
 796            Component::RootDir => {
 797                ret.push(component.as_os_str());
 798            }
 799            Component::CurDir => {}
 800            Component::ParentDir => {
 801                ret.pop();
 802            }
 803            Component::Normal(c) => {
 804                ret.push(c);
 805            }
 806        }
 807    }
 808    ret
 809}
 810
 811#[cfg(test)]
 812mod tests {
 813    use super::*;
 814
 815    #[test]
 816    fn test_extend_sorted() {
 817        let mut vec = vec![];
 818
 819        extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
 820        assert_eq!(vec, &[21, 17, 13, 8, 1]);
 821
 822        extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
 823        assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
 824
 825        extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
 826        assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
 827    }
 828
 829    #[test]
 830    fn test_truncate_to_bottom_n_sorted_by() {
 831        let mut vec: Vec<u32> = vec![5, 2, 3, 4, 1];
 832        truncate_to_bottom_n_sorted_by(&mut vec, 10, &u32::cmp);
 833        assert_eq!(vec, &[1, 2, 3, 4, 5]);
 834
 835        vec = vec![5, 2, 3, 4, 1];
 836        truncate_to_bottom_n_sorted_by(&mut vec, 5, &u32::cmp);
 837        assert_eq!(vec, &[1, 2, 3, 4, 5]);
 838
 839        vec = vec![5, 2, 3, 4, 1];
 840        truncate_to_bottom_n_sorted_by(&mut vec, 4, &u32::cmp);
 841        assert_eq!(vec, &[1, 2, 3, 4]);
 842
 843        vec = vec![5, 2, 3, 4, 1];
 844        truncate_to_bottom_n_sorted_by(&mut vec, 1, &u32::cmp);
 845        assert_eq!(vec, &[1]);
 846
 847        vec = vec![5, 2, 3, 4, 1];
 848        truncate_to_bottom_n_sorted_by(&mut vec, 0, &u32::cmp);
 849        assert!(vec.is_empty());
 850    }
 851
 852    #[test]
 853    fn test_iife() {
 854        fn option_returning_function() -> Option<()> {
 855            None
 856        }
 857
 858        let foo = maybe!({
 859            option_returning_function()?;
 860            Some(())
 861        });
 862
 863        assert_eq!(foo, None);
 864    }
 865
 866    #[test]
 867    fn test_truncate_and_trailoff() {
 868        assert_eq!(truncate_and_trailoff("", 5), "");
 869        assert_eq!(truncate_and_trailoff("aaaaaa", 7), "aaaaaa");
 870        assert_eq!(truncate_and_trailoff("aaaaaa", 6), "aaaaaa");
 871        assert_eq!(truncate_and_trailoff("aaaaaa", 5), "aaaaa…");
 872        assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
 873        assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
 874        assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…");
 875    }
 876
 877    #[test]
 878    fn test_truncate_and_remove_front() {
 879        assert_eq!(truncate_and_remove_front("", 5), "");
 880        assert_eq!(truncate_and_remove_front("aaaaaa", 7), "aaaaaa");
 881        assert_eq!(truncate_and_remove_front("aaaaaa", 6), "aaaaaa");
 882        assert_eq!(truncate_and_remove_front("aaaaaa", 5), "…aaaaa");
 883        assert_eq!(truncate_and_remove_front("èèèèèè", 7), "èèèèèè");
 884        assert_eq!(truncate_and_remove_front("èèèèèè", 6), "èèèèèè");
 885        assert_eq!(truncate_and_remove_front("èèèèèè", 5), "…èèèèè");
 886    }
 887
 888    #[test]
 889    fn test_numeric_prefix_str_method() {
 890        let target = "1a";
 891        assert_eq!(
 892            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 893            NumericPrefixWithSuffix(Some(1), "a")
 894        );
 895
 896        let target = "12ab";
 897        assert_eq!(
 898            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 899            NumericPrefixWithSuffix(Some(12), "ab")
 900        );
 901
 902        let target = "12_ab";
 903        assert_eq!(
 904            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 905            NumericPrefixWithSuffix(Some(12), "_ab")
 906        );
 907
 908        let target = "1_2ab";
 909        assert_eq!(
 910            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 911            NumericPrefixWithSuffix(Some(1), "_2ab")
 912        );
 913
 914        let target = "1.2";
 915        assert_eq!(
 916            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 917            NumericPrefixWithSuffix(Some(1), ".2")
 918        );
 919
 920        let target = "1.2_a";
 921        assert_eq!(
 922            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 923            NumericPrefixWithSuffix(Some(1), ".2_a")
 924        );
 925
 926        let target = "12.2_a";
 927        assert_eq!(
 928            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 929            NumericPrefixWithSuffix(Some(12), ".2_a")
 930        );
 931
 932        let target = "12a.2_a";
 933        assert_eq!(
 934            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
 935            NumericPrefixWithSuffix(Some(12), "a.2_a")
 936        );
 937    }
 938
 939    #[test]
 940    fn test_numeric_prefix_with_suffix() {
 941        let mut sorted = vec!["1-abc", "10", "11def", "2", "21-abc"];
 942        sorted.sort_by_key(|s| NumericPrefixWithSuffix::from_numeric_prefixed_str(s));
 943        assert_eq!(sorted, ["1-abc", "2", "10", "11def", "21-abc"]);
 944
 945        for numeric_prefix_less in ["numeric_prefix_less", "aaa", "~β„’Β£"] {
 946            assert_eq!(
 947                NumericPrefixWithSuffix::from_numeric_prefixed_str(numeric_prefix_less),
 948                NumericPrefixWithSuffix(None, numeric_prefix_less),
 949                "String without numeric prefix `{numeric_prefix_less}` should not be converted into NumericPrefixWithSuffix"
 950            )
 951        }
 952    }
 953
 954    #[test]
 955    fn test_word_consists_of_emojis() {
 956        let words_to_test = vec![
 957            ("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘§πŸ‘‹πŸ₯’", true),
 958            ("πŸ‘‹", true),
 959            ("!πŸ‘‹", false),
 960            ("πŸ‘‹!", false),
 961            ("πŸ‘‹ ", false),
 962            (" πŸ‘‹", false),
 963            ("Test", false),
 964        ];
 965
 966        for (text, expected_result) in words_to_test {
 967            assert_eq!(word_consists_of_emojis(text), expected_result);
 968        }
 969    }
 970
 971    #[test]
 972    fn test_truncate_lines_and_trailoff() {
 973        let text = r#"Line 1
 974Line 2
 975Line 3"#;
 976
 977        assert_eq!(
 978            truncate_lines_and_trailoff(text, 2),
 979            r#"Line 1
 980…"#
 981        );
 982
 983        assert_eq!(
 984            truncate_lines_and_trailoff(text, 3),
 985            r#"Line 1
 986Line 2
 987…"#
 988        );
 989
 990        assert_eq!(
 991            truncate_lines_and_trailoff(text, 4),
 992            r#"Line 1
 993Line 2
 994Line 3"#
 995        );
 996    }
 997
 998    #[test]
 999    fn test_expanded_and_wrapped_usize_range() {
1000        // Neither wrap
1001        assert_eq!(
1002            expanded_and_wrapped_usize_range(2..4, 1, 1, 8).collect::<Vec<usize>>(),
1003            (1..5).collect::<Vec<usize>>()
1004        );
1005        // Start wraps
1006        assert_eq!(
1007            expanded_and_wrapped_usize_range(2..4, 3, 1, 8).collect::<Vec<usize>>(),
1008            ((0..5).chain(7..8)).collect::<Vec<usize>>()
1009        );
1010        // Start wraps all the way around
1011        assert_eq!(
1012            expanded_and_wrapped_usize_range(2..4, 5, 1, 8).collect::<Vec<usize>>(),
1013            (0..8).collect::<Vec<usize>>()
1014        );
1015        // Start wraps all the way around and past 0
1016        assert_eq!(
1017            expanded_and_wrapped_usize_range(2..4, 10, 1, 8).collect::<Vec<usize>>(),
1018            (0..8).collect::<Vec<usize>>()
1019        );
1020        // End wraps
1021        assert_eq!(
1022            expanded_and_wrapped_usize_range(3..5, 1, 4, 8).collect::<Vec<usize>>(),
1023            (0..1).chain(2..8).collect::<Vec<usize>>()
1024        );
1025        // End wraps all the way around
1026        assert_eq!(
1027            expanded_and_wrapped_usize_range(3..5, 1, 5, 8).collect::<Vec<usize>>(),
1028            (0..8).collect::<Vec<usize>>()
1029        );
1030        // End wraps all the way around and past the end
1031        assert_eq!(
1032            expanded_and_wrapped_usize_range(3..5, 1, 10, 8).collect::<Vec<usize>>(),
1033            (0..8).collect::<Vec<usize>>()
1034        );
1035        // Both start and end wrap
1036        assert_eq!(
1037            expanded_and_wrapped_usize_range(3..5, 4, 4, 8).collect::<Vec<usize>>(),
1038            (0..8).collect::<Vec<usize>>()
1039        );
1040    }
1041
1042    #[test]
1043    fn test_wrapped_usize_outward_from() {
1044        // No wrapping
1045        assert_eq!(
1046            wrapped_usize_outward_from(4, 2, 2, 10).collect::<Vec<usize>>(),
1047            vec![4, 5, 3, 6, 2]
1048        );
1049        // Wrapping at end
1050        assert_eq!(
1051            wrapped_usize_outward_from(8, 2, 3, 10).collect::<Vec<usize>>(),
1052            vec![8, 9, 7, 0, 6, 1]
1053        );
1054        // Wrapping at start
1055        assert_eq!(
1056            wrapped_usize_outward_from(1, 3, 2, 10).collect::<Vec<usize>>(),
1057            vec![1, 2, 0, 3, 9, 8]
1058        );
1059        // All values wrap around
1060        assert_eq!(
1061            wrapped_usize_outward_from(5, 10, 10, 8).collect::<Vec<usize>>(),
1062            vec![5, 6, 4, 7, 3, 0, 2, 1]
1063        );
1064        // None before / after
1065        assert_eq!(
1066            wrapped_usize_outward_from(3, 0, 0, 8).collect::<Vec<usize>>(),
1067            vec![3]
1068        );
1069        // Starting point already wrapped
1070        assert_eq!(
1071            wrapped_usize_outward_from(15, 2, 2, 10).collect::<Vec<usize>>(),
1072            vec![5, 6, 4, 7, 3]
1073        );
1074        // wrap_length of 0
1075        assert_eq!(
1076            wrapped_usize_outward_from(4, 2, 2, 0).collect::<Vec<usize>>(),
1077            Vec::<usize>::new()
1078        );
1079    }
1080
1081    #[test]
1082    fn test_split_with_ranges() {
1083        let input = "hi";
1084        let result = split_str_with_ranges(input, &|c| c == ' ');
1085
1086        assert_eq!(result.len(), 1);
1087        assert_eq!(result[0], (0..2, "hi"));
1088
1089        let input = "hΓ©lloπŸ¦€world";
1090        let result = split_str_with_ranges(input, &|c| c == 'πŸ¦€');
1091
1092        assert_eq!(result.len(), 2);
1093        assert_eq!(result[0], (0..6, "hΓ©llo")); // 'Γ©' is 2 bytes
1094        assert_eq!(result[1], (10..15, "world")); // 'πŸ¦€' is 4 bytes
1095    }
1096}