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