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.
yuurei/src/state/local/delta.rs

164 lines
4.0 KiB

//! Deltas and applying them
use super::*;
const MAX_SINGLE_DELTA_SIZE: usize = 14;
/// Create a delta span from an input iterator.
///
/// This function can take no more than 255 chars from the input. The number of chars inserted is also returned as `u8`.
pub(super) fn delta_span<I>(from: I) -> ([char; MAX_SINGLE_DELTA_SIZE], u8)
where I: IntoIterator<Item = char>
{
let mut output: [char; MAX_SINGLE_DELTA_SIZE] = Default::default();
let mut sz: u8 = 0;
for (d, s) in output.iter_mut().zip(from.into_iter().take(usize::from(u8::MAX)))
{
*d = s;
sz += 1;
}
(output, sz)
}
/// Information about the delta to be applied in `Delta`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum DeltaKind
{
/// Append to the end of body. Equivilant to `Insert` with a location at `karada.scape.len()` (the end of the buffer). Might be removed idk
Append{
span: [char; MAX_SINGLE_DELTA_SIZE],
span_len: u8,
},
/// Insert `span_len` chars from `span` into body starting *at* `location` and moving ahead
Insert{
span: [char; MAX_SINGLE_DELTA_SIZE],
span_len: u8,
},
/// Remove `span_len` chars ahead of `location`. (INCLUSIVE)
RemoveAhead{
span_len: usize,
},
/// Remove `span_len` chars behind this `location`. (EXCLUSIVE)
RemoveBehind{
span_len: usize,
},
/// Remove char at `location`
RemoveSingle,
/// Remove entire post body
Clear,
}
/// A delta to apply to `Karada`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct Delta
{
/// Location to insert into. This is the INclusive range of: 0..=(karada.scape.len()).
///
/// Insertions off the end of the buffer are to be appened instead.
location: usize,
/// The kind of delta t oinsert
kind: DeltaKind,
}
/// Static assertion: `MAX_SINGLE_DELTA_SIZE` can fit into `u8`.
const _: [u8;(MAX_SINGLE_DELTA_SIZE < (!0u8 as usize)) as usize] = [0];
impl Delta
{
pub fn insert(&self, inserter: &mut MessageSpan)
{
match self.kind {
DeltaKind::Append{span, span_len} => {
inserter.extend_from_slice(&span[..usize::from(span_len)]);
},
DeltaKind::Insert{span, span_len} => {
let span = &span[..usize::from(span_len)];
if self.location == inserter.len() {
inserter.extend_from_slice(span);
} else if span.len() == 1 {
inserter.insert(self.location, span[0]);
} else if span.len() > 1 {
// reserve the extra space
inserter.reserve(span.len());
// shift everything across, replacing with the new values
let splice: Vec<_> = inserter.splice(self.location.., span.iter().cloned()).collect();
// add tail back
inserter.extend(splice);
}
},
_ => unimplemented!(),
}
}
}
#[cfg(test)]
mod tests
{
use super::*;
macro_rules! insert {
($expects:literal; $start:literal, $ins:literal, $where:expr) => {
{
let mut message: Vec<char> = $start.chars().collect();
let delta = {
let (span, span_len) = delta_span($ins.chars());
Delta {
location: $where,
kind: DeltaKind::Insert{span, span_len},
}
};
println!("from: {:?}", message);
println!("delta: {:?}", delta);
delta.insert(&mut message);
assert_eq!(&message.into_iter().collect::<String>()[..], $expects);
}
};
}
#[test]
fn insert_body()
{
insert!("123456789"; "126789", "345", 2);
}
#[test]
fn insert_end()
{
insert!("123456789"; "1289", "34567", 2);
}
#[test]
fn insert_end_rev()
{
insert!("123456789"; "1234569", "78", 6);
}
#[test]
fn insert_begin_rev()
{
insert!("123456789"; "1456789", "23", 1);
}
#[test]
fn insert_begin()
{
insert!("123456789"; "789", "123456", 0);
}
#[test]
fn insert_end_f()
{
insert!("123456789"; "123", "456789", 3);
}
#[test]
fn insert_end_f_rev()
{
insert!("123456789"; "1234567", "89", 7);
}
#[test]
fn insert_single()
{
insert!("123456789"; "12346789", "5",4);
}
}