You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
966 B
49 lines
966 B
//! Extensions
|
|
|
|
pub trait Tuple2Ext<T, U>
|
|
{
|
|
fn swap(self) -> (U, T);
|
|
}
|
|
|
|
impl<T, U> Tuple2Ext<T, U> for (T, U)
|
|
{
|
|
#[inline(always)]
|
|
fn swap(self) -> (U, T) {
|
|
(self.1, self.0)
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
pub fn is_utf8_char_boundary(byte: u8) -> bool
|
|
{
|
|
//!
|
|
//! Copied from stdlib
|
|
(byte as i8) >= -0x40
|
|
}
|
|
|
|
pub trait StrExt
|
|
{
|
|
fn floor_char_boundary_impl(&self, index: usize) -> usize;
|
|
}
|
|
|
|
impl<T: ?Sized + AsRef<str>> StrExt for T
|
|
{
|
|
#[inline]
|
|
fn floor_char_boundary_impl(&self, index: usize) -> usize {
|
|
//!
|
|
//! copied from stdlib
|
|
let this = self.as_ref();
|
|
if index >= this.len() {
|
|
this.len()
|
|
} else {
|
|
let lower_bound = index.saturating_sub(3);
|
|
let new_index = this.as_bytes()[lower_bound..=index]
|
|
.iter()
|
|
.rposition(|&b| is_utf8_char_boundary(b));
|
|
|
|
// SAFETY: we know that the character boundary will be within four bytes
|
|
unsafe { lower_bound + new_index.unwrap_unchecked() }
|
|
}
|
|
}
|
|
}
|