1 //! Missing batteries for standard libraries.
2 use std::{cmp::Ordering, ops, process, time::Instant};
8 pub fn is_ci() -> bool {
9 option_env!("CI").is_some()
13 pub fn timeit(label: &'static str) -> impl Drop {
21 eprintln!("{}: {:.2?}", self.label, self.start.elapsed())
25 Guard { label, start: Instant::now() }
28 fn to_snake_case<F: Fn(&char) -> char>(s: &str, change_case: F) -> String {
29 let mut buf = String::with_capacity(s.len());
32 // `&& prev` is required to not insert `_` before the first symbol.
33 if c.is_ascii_uppercase() && prev {
34 // This check is required to not translate `Weird_Case` into `weird__case`.
35 if !buf.ends_with('_') {
41 buf.push(change_case(&c));
46 pub fn to_lower_snake_case(s: &str) -> String {
47 to_snake_case(s, char::to_ascii_lowercase)
50 pub fn to_upper_snake_case(s: &str) -> String {
51 to_snake_case(s, char::to_ascii_uppercase)
54 pub fn replace(buf: &mut String, from: char, to: &str) {
55 if !buf.contains(from) {
58 // FIXME: do this in place.
59 *buf = buf.replace(from, to)
62 // https://github.com/rust-lang/rust/issues/74773
63 pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
64 let mut split = haystack.splitn(2, delim);
65 let prefix = split.next()?;
66 let suffix = split.next()?;
67 Some((prefix, suffix))
69 pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
70 let mut split = haystack.rsplitn(2, delim);
71 let suffix = split.next()?;
72 let prefix = split.next()?;
73 Some((prefix, suffix))
76 pub fn trim_indent(mut text: &str) -> String {
77 if text.starts_with('\n') {
82 .filter(|it| !it.trim().is_empty())
83 .map(|it| it.len() - it.trim_start().len())
89 if line.len() <= indent {
90 line.trim_start_matches(' ')
99 pub fn lines_with_ends(text: &str) -> LinesWithEnds {
100 LinesWithEnds { text }
103 pub struct LinesWithEnds<'a> {
107 impl<'a> Iterator for LinesWithEnds<'a> {
109 fn next(&mut self) -> Option<&'a str> {
110 if self.text.is_empty() {
113 let idx = self.text.find('\n').map_or(self.text.len(), |it| it + 1);
114 let (res, next) = self.text.split_at(idx);
120 /// Returns `idx` such that:
122 /// ∀ x in slice[..idx]: pred(x)
123 /// && ∀ x in slice[idx..]: !pred(x)
125 /// https://github.com/rust-lang/rust/issues/73831
126 pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
128 P: FnMut(&T) -> bool,
131 let mut right = slice.len();
133 while left != right {
134 let mid = left + (right - left) / 2;
136 // When left < right, left <= mid < right.
137 // Therefore left always increases and right always decreases,
138 // and either of them is selected.
139 // In both cases left <= right is satisfied.
140 // Therefore if left < right in a step,
141 // left <= right is satisfied in the next step.
142 // Therefore as long as left != right, 0 <= left < right <= len is satisfied
143 // and if this case 0 <= mid < len is satisfied too.
144 let value = unsafe { slice.get_unchecked(mid) };
155 pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> (usize, usize)
157 F: FnMut(&T) -> Ordering,
159 let start = partition_point(slice, |it| key(it) == Ordering::Less);
160 let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal);
164 pub struct JodChild(pub process::Child);
166 impl ops::Deref for JodChild {
167 type Target = process::Child;
168 fn deref(&self) -> &process::Child {
173 impl ops::DerefMut for JodChild {
174 fn deref_mut(&mut self) -> &mut process::Child {
179 impl Drop for JodChild {
181 let _ = self.0.kill();
190 fn test_trim_indent() {
191 assert_eq!(trim_indent(""), "");
209 assert_eq!(trim_indent(" hello\n world\n"), "hello\nworld\n");
218 "fn main() {\n return 92;\n}\n"