1 //! Missing batteries for standard libraries.
2 use std::{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 // https://github.com/rust-lang/rust/issues/73831
121 pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
123 P: FnMut(&T) -> bool,
126 let mut right = slice.len();
128 while left != right {
129 let mid = left + (right - left) / 2;
131 // When left < right, left <= mid < right.
132 // Therefore left always increases and right always decreases,
133 // and either of them is selected.
134 // In both cases left <= right is satisfied.
135 // Therefore if left < right in a step,
136 // left <= right is satisfied in the next step.
137 // Therefore as long as left != right, 0 <= left < right <= len is satisfied
138 // and if this case 0 <= mid < len is satisfied too.
139 let value = unsafe { slice.get_unchecked(mid) };
150 pub struct JodChild(pub process::Child);
152 impl ops::Deref for JodChild {
153 type Target = process::Child;
154 fn deref(&self) -> &process::Child {
159 impl ops::DerefMut for JodChild {
160 fn deref_mut(&mut self) -> &mut process::Child {
165 impl Drop for JodChild {
167 let _ = self.0.kill();
176 fn test_trim_indent() {
177 assert_eq!(trim_indent(""), "");
195 assert_eq!(trim_indent(" hello\n world\n"), "hello\nworld\n");
204 "fn main() {\n return 92;\n}\n"