This commit is contained in:
mehbark 2025-10-02 14:33:54 -04:00
commit 0100f92504
Signed by: mbk
GPG key ID: E333EC1335FFCCDB
5 changed files with 69 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "text-compression"
version = "0.1.0"

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "text-compression"
version = "0.1.0"
edition = "2024"
[dependencies]

15
src/main.rs Normal file
View file

@ -0,0 +1,15 @@
use std::io::{self, Read};
mod rle;
fn main() -> Result<(), io::Error> {
let mut buf = Vec::new();
let len_src = io::stdin().read_to_end(&mut buf)?;
println!("Original size: {len_src}");
let len_rle = rle::Encoder::new(&buf).count() * 2;
println!("Rle'd size: {len_rle}");
Ok(())
}

40
src/rle.rs Normal file
View file

@ -0,0 +1,40 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Run {
pub count: u8,
pub byte: u8,
}
#[derive(Debug, Clone)]
pub struct Encoder<'src> {
src: &'src [u8],
}
impl<'src> Encoder<'src> {
pub fn new(src: &'src [u8]) -> Self {
Self { src }
}
}
impl Iterator for Encoder<'_> {
type Item = Run;
fn next(&mut self) -> Option<Self::Item> {
let &byte = self.src.first()?;
let (count, _) = self
.src
.iter()
.copied()
.take(256)
.enumerate()
.find(|&(_i, c)| c != byte)
.unwrap_or((self.src.len(), 0));
self.src = &self.src[count..];
Some(Run {
count: count.try_into().unwrap(),
byte,
})
}
}