1 //! Missing batteries for standard libraries.
2 use std::{cmp::Ordering, ops, time::Instant};
8 pub use always_assert::{always, never};
11 pub fn is_ci() -> bool {
12 option_env!("CI").is_some()
16 pub fn timeit(label: &'static str) -> impl Drop {
17 let start = Instant::now();
18 defer(move || eprintln!("{}: {:.2?}", label, start.elapsed()))
21 /// Prints backtrace to stderr, useful for debugging.
22 #[cfg(feature = "backtrace")]
23 pub fn print_backtrace() {
24 let bt = backtrace::Backtrace::new();
25 eprintln!("{:?}", bt);
27 #[cfg(not(feature = "backtrace"))]
28 pub fn print_backtrace() {
30 r#"Enable the backtrace feature.
31 Uncomment `default = [ "backtrace" ]` in `crates/stdx/Cargo.toml`.
36 pub fn to_lower_snake_case(s: &str) -> String {
37 to_snake_case(s, char::to_ascii_lowercase)
39 pub fn to_upper_snake_case(s: &str) -> String {
40 to_snake_case(s, char::to_ascii_uppercase)
42 fn to_snake_case<F: Fn(&char) -> char>(s: &str, change_case: F) -> String {
43 let mut buf = String::with_capacity(s.len());
46 // `&& prev` is required to not insert `_` before the first symbol.
47 if c.is_ascii_uppercase() && prev {
48 // This check is required to not translate `Weird_Case` into `weird__case`.
49 if !buf.ends_with('_') {
55 buf.push(change_case(&c));
60 pub fn replace(buf: &mut String, from: char, to: &str) {
61 if !buf.contains(from) {
64 // FIXME: do this in place.
65 *buf = buf.replace(from, to)
68 // https://github.com/rust-lang/rust/issues/74773
69 pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
70 let mut split = haystack.splitn(2, delim);
71 let prefix = split.next()?;
72 let suffix = split.next()?;
73 Some((prefix, suffix))
75 pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
76 let mut split = haystack.rsplitn(2, delim);
77 let suffix = split.next()?;
78 let prefix = split.next()?;
79 Some((prefix, suffix))
82 pub fn trim_indent(mut text: &str) -> String {
83 if text.starts_with('\n') {
88 .filter(|it| !it.trim().is_empty())
89 .map(|it| it.len() - it.trim_start().len())
95 if line.len() <= indent {
96 line.trim_start_matches(' ')
105 pub fn lines_with_ends(text: &str) -> LinesWithEnds {
106 LinesWithEnds { text }
109 pub struct LinesWithEnds<'a> {
113 impl<'a> Iterator for LinesWithEnds<'a> {
115 fn next(&mut self) -> Option<&'a str> {
116 if self.text.is_empty() {
119 let idx = self.text.find('\n').map_or(self.text.len(), |it| it + 1);
120 let (res, next) = self.text.split_at(idx);
126 /// Returns `idx` such that:
129 /// ∀ x in slice[..idx]: pred(x)
130 /// && ∀ x in slice[idx..]: !pred(x)
133 /// https://github.com/rust-lang/rust/issues/73831
134 pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
136 P: FnMut(&T) -> bool,
139 let mut right = slice.len();
141 while left != right {
142 let mid = left + (right - left) / 2;
144 // When left < right, left <= mid < right.
145 // Therefore left always increases and right always decreases,
146 // and either of them is selected.
147 // In both cases left <= right is satisfied.
148 // Therefore if left < right in a step,
149 // left <= right is satisfied in the next step.
150 // Therefore as long as left != right, 0 <= left < right <= len is satisfied
151 // and if this case 0 <= mid < len is satisfied too.
152 let value = unsafe { slice.get_unchecked(mid) };
163 pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<usize>
165 F: FnMut(&T) -> Ordering,
167 let start = partition_point(slice, |it| key(it) == Ordering::Less);
168 let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal);
173 pub fn defer<F: FnOnce()>(f: F) -> impl Drop {
174 struct D<F: FnOnce()>(Option<F>);
175 impl<F: FnOnce()> Drop for D<F> {
177 if let Some(f) = self.0.take() {
186 pub struct JodChild(pub std::process::Child);
188 impl ops::Deref for JodChild {
189 type Target = std::process::Child;
190 fn deref(&self) -> &std::process::Child {
195 impl ops::DerefMut for JodChild {
196 fn deref_mut(&mut self) -> &mut std::process::Child {
201 impl Drop for JodChild {
203 let _ = self.0.kill();
204 let _ = self.0.wait();
209 pub fn into_inner(self) -> std::process::Child {
210 // SAFETY: repr transparent
211 unsafe { std::mem::transmute::<JodChild, std::process::Child>(self) }
220 fn test_trim_indent() {
221 assert_eq!(trim_indent(""), "");
239 assert_eq!(trim_indent(" hello\n world\n"), "hello\nworld\n");
248 "fn main() {\n return 92;\n}\n"