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