This commit is contained in:
Ed Guloien
2026-05-28 16:10:26 -04:00
parent b21662247d
commit d3f532ce5c
+23 -9
View File
@@ -1,4 +1,3 @@
#[derive(PartialEq, Debug, Clone, Copy)] #[derive(PartialEq, Debug, Clone, Copy)]
struct Range { struct Range {
// todo: consider just using a map instead // todo: consider just using a map instead
@@ -12,7 +11,7 @@ enum RangeLocation {
LeftEnd, LeftEnd,
RightEnd, RightEnd,
Middle, Middle,
No No,
} }
impl Range { impl Range {
@@ -25,7 +24,7 @@ impl Range {
return RangeLocation::LeftEnd; return RangeLocation::LeftEnd;
} }
if index >= self.end { if index >= self.end {
return RangeLocation::No return RangeLocation::No;
} }
if index == self.end - 1 { if index == self.end - 1 {
return RangeLocation::RightEnd; return RangeLocation::RightEnd;
@@ -44,14 +43,24 @@ pub struct Blob {
} }
impl Blob { impl Blob {
pub fn new() -> Self { Self {bytes: Vec::new(), valid_ranges: Vec::new()} } pub fn new() -> Self {
Self {
bytes: Vec::new(),
valid_ranges: Vec::new(),
}
}
pub fn append(&mut self, input: &[u8]) { pub fn append(&mut self, input: &[u8]) {
let new_range = Range {start: self.bytes.len(), end: self.bytes.len() + input.len()}; let new_range = Range {
start: self.bytes.len(),
end: self.bytes.len() + input.len(),
};
// we are appending so only have to check the last range // we are appending so only have to check the last range
match self.valid_ranges.last_mut() { match self.valid_ranges.last_mut() {
Some(range) if range.where_in_range(new_range.start) != RangeLocation::No => range.end = new_range.end, Some(range) if range.where_in_range(new_range.start) != RangeLocation::No => {
range.end = new_range.end
}
_ => self.valid_ranges.push(new_range), _ => self.valid_ranges.push(new_range),
} }
self.bytes.extend_from_slice(input); self.bytes.extend_from_slice(input);
@@ -64,9 +73,12 @@ impl Blob {
slice.swap_with_slice(output); slice.swap_with_slice(output);
// we copy existing entries into a new list to avoid iterator invalidation // we copy existing entries into a new list to avoid iterator invalidation
// of inserting while iterating // from inserting while iterating
// todo: might be a better way // todo: might be a better way
let clear_range = Range{ start, end: start + len }; let clear_range = Range {
start,
end: start + len,
};
let mut new_ranges = Vec::new(); let mut new_ranges = Vec::new();
let mut i = 0; let mut i = 0;
while i < self.valid_ranges.len() { while i < self.valid_ranges.len() {
@@ -86,7 +98,9 @@ impl Blob {
new_ranges.push(range); new_ranges.push(range);
} }
if let Some(er) = extra_range && !er.is_empty() { if let Some(er) = extra_range
&& !er.is_empty()
{
new_ranges.push(er); new_ranges.push(er);
} }
i += 1; i += 1;