1 //! Missing batteries for standard libraries.
3 sync::atomic::{AtomicUsize, Ordering},
10 pub fn is_ci() -> bool {
11 option_env!("CI").is_some()
15 pub fn timeit(label: &'static str) -> impl Drop {
23 eprintln!("{}: {:.2?}", self.label, self.start.elapsed())
27 Guard { label, start: Instant::now() }
30 pub fn to_lower_snake_case(s: &str) -> String {
31 let mut buf = String::with_capacity(s.len());
34 if c.is_ascii_uppercase() && prev {
39 buf.push(c.to_ascii_lowercase());
44 pub fn replace(buf: &mut String, from: char, to: &str) {
45 if !buf.contains(from) {
48 // FIXME: do this in place.
49 *buf = buf.replace(from, to)
52 // https://github.com/rust-lang/rust/issues/74773
53 pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
54 let mut split = haystack.splitn(2, delim);
55 let prefix = split.next()?;
56 let suffix = split.next()?;
57 Some((prefix, suffix))
59 pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
60 let mut split = haystack.rsplitn(2, delim);
61 let suffix = split.next()?;
62 let prefix = split.next()?;
63 Some((prefix, suffix))
66 pub fn trim_indent(mut text: &str) -> String {
67 if text.starts_with('\n') {
72 .filter(|it| !it.trim().is_empty())
73 .map(|it| it.len() - it.trim_start().len())
79 if line.len() <= indent {
80 line.trim_start_matches(' ')
89 pub fn lines_with_ends(text: &str) -> LinesWithEnds {
90 LinesWithEnds { text }
93 pub struct LinesWithEnds<'a> {
97 impl<'a> Iterator for LinesWithEnds<'a> {
99 fn next(&mut self) -> Option<&'a str> {
100 if self.text.is_empty() {
103 let idx = self.text.find('\n').map_or(self.text.len(), |it| it + 1);
104 let (res, next) = self.text.split_at(idx);
110 // https://github.com/rust-lang/rust/issues/73831
111 pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
113 P: FnMut(&T) -> bool,
116 let mut right = slice.len();
118 while left != right {
119 let mid = left + (right - left) / 2;
121 // When left < right, left <= mid < right.
122 // Therefore left always increases and right always decreases,
123 // and either of them is selected.
124 // In both cases left <= right is satisfied.
125 // Therefore if left < right in a step,
126 // left <= right is satisfied in the next step.
127 // Therefore as long as left != right, 0 <= left < right <= len is satisfied
128 // and if this case 0 <= mid < len is satisfied too.
129 let value = unsafe { slice.get_unchecked(mid) };
140 pub struct RacyFlag(AtomicUsize);
143 pub const fn new() -> RacyFlag {
144 RacyFlag(AtomicUsize::new(!0))
147 pub fn get(&self, init: impl FnMut() -> bool) -> bool {
148 let mut init = Some(init);
149 self.get_impl(&mut || init.take().map_or(false, |mut f| f()))
152 fn get_impl(&self, init: &mut dyn FnMut() -> bool) -> bool {
153 match self.0.load(Ordering::Relaxed) {
158 self.0.store(if res { 1 } else { 0 }, Ordering::Relaxed);
170 fn test_trim_indent() {
171 assert_eq!(trim_indent(""), "");
189 assert_eq!(trim_indent(" hello\n world\n"), "hello\nworld\n");
198 "fn main() {\n return 92;\n}\n"