util.rs

  1pub mod arc_cow;
  2pub mod command;
  3pub mod fs;
  4pub mod paths;
  5pub mod serde;
  6#[cfg(any(test, feature = "test-support"))]
  7pub mod test;
  8
  9use anyhow::Result;
 10use futures::Future;
 11use itertools::Either;
 12use regex::Regex;
 13use std::sync::{LazyLock, OnceLock};
 14use std::{
 15    borrow::Cow,
 16    cmp::{self, Ordering},
 17    env,
 18    ops::{AddAssign, Range, RangeInclusive},
 19    panic::Location,
 20    pin::Pin,
 21    task::{Context, Poll},
 22    time::Instant,
 23};
 24use unicase::UniCase;
 25
 26#[cfg(unix)]
 27use anyhow::{anyhow, Context as _};
 28
 29pub use take_until::*;
 30
 31#[macro_export]
 32macro_rules! debug_panic {
 33    ( $($fmt_arg:tt)* ) => {
 34        if cfg!(debug_assertions) {
 35            panic!( $($fmt_arg)* );
 36        } else {
 37            let backtrace = std::backtrace::Backtrace::capture();
 38            log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
 39        }
 40    };
 41}
 42
 43pub fn truncate(s: &str, max_chars: usize) -> &str {
 44    match s.char_indices().nth(max_chars) {
 45        None => s,
 46        Some((idx, _)) => &s[..idx],
 47    }
 48}
 49
 50/// Removes characters from the end of the string if its length is greater than `max_chars` and
 51/// appends "..." to the string. Returns string unchanged if its length is smaller than max_chars.
 52pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
 53    debug_assert!(max_chars >= 5);
 54
 55    // If the string's byte length is <= max_chars, walking the string can be skipped since the
 56    // number of chars is <= the number of bytes.
 57    if s.len() <= max_chars {
 58        return s.to_string();
 59    }
 60    let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars);
 61    match truncation_ix {
 62        Some(index) => s[..index].to_string() + "",
 63        _ => s.to_string(),
 64    }
 65}
 66
 67/// Removes characters from the front of the string if its length is greater than `max_chars` and
 68/// prepends the string with "...". Returns string unchanged if its length is smaller than max_chars.
 69pub fn truncate_and_remove_front(s: &str, max_chars: usize) -> String {
 70    debug_assert!(max_chars >= 5);
 71
 72    // If the string's byte length is <= max_chars, walking the string can be skipped since the
 73    // number of chars is <= the number of bytes.
 74    if s.len() <= max_chars {
 75        return s.to_string();
 76    }
 77    let suffix_char_length = max_chars.saturating_sub(1);
 78    let truncation_ix = s
 79        .char_indices()
 80        .map(|(i, _)| i)
 81        .nth_back(suffix_char_length);
 82    match truncation_ix {
 83        Some(index) if index > 0 => "".to_string() + &s[index..],
 84        _ => s.to_string(),
 85    }
 86}
 87
 88/// Takes only `max_lines` from the string and, if there were more than `max_lines-1`, appends a
 89/// a newline and "..." to the string, so that `max_lines` are returned.
 90/// Returns string unchanged if its length is smaller than max_lines.
 91pub fn truncate_lines_and_trailoff(s: &str, max_lines: usize) -> String {
 92    let mut lines = s.lines().take(max_lines).collect::<Vec<_>>();
 93    if lines.len() > max_lines - 1 {
 94        lines.pop();
 95        lines.join("\n") + "\n"
 96    } else {
 97        lines.join("\n")
 98    }
 99}
100
101pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
102    let prev = *value;
103    *value += T::from(1);
104    prev
105}
106
107/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
108/// enforcing a maximum length. This also de-duplicates items. Sort the items according to the given callback. Before calling this,
109/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
110pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
111where
112    I: IntoIterator<Item = T>,
113    F: FnMut(&T, &T) -> Ordering,
114{
115    let mut start_index = 0;
116    for new_item in new_items {
117        if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
118            let index = start_index + i;
119            if vec.len() < limit {
120                vec.insert(index, new_item);
121            } else if index < vec.len() {
122                vec.pop();
123                vec.insert(index, new_item);
124            }
125            start_index = index;
126        }
127    }
128}
129
130pub fn truncate_to_bottom_n_sorted_by<T, F>(items: &mut Vec<T>, limit: usize, compare: &F)
131where
132    F: Fn(&T, &T) -> Ordering,
133{
134    if limit == 0 {
135        items.truncate(0);
136    }
137    if items.len() <= limit {
138        items.sort_by(compare);
139        return;
140    }
141    // When limit is near to items.len() it may be more efficient to sort the whole list and
142    // truncate, rather than always doing selection first as is done below. It's hard to analyze
143    // where the threshold for this should be since the quickselect style algorithm used by
144    // `select_nth_unstable_by` makes the prefix partially sorted, and so its work is not wasted -
145    // the expected number of comparisons needed by `sort_by` is less than it is for some arbitrary
146    // unsorted input.
147    items.select_nth_unstable_by(limit, compare);
148    items.truncate(limit);
149    items.sort_by(compare);
150}
151
152#[cfg(unix)]
153pub fn load_shell_from_passwd() -> Result<()> {
154    let buflen = match unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) } {
155        n if n < 0 => 1024,
156        n => n as usize,
157    };
158    let mut buffer = Vec::with_capacity(buflen);
159
160    let mut pwd: std::mem::MaybeUninit<libc::passwd> = std::mem::MaybeUninit::uninit();
161    let mut result: *mut libc::passwd = std::ptr::null_mut();
162
163    let uid = unsafe { libc::getuid() };
164    let status = unsafe {
165        libc::getpwuid_r(
166            uid,
167            pwd.as_mut_ptr(),
168            buffer.as_mut_ptr() as *mut libc::c_char,
169            buflen,
170            &mut result,
171        )
172    };
173    let entry = unsafe { pwd.assume_init() };
174
175    anyhow::ensure!(
176        status == 0,
177        "call to getpwuid_r failed. uid: {}, status: {}",
178        uid,
179        status
180    );
181    anyhow::ensure!(!result.is_null(), "passwd entry for uid {} not found", uid);
182    anyhow::ensure!(
183        entry.pw_uid == uid,
184        "passwd entry has different uid ({}) than getuid ({}) returned",
185        entry.pw_uid,
186        uid,
187    );
188
189    let shell = unsafe { std::ffi::CStr::from_ptr(entry.pw_shell).to_str().unwrap() };
190    if env::var("SHELL").map_or(true, |shell_env| shell_env != shell) {
191        log::info!(
192            "updating SHELL environment variable to value from passwd entry: {:?}",
193            shell,
194        );
195        env::set_var("SHELL", shell);
196    }
197
198    Ok(())
199}
200
201#[cfg(unix)]
202pub fn load_login_shell_environment() -> Result<()> {
203    let marker = "ZED_LOGIN_SHELL_START";
204    let shell = env::var("SHELL").context(
205        "SHELL environment variable is not assigned so we can't source login environment variables",
206    )?;
207
208    // If possible, we want to `cd` in the user's `$HOME` to trigger programs
209    // such as direnv, asdf, mise, ... to adjust the PATH. These tools often hook
210    // into shell's `cd` command (and hooks) to manipulate env.
211    // We do this so that we get the env a user would have when spawning a shell
212    // in home directory.
213    let shell_cmd_prefix = std::env::var_os("HOME")
214        .and_then(|home| home.into_string().ok())
215        .map(|home| format!("cd '{home}';"));
216
217    // The `exit 0` is the result of hours of debugging, trying to find out
218    // why running this command here, without `exit 0`, would mess
219    // up signal process for our process so that `ctrl-c` doesn't work
220    // anymore.
221    // We still don't know why `$SHELL -l -i -c '/usr/bin/env -0'`  would
222    // do that, but it does, and `exit 0` helps.
223    let shell_cmd = format!(
224        "{}printf '%s' {marker}; /usr/bin/env; exit 0;",
225        shell_cmd_prefix.as_deref().unwrap_or("")
226    );
227
228    let output = std::process::Command::new(&shell)
229        .args(["-l", "-i", "-c", &shell_cmd])
230        .output()
231        .context("failed to spawn login shell to source login environment variables")?;
232    if !output.status.success() {
233        Err(anyhow!("login shell exited with error"))?;
234    }
235
236    let stdout = String::from_utf8_lossy(&output.stdout);
237
238    if let Some(env_output_start) = stdout.find(marker) {
239        let env_output = &stdout[env_output_start + marker.len()..];
240
241        parse_env_output(env_output, |key, value| env::set_var(key, value));
242
243        log::info!(
244            "set environment variables from shell:{}, path:{}",
245            shell,
246            env::var("PATH").unwrap_or_default(),
247        );
248    }
249
250    Ok(())
251}
252
253/// Parse the result of calling `usr/bin/env` with no arguments
254pub fn parse_env_output(env: &str, mut f: impl FnMut(String, String)) {
255    let mut current_key: Option<String> = None;
256    let mut current_value: Option<String> = None;
257
258    for line in env.split_terminator('\n') {
259        if let Some(separator_index) = line.find('=') {
260            if !line[..separator_index].is_empty() {
261                if let Some((key, value)) = Option::zip(current_key.take(), current_value.take()) {
262                    f(key, value)
263                }
264                current_key = Some(line[..separator_index].to_string());
265                current_value = Some(line[separator_index + 1..].to_string());
266                continue;
267            };
268        }
269        if let Some(value) = current_value.as_mut() {
270            value.push('\n');
271            value.push_str(line);
272        }
273    }
274    if let Some((key, value)) = Option::zip(current_key.take(), current_value.take()) {
275        f(key, value)
276    }
277}
278
279pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
280    use serde_json::Value;
281
282    match (source, target) {
283        (Value::Object(source), Value::Object(target)) => {
284            for (key, value) in source {
285                if let Some(target) = target.get_mut(&key) {
286                    merge_json_value_into(value, target);
287                } else {
288                    target.insert(key.clone(), value);
289                }
290            }
291        }
292
293        (Value::Array(source), Value::Array(target)) => {
294            for value in source {
295                target.push(value);
296            }
297        }
298
299        (source, target) => *target = source,
300    }
301}
302
303pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
304    use serde_json::Value;
305    if let Value::Object(source_object) = source {
306        let target_object = if let Value::Object(target) = target {
307            target
308        } else {
309            *target = Value::Object(Default::default());
310            target.as_object_mut().unwrap()
311        };
312        for (key, value) in source_object {
313            if let Some(target) = target_object.get_mut(&key) {
314                merge_non_null_json_value_into(value, target);
315            } else if !value.is_null() {
316                target_object.insert(key.clone(), value);
317            }
318        }
319    } else if !source.is_null() {
320        *target = source
321    }
322}
323
324pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
325    static ZED_MEASUREMENTS: OnceLock<bool> = OnceLock::new();
326    let zed_measurements = ZED_MEASUREMENTS.get_or_init(|| {
327        env::var("ZED_MEASUREMENTS")
328            .map(|measurements| measurements == "1" || measurements == "true")
329            .unwrap_or(false)
330    });
331
332    if *zed_measurements {
333        let start = Instant::now();
334        let result = f();
335        let elapsed = start.elapsed();
336        eprintln!("{}: {:?}", label, elapsed);
337        result
338    } else {
339        f()
340    }
341}
342
343pub fn iterate_expanded_and_wrapped_usize_range(
344    range: Range<usize>,
345    additional_before: usize,
346    additional_after: usize,
347    wrap_length: usize,
348) -> impl Iterator<Item = usize> {
349    let start_wraps = range.start < additional_before;
350    let end_wraps = wrap_length < range.end + additional_after;
351    if start_wraps && end_wraps {
352        Either::Left(0..wrap_length)
353    } else if start_wraps {
354        let wrapped_start = (range.start + wrap_length).saturating_sub(additional_before);
355        if wrapped_start <= range.end {
356            Either::Left(0..wrap_length)
357        } else {
358            Either::Right((0..range.end + additional_after).chain(wrapped_start..wrap_length))
359        }
360    } else if end_wraps {
361        let wrapped_end = range.end + additional_after - wrap_length;
362        if range.start <= wrapped_end {
363            Either::Left(0..wrap_length)
364        } else {
365            Either::Right((0..wrapped_end).chain(range.start - additional_before..wrap_length))
366        }
367    } else {
368        Either::Left((range.start - additional_before)..(range.end + additional_after))
369    }
370}
371
372pub trait ResultExt<E> {
373    type Ok;
374
375    fn log_err(self) -> Option<Self::Ok>;
376    /// Assert that this result should never be an error in development or tests.
377    fn debug_assert_ok(self, reason: &str) -> Self;
378    fn warn_on_err(self) -> Option<Self::Ok>;
379    fn anyhow(self) -> anyhow::Result<Self::Ok>
380    where
381        E: Into<anyhow::Error>;
382}
383
384impl<T, E> ResultExt<E> for Result<T, E>
385where
386    E: std::fmt::Debug,
387{
388    type Ok = T;
389
390    #[track_caller]
391    fn log_err(self) -> Option<T> {
392        match self {
393            Ok(value) => Some(value),
394            Err(error) => {
395                log_error_with_caller(*Location::caller(), error, log::Level::Error);
396                None
397            }
398        }
399    }
400
401    #[track_caller]
402    fn debug_assert_ok(self, reason: &str) -> Self {
403        if let Err(error) = &self {
404            debug_panic!("{reason} - {error:?}");
405        }
406        self
407    }
408
409    #[track_caller]
410    fn warn_on_err(self) -> Option<T> {
411        match self {
412            Ok(value) => Some(value),
413            Err(error) => {
414                log_error_with_caller(*Location::caller(), error, log::Level::Warn);
415                None
416            }
417        }
418    }
419
420    fn anyhow(self) -> anyhow::Result<T>
421    where
422        E: Into<anyhow::Error>,
423    {
424        self.map_err(Into::into)
425    }
426}
427
428fn log_error_with_caller<E>(caller: core::panic::Location<'_>, error: E, level: log::Level)
429where
430    E: std::fmt::Debug,
431{
432    #[cfg(not(target_os = "windows"))]
433    let file = caller.file();
434    #[cfg(target_os = "windows")]
435    let file = caller.file().replace('\\', "/");
436    // In this codebase, the first segment of the file path is
437    // the 'crates' folder, followed by the crate name.
438    let target = file.split('/').nth(1);
439
440    log::logger().log(
441        &log::Record::builder()
442            .target(target.unwrap_or(""))
443            .module_path(target)
444            .args(format_args!("{:?}", error))
445            .file(Some(caller.file()))
446            .line(Some(caller.line()))
447            .level(level)
448            .build(),
449    );
450}
451
452pub fn log_err<E: std::fmt::Debug>(error: &E) {
453    log_error_with_caller(*Location::caller(), error, log::Level::Warn);
454}
455
456pub trait TryFutureExt {
457    fn log_err(self) -> LogErrorFuture<Self>
458    where
459        Self: Sized;
460
461    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
462    where
463        Self: Sized;
464
465    fn warn_on_err(self) -> LogErrorFuture<Self>
466    where
467        Self: Sized;
468    fn unwrap(self) -> UnwrapFuture<Self>
469    where
470        Self: Sized;
471}
472
473impl<F, T, E> TryFutureExt for F
474where
475    F: Future<Output = Result<T, E>>,
476    E: std::fmt::Debug,
477{
478    #[track_caller]
479    fn log_err(self) -> LogErrorFuture<Self>
480    where
481        Self: Sized,
482    {
483        let location = Location::caller();
484        LogErrorFuture(self, log::Level::Error, *location)
485    }
486
487    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
488    where
489        Self: Sized,
490    {
491        LogErrorFuture(self, log::Level::Error, location)
492    }
493
494    #[track_caller]
495    fn warn_on_err(self) -> LogErrorFuture<Self>
496    where
497        Self: Sized,
498    {
499        let location = Location::caller();
500        LogErrorFuture(self, log::Level::Warn, *location)
501    }
502
503    fn unwrap(self) -> UnwrapFuture<Self>
504    where
505        Self: Sized,
506    {
507        UnwrapFuture(self)
508    }
509}
510
511#[must_use]
512pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
513
514impl<F, T, E> Future for LogErrorFuture<F>
515where
516    F: Future<Output = Result<T, E>>,
517    E: std::fmt::Debug,
518{
519    type Output = Option<T>;
520
521    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
522        let level = self.1;
523        let location = self.2;
524        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
525        match inner.poll(cx) {
526            Poll::Ready(output) => Poll::Ready(match output {
527                Ok(output) => Some(output),
528                Err(error) => {
529                    log_error_with_caller(location, error, level);
530                    None
531                }
532            }),
533            Poll::Pending => Poll::Pending,
534        }
535    }
536}
537
538pub struct UnwrapFuture<F>(F);
539
540impl<F, T, E> Future for UnwrapFuture<F>
541where
542    F: Future<Output = Result<T, E>>,
543    E: std::fmt::Debug,
544{
545    type Output = T;
546
547    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
548        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
549        match inner.poll(cx) {
550            Poll::Ready(result) => Poll::Ready(result.unwrap()),
551            Poll::Pending => Poll::Pending,
552        }
553    }
554}
555
556pub struct Deferred<F: FnOnce()>(Option<F>);
557
558impl<F: FnOnce()> Deferred<F> {
559    /// Drop without running the deferred function.
560    pub fn abort(mut self) {
561        self.0.take();
562    }
563}
564
565impl<F: FnOnce()> Drop for Deferred<F> {
566    fn drop(&mut self) {
567        if let Some(f) = self.0.take() {
568            f()
569        }
570    }
571}
572
573/// Run the given function when the returned value is dropped (unless it's cancelled).
574#[must_use]
575pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
576    Deferred(Some(f))
577}
578
579#[cfg(any(test, feature = "test-support"))]
580mod rng {
581    use rand::{seq::SliceRandom, Rng};
582    pub struct RandomCharIter<T: Rng> {
583        rng: T,
584        simple_text: bool,
585    }
586
587    impl<T: Rng> RandomCharIter<T> {
588        pub fn new(rng: T) -> Self {
589            Self {
590                rng,
591                simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
592            }
593        }
594
595        pub fn with_simple_text(mut self) -> Self {
596            self.simple_text = true;
597            self
598        }
599    }
600
601    impl<T: Rng> Iterator for RandomCharIter<T> {
602        type Item = char;
603
604        fn next(&mut self) -> Option<Self::Item> {
605            if self.simple_text {
606                return if self.rng.gen_range(0..100) < 5 {
607                    Some('\n')
608                } else {
609                    Some(self.rng.gen_range(b'a'..b'z' + 1).into())
610                };
611            }
612
613            match self.rng.gen_range(0..100) {
614                // whitespace
615                0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
616                // two-byte greek letters
617                20..=32 => char::from_u32(self.rng.gen_range(('α' as u32)..('ω' as u32 + 1))),
618                // // three-byte characters
619                33..=45 => ['✋', '✅', '❌', '❎', '⭐']
620                    .choose(&mut self.rng)
621                    .copied(),
622                // // four-byte characters
623                46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.rng).copied(),
624                // ascii letters
625                _ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
626            }
627        }
628    }
629}
630#[cfg(any(test, feature = "test-support"))]
631pub use rng::RandomCharIter;
632/// Get an embedded file as a string.
633pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
634    match A::get(path).unwrap().data {
635        Cow::Borrowed(bytes) => Cow::Borrowed(std::str::from_utf8(bytes).unwrap()),
636        Cow::Owned(bytes) => Cow::Owned(String::from_utf8(bytes).unwrap()),
637    }
638}
639
640/// Expands to an immediately-invoked function expression. Good for using the ? operator
641/// in functions which do not return an Option or Result.
642///
643/// Accepts a normal block, an async block, or an async move block.
644#[macro_export]
645macro_rules! maybe {
646    ($block:block) => {
647        (|| $block)()
648    };
649    (async $block:block) => {
650        (|| async $block)()
651    };
652    (async move $block:block) => {
653        (|| async move $block)()
654    };
655}
656
657pub trait RangeExt<T> {
658    fn sorted(&self) -> Self;
659    fn to_inclusive(&self) -> RangeInclusive<T>;
660    fn overlaps(&self, other: &Range<T>) -> bool;
661    fn contains_inclusive(&self, other: &Range<T>) -> bool;
662}
663
664impl<T: Ord + Clone> RangeExt<T> for Range<T> {
665    fn sorted(&self) -> Self {
666        cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
667    }
668
669    fn to_inclusive(&self) -> RangeInclusive<T> {
670        self.start.clone()..=self.end.clone()
671    }
672
673    fn overlaps(&self, other: &Range<T>) -> bool {
674        self.start < other.end && other.start < self.end
675    }
676
677    fn contains_inclusive(&self, other: &Range<T>) -> bool {
678        self.start <= other.start && other.end <= self.end
679    }
680}
681
682impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
683    fn sorted(&self) -> Self {
684        cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
685    }
686
687    fn to_inclusive(&self) -> RangeInclusive<T> {
688        self.clone()
689    }
690
691    fn overlaps(&self, other: &Range<T>) -> bool {
692        self.start() < &other.end && &other.start <= self.end()
693    }
694
695    fn contains_inclusive(&self, other: &Range<T>) -> bool {
696        self.start() <= &other.start && &other.end <= self.end()
697    }
698}
699
700/// A way to sort strings with starting numbers numerically first, falling back to alphanumeric one,
701/// case-insensitive.
702///
703/// This is useful for turning regular alphanumerically sorted sequences as `1-abc, 10, 11-def, .., 2, 21-abc`
704/// into `1-abc, 2, 10, 11-def, .., 21-abc`
705#[derive(Debug, PartialEq, Eq)]
706pub struct NumericPrefixWithSuffix<'a>(Option<u64>, &'a str);
707
708impl<'a> NumericPrefixWithSuffix<'a> {
709    pub fn from_numeric_prefixed_str(str: &'a str) -> Self {
710        let i = str.chars().take_while(|c| c.is_ascii_digit()).count();
711        let (prefix, remainder) = str.split_at(i);
712
713        let prefix = prefix.parse().ok();
714        Self(prefix, remainder)
715    }
716}
717
718/// When dealing with equality, we need to consider the case of the strings to achieve strict equality
719/// to handle cases like "a" < "A" instead of "a" == "A".
720impl Ord for NumericPrefixWithSuffix<'_> {
721    fn cmp(&self, other: &Self) -> Ordering {
722        match (self.0, other.0) {
723            (None, None) => UniCase::new(self.1)
724                .cmp(&UniCase::new(other.1))
725                .then_with(|| self.1.cmp(other.1).reverse()),
726            (None, Some(_)) => Ordering::Greater,
727            (Some(_), None) => Ordering::Less,
728            (Some(a), Some(b)) => a.cmp(&b).then_with(|| {
729                UniCase::new(self.1)
730                    .cmp(&UniCase::new(other.1))
731                    .then_with(|| self.1.cmp(other.1).reverse())
732            }),
733        }
734    }
735}
736
737impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
738    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
739        Some(self.cmp(other))
740    }
741}
742
743fn emoji_regex() -> &'static Regex {
744    static EMOJI_REGEX: LazyLock<Regex> =
745        LazyLock::new(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap());
746    &EMOJI_REGEX
747}
748
749/// Returns true if the given string consists of emojis only.
750/// E.g. "👨‍👩‍👧‍👧👋" will return true, but "👋!" will return false.
751pub fn word_consists_of_emojis(s: &str) -> bool {
752    let mut prev_end = 0;
753    for capture in emoji_regex().find_iter(s) {
754        if capture.start() != prev_end {
755            return false;
756        }
757        prev_end = capture.end();
758    }
759    prev_end == s.len()
760}
761
762#[cfg(test)]
763mod tests {
764    use super::*;
765
766    #[test]
767    fn test_extend_sorted() {
768        let mut vec = vec![];
769
770        extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
771        assert_eq!(vec, &[21, 17, 13, 8, 1]);
772
773        extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
774        assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
775
776        extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
777        assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
778    }
779
780    #[test]
781    fn test_truncate_to_bottom_n_sorted_by() {
782        let mut vec: Vec<u32> = vec![5, 2, 3, 4, 1];
783        truncate_to_bottom_n_sorted_by(&mut vec, 10, &u32::cmp);
784        assert_eq!(vec, &[1, 2, 3, 4, 5]);
785
786        vec = vec![5, 2, 3, 4, 1];
787        truncate_to_bottom_n_sorted_by(&mut vec, 5, &u32::cmp);
788        assert_eq!(vec, &[1, 2, 3, 4, 5]);
789
790        vec = vec![5, 2, 3, 4, 1];
791        truncate_to_bottom_n_sorted_by(&mut vec, 4, &u32::cmp);
792        assert_eq!(vec, &[1, 2, 3, 4]);
793
794        vec = vec![5, 2, 3, 4, 1];
795        truncate_to_bottom_n_sorted_by(&mut vec, 1, &u32::cmp);
796        assert_eq!(vec, &[1]);
797
798        vec = vec![5, 2, 3, 4, 1];
799        truncate_to_bottom_n_sorted_by(&mut vec, 0, &u32::cmp);
800        assert!(vec.is_empty());
801    }
802
803    #[test]
804    fn test_iife() {
805        fn option_returning_function() -> Option<()> {
806            None
807        }
808
809        let foo = maybe!({
810            option_returning_function()?;
811            Some(())
812        });
813
814        assert_eq!(foo, None);
815    }
816
817    #[test]
818    fn test_truncate_and_trailoff() {
819        assert_eq!(truncate_and_trailoff("", 5), "");
820        assert_eq!(truncate_and_trailoff("aaaaaa", 7), "aaaaaa");
821        assert_eq!(truncate_and_trailoff("aaaaaa", 6), "aaaaaa");
822        assert_eq!(truncate_and_trailoff("aaaaaa", 5), "aaaaa…");
823        assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
824        assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
825        assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…");
826    }
827
828    #[test]
829    fn test_truncate_and_remove_front() {
830        assert_eq!(truncate_and_remove_front("", 5), "");
831        assert_eq!(truncate_and_remove_front("aaaaaa", 7), "aaaaaa");
832        assert_eq!(truncate_and_remove_front("aaaaaa", 6), "aaaaaa");
833        assert_eq!(truncate_and_remove_front("aaaaaa", 5), "…aaaaa");
834        assert_eq!(truncate_and_remove_front("èèèèèè", 7), "èèèèèè");
835        assert_eq!(truncate_and_remove_front("èèèèèè", 6), "èèèèèè");
836        assert_eq!(truncate_and_remove_front("èèèèèè", 5), "…èèèèè");
837    }
838
839    #[test]
840    fn test_numeric_prefix_str_method() {
841        let target = "1a";
842        assert_eq!(
843            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
844            NumericPrefixWithSuffix(Some(1), "a")
845        );
846
847        let target = "12ab";
848        assert_eq!(
849            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
850            NumericPrefixWithSuffix(Some(12), "ab")
851        );
852
853        let target = "12_ab";
854        assert_eq!(
855            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
856            NumericPrefixWithSuffix(Some(12), "_ab")
857        );
858
859        let target = "1_2ab";
860        assert_eq!(
861            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
862            NumericPrefixWithSuffix(Some(1), "_2ab")
863        );
864
865        let target = "1.2";
866        assert_eq!(
867            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
868            NumericPrefixWithSuffix(Some(1), ".2")
869        );
870
871        let target = "1.2_a";
872        assert_eq!(
873            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
874            NumericPrefixWithSuffix(Some(1), ".2_a")
875        );
876
877        let target = "12.2_a";
878        assert_eq!(
879            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
880            NumericPrefixWithSuffix(Some(12), ".2_a")
881        );
882
883        let target = "12a.2_a";
884        assert_eq!(
885            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
886            NumericPrefixWithSuffix(Some(12), "a.2_a")
887        );
888    }
889
890    #[test]
891    fn test_numeric_prefix_with_suffix() {
892        let mut sorted = vec!["1-abc", "10", "11def", "2", "21-abc"];
893        sorted.sort_by_key(|s| NumericPrefixWithSuffix::from_numeric_prefixed_str(s));
894        assert_eq!(sorted, ["1-abc", "2", "10", "11def", "21-abc"]);
895
896        for numeric_prefix_less in ["numeric_prefix_less", "aaa", "~™£"] {
897            assert_eq!(
898                NumericPrefixWithSuffix::from_numeric_prefixed_str(numeric_prefix_less),
899                NumericPrefixWithSuffix(None, numeric_prefix_less),
900                "String without numeric prefix `{numeric_prefix_less}` should not be converted into NumericPrefixWithSuffix"
901            )
902        }
903    }
904
905    #[test]
906    fn test_word_consists_of_emojis() {
907        let words_to_test = vec![
908            ("👨‍👩‍👧‍👧👋🥒", true),
909            ("👋", true),
910            ("!👋", false),
911            ("👋!", false),
912            ("👋 ", false),
913            (" 👋", false),
914            ("Test", false),
915        ];
916
917        for (text, expected_result) in words_to_test {
918            assert_eq!(word_consists_of_emojis(text), expected_result);
919        }
920    }
921
922    #[test]
923    fn test_truncate_lines_and_trailoff() {
924        let text = r#"Line 1
925Line 2
926Line 3"#;
927
928        assert_eq!(
929            truncate_lines_and_trailoff(text, 2),
930            r#"Line 1
931…"#
932        );
933
934        assert_eq!(
935            truncate_lines_and_trailoff(text, 3),
936            r#"Line 1
937Line 2
938…"#
939        );
940
941        assert_eq!(
942            truncate_lines_and_trailoff(text, 4),
943            r#"Line 1
944Line 2
945Line 3"#
946        );
947    }
948
949    #[test]
950    fn test_iterate_expanded_and_wrapped_usize_range() {
951        // Neither wrap
952        assert_eq!(
953            iterate_expanded_and_wrapped_usize_range(2..4, 1, 1, 8).collect::<Vec<usize>>(),
954            (1..5).collect::<Vec<usize>>()
955        );
956        // Start wraps
957        assert_eq!(
958            iterate_expanded_and_wrapped_usize_range(2..4, 3, 1, 8).collect::<Vec<usize>>(),
959            ((0..5).chain(7..8)).collect::<Vec<usize>>()
960        );
961        // Start wraps all the way around
962        assert_eq!(
963            iterate_expanded_and_wrapped_usize_range(2..4, 5, 1, 8).collect::<Vec<usize>>(),
964            (0..8).collect::<Vec<usize>>()
965        );
966        // Start wraps all the way around and past 0
967        assert_eq!(
968            iterate_expanded_and_wrapped_usize_range(2..4, 10, 1, 8).collect::<Vec<usize>>(),
969            (0..8).collect::<Vec<usize>>()
970        );
971        // End wraps
972        assert_eq!(
973            iterate_expanded_and_wrapped_usize_range(3..5, 1, 4, 8).collect::<Vec<usize>>(),
974            (0..1).chain(2..8).collect::<Vec<usize>>()
975        );
976        // End wraps all the way around
977        assert_eq!(
978            iterate_expanded_and_wrapped_usize_range(3..5, 1, 5, 8).collect::<Vec<usize>>(),
979            (0..8).collect::<Vec<usize>>()
980        );
981        // End wraps all the way around and past the end
982        assert_eq!(
983            iterate_expanded_and_wrapped_usize_range(3..5, 1, 10, 8).collect::<Vec<usize>>(),
984            (0..8).collect::<Vec<usize>>()
985        );
986        // Both start and end wrap
987        assert_eq!(
988            iterate_expanded_and_wrapped_usize_range(3..5, 4, 4, 8).collect::<Vec<usize>>(),
989            (0..8).collect::<Vec<usize>>()
990        );
991    }
992}