util.rs

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