util.rs

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