From e30c1c3fbf8f70336d985b2b73e5b0f45f3b95f5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Jan 2021 01:39:02 +0300 Subject: Simplify highlighting infra This also fixes the killer whale bug --- crates/stdx/src/lib.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'crates/stdx') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 5332edb09..5aacdb16e 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -1,5 +1,5 @@ //! Missing batteries for standard libraries. -use std::{ops, process, time::Instant}; +use std::{cmp::Ordering, ops, process, time::Instant}; mod macros; pub mod panic_context; @@ -117,7 +117,12 @@ impl<'a> Iterator for LinesWithEnds<'a> { } } -// https://github.com/rust-lang/rust/issues/73831 +/// Returns `idx` such that: +/// +/// ∀ x in slice[..idx]: pred(x) +/// && ∀ x in slice[idx..]: !pred(x) +/// +/// https://github.com/rust-lang/rust/issues/73831 pub fn partition_point(slice: &[T], mut pred: P) -> usize where P: FnMut(&T) -> bool, @@ -147,6 +152,15 @@ where left } +pub fn equal_range_by(slice: &[T], mut key: F) -> (usize, usize) +where + F: FnMut(&T) -> Ordering, +{ + let start = partition_point(slice, |it| key(it) == Ordering::Less); + let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal); + (start, len) +} + pub struct JodChild(pub process::Child); impl ops::Deref for JodChild { -- cgit v1.2.3