add combine and from_string
This commit is contained in:
parent
682dff9243
commit
96df3dd8ba
2 changed files with 44 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "clippet"
|
name = "clippet"
|
||||||
description = "Provides a teeny way to have some nice byte structures that's easy to use."
|
description = "Provides a teeny way to have some nice byte structures that's easy to use."
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Celeste <colean@colean.cc>"]
|
authors = ["Celeste <colean@colean.cc>"]
|
||||||
license = "LGPL-3.0-or-later"
|
license = "LGPL-3.0-or-later"
|
||||||
|
|
43
src/lib.rs
43
src/lib.rs
|
@ -120,4 +120,47 @@ impl Clippet {
|
||||||
pub fn len(&self) -> i32 {
|
pub fn len(&self) -> i32 {
|
||||||
return self.get_size()
|
return self.get_size()
|
||||||
}
|
}
|
||||||
|
/// Takes a string and splits it into sequences of bytes within a Clippet.
|
||||||
|
/// It is recommended to use smaller byte sequences, however this is user-configurable.
|
||||||
|
/// This uses a string reference.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let data = Clippet::from_string("Lorem ipsum dolor amet...");
|
||||||
|
/// ```
|
||||||
|
pub fn from_string(str: &str, size: usize) -> Clippet {
|
||||||
|
let mut i = false;
|
||||||
|
let mut data: Vec<BytesMut> = vec![];
|
||||||
|
let string = str.as_bytes();
|
||||||
|
let mut reader = BufReader::with_capacity(size, string);
|
||||||
|
while !i {
|
||||||
|
let buffer = reader.fill_buf().unwrap();
|
||||||
|
let bytes = BytesMut::from(buffer);
|
||||||
|
data.push(bytes);
|
||||||
|
let length = buffer.len();
|
||||||
|
if length < size { i = true; }
|
||||||
|
reader.consume(length);
|
||||||
|
}
|
||||||
|
let clip = Clippet { label: "".to_string(), size: data.len() as i32, data: data };
|
||||||
|
return clip
|
||||||
|
|
||||||
|
}
|
||||||
|
/// Consumes the Clippet, combines all of it's byte sequences and outputs them as a String literal.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let data = Clippet::from_string("Hello, World!");
|
||||||
|
/// println!("{}", data.combine()); /// Hello, World!
|
||||||
|
/// ```
|
||||||
|
pub fn combine(self) -> String {
|
||||||
|
let mut string = "".to_string();
|
||||||
|
for v in self.data {
|
||||||
|
let y = v.as_ref();
|
||||||
|
let x = String::from_utf8_lossy(y).to_string();
|
||||||
|
if string == "".to_string() {
|
||||||
|
string = x;
|
||||||
|
} else {
|
||||||
|
string = format!("{}{}", string, x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue