Skip to main content

util/
util.rs

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