Skip to content

Commit

Permalink
refactored byte buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jun 28, 2024
1 parent f252a60 commit e502eef
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
/target
**/target
out.bin
1 change: 1 addition & 0 deletions .idea/hexo.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ exclude = [
"testcases"
]

[workspace]
members = ["hexo-io"]

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
pest = "2.7.9"
pest_derive = "2.7.9"
notify = "6.1.1"
console = "0.15.8"
hexo-io = { path = "hexo-io" }
6 changes: 6 additions & 0 deletions hexo-io/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "hexo-io"
version = "0.1.0"
edition = "2021"

[dependencies]
79 changes: 48 additions & 31 deletions src/compiler/util/byte_buffer.rs → hexo-io/src/byte_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
use crate::compiler::util::encoding::to_shrunk_bytes;
use std::fmt::{Debug, Formatter};
use std::string::FromUtf8Error;

#[derive(Clone)]
pub(crate) struct ByteBuffer {
pub struct ByteBuffer {
inner: Vec<u8>,
}

impl ByteBuffer {
pub(crate) fn new() -> Self {
ByteBuffer { inner: Vec::new() }
impl Debug for ByteBuffer {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("{:?}", self.inner))
}
}

pub(crate) fn from(vec: Vec<u8>) -> Self {
ByteBuffer { inner: vec }
impl ByteBuffer {
pub fn new() -> Self {
ByteBuffer { inner: Vec::new() }
}

pub(crate) fn push_byte(&mut self, byte: u8) {
pub fn push_byte(&mut self, byte: u8) {
self.inner.push(byte);
}

pub(crate) fn push_string(&mut self, string: String) {
pub fn push_string(&mut self, string: String) {
self.inner.extend_from_slice(string.as_bytes());
}

pub(crate) fn push_u32_shrunk(&mut self, num: u32) {
self.inner.extend(to_shrunk_bytes(num));
pub fn push_u32_shrunk(&mut self, num: u32) {
self.inner.extend(Self::_to_shrunk_bytes(num));
}

pub(crate) fn push_byte_buffer(&mut self, other: &ByteBuffer) {
self.inner.extend(other.as_vec());
pub fn push_byte_buffer(&mut self, other: &ByteBuffer) {
self.inner.extend(other.to_vec());
}

pub(crate) fn pad_left(&mut self, size: usize) {
/// Moves the byte buffer to the left by the specified [size]
pub fn pad_left(&mut self, size: usize) {
let padding = size.checked_sub(self.inner.len());
if let Some(padding) = padding {
if padding > 0 {
Expand All @@ -43,7 +45,8 @@ impl ByteBuffer {
}
}

pub(crate) fn pad_right(&mut self, size: usize) {
/// Moves the byte buffer to the right by the specified [size]
pub fn pad_right(&mut self, size: usize) {
let padding = size.checked_sub(self.inner.len());

if let Some(padding) = padding {
Expand All @@ -54,15 +57,12 @@ impl ByteBuffer {
}
}

pub(crate) fn len(&self) -> usize {
/// Returns the length of the byte buffer
pub fn len(&self) -> usize {
self.inner.len()
}

pub(crate) fn as_vec(&self) -> Vec<u8> {
self.inner.clone()
}

pub(crate) fn as_usize(&self) -> usize {
pub fn as_usize_unsafe(&self) -> usize {
let mut padded = self.clone();
padded.pad_left(4);
((padded.inner[0] as usize) << 24) +
Expand All @@ -71,13 +71,35 @@ impl ByteBuffer {
(padded.inner[3] as usize)
}

pub(crate) fn as_string(&self) -> Result<String, FromUtf8Error> {
/// Clones inner representation of the byte buffer and returns Vec<u8> of it
pub fn to_vec(&self) -> Vec<u8> {
self.inner.clone()
}

pub fn to_string(&self) -> Result<String, FromUtf8Error> {
String::from_utf8(self.inner.clone())
}

fn _to_shrunk_bytes(value: u32) -> Vec<u8> {
let mut bytes = Vec::new();
let mut value = value;
while value > 0 {
bytes.push((value & 0xFF) as u8);
value >>= 8;
}
bytes
}

}

impl From<Vec<u8>> for ByteBuffer {
fn from(value: Vec<u8>) -> Self {
ByteBuffer { inner: value }
}
}

mod test {
use crate::compiler::util::ByteBuffer;
use crate::byte_buffer::ByteBuffer;

#[test]
fn byte_push() {
Expand All @@ -87,7 +109,7 @@ mod test {

assert_eq!(buffer.len(), 2);

assert_eq!(buffer.as_vec(), vec![0x01, 0x02]);
assert_eq!(buffer.to_vec(), vec![0x01, 0x02]);
}

#[test]
Expand All @@ -98,7 +120,7 @@ mod test {
assert_eq!(buffer.len(), 11);

assert_eq!(
buffer.as_vec(),
buffer.to_vec(),
vec![104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
);
}
Expand All @@ -110,12 +132,7 @@ mod test {

assert_eq!(buffer.len(), 1);

assert_eq!(buffer.as_vec(), vec![13]);
assert_eq!(buffer.to_vec(), vec![13]);
}
}

impl Debug for ByteBuffer {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("{:?}", self.inner))
}
}
1 change: 1 addition & 0 deletions hexo-io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod byte_buffer;
2 changes: 1 addition & 1 deletion src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ impl HexoCompiler {
) -> Result<Compilation, Error> {
let rst = self.compile_rst(source)?;

Ok(Compilation::from(rst.emits.as_vec()))
Ok(Compilation::from(rst.emits.to_vec()))
}
}
14 changes: 7 additions & 7 deletions src/compiler/native_fn/implementations.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use hexo_io::byte_buffer::ByteBuffer;

use crate::compiler::native_fn::error::Error;
use crate::compiler::native_fn::signature::{NativeFunction, NativeFunctionSignature};
use crate::compiler::util::ByteBuffer;

pub(crate) fn create_len_native_function() -> NativeFunction {
NativeFunction {
Expand Down Expand Up @@ -32,7 +32,7 @@ pub(crate) fn create_pad_left_native_function() -> NativeFunction {
let mut arg0 = get_argument_at(&arguments, 0, "pad_left")?.clone();
let arg1 = get_argument_at(&arguments, 1, "pad_left")?;

arg0.pad_left(arg1.as_usize());
arg0.pad_left(arg1.as_usize_unsafe());

Ok(arg0.clone())
},
Expand All @@ -48,7 +48,7 @@ pub(crate) fn create_pad_right_native_function() -> NativeFunction {
let mut arg0: ByteBuffer = get_argument_at(&arguments, 0, "pad_right")?.clone();
let arg1 = get_argument_at(&arguments, 1, "pad_right")?;

arg0.pad_right(arg1.as_usize());
arg0.pad_right(arg1.as_usize_unsafe());

Ok(arg0.clone())
},
Expand All @@ -62,7 +62,7 @@ pub(crate) fn create_cmd_native_function() -> NativeFunction {
},
executor: |arguments: HashMap<String, ByteBuffer>| {
let command = get_argument_at(&arguments, 0, "cmd")?
.as_string()
.to_string()
.map_err(|e| Error::Unknown(e.to_string()))?;

let output = std::process::Command::new(command)
Expand All @@ -88,7 +88,7 @@ pub(crate) fn create_read_file_native_function() -> NativeFunction {
executor: |arguments: HashMap<String, ByteBuffer>| {
let arg0 = get_argument_at(&arguments, 0, "read_file")?;

let file_path = arg0.as_string()
let file_path = arg0.to_string()
.map_err(|e| Error::Unknown(e.to_string()))?;

let mut file = File::open(file_path)
Expand Down Expand Up @@ -122,9 +122,9 @@ pub(crate) fn create_pad_native_function() -> NativeFunction {
let mut buffer = get_argument_at(&arguments, 0, "pad")?.clone();

let left_padding = get_named_argument(&arguments, "left")
.map(|b| b.as_usize());
.map(|b| b.as_usize_unsafe());
let right_padding = get_named_argument(&arguments, "right")
.map(|b| b.as_usize());
.map(|b| b.as_usize_unsafe());

if let Some(size) = left_padding {
buffer.pad_left(size);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/native_fn/signature.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::compiler::util::ByteBuffer;
use std::collections::HashMap;
use hexo_io::byte_buffer::ByteBuffer;
use crate::compiler::native_fn::error::Error;

#[derive(Clone, Debug)]
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/rst/compilation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::compiler::cst::{CstEmitStatement};
use crate::compiler::native_fn::{NativeFunction, NativeFunctionIndex};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::compiler::util::ByteBuffer;
use hexo_io::byte_buffer::ByteBuffer;

#[derive(Clone, Debug)]
pub(crate) struct ConstantBinding {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::path::Path;
use hexo_io::byte_buffer::ByteBuffer;

use crate::compiler::cst::{
CstActualParameter, CstAtom, CstAtomVec, CstEmitStatement, CstFile, CstFunctionStatement,
Expand All @@ -8,7 +9,7 @@ use crate::compiler::rst::compilation_context::{
CompilationContext, ConstantBinding, FunctionBinding,
};
use crate::compiler::rst::node::HexoFile;
use crate::compiler::util::{ByteBuffer, next_identifier};
use crate::compiler::util::{next_identifier};
use crate::compiler::HexoCompiler;
use crate::compiler::rst::error::Error;

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/rst/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::compiler::rst::compilation_context::CompilationContext;
use crate::compiler::util::ByteBuffer;
use std::path::PathBuf;
use hexo_io::byte_buffer::ByteBuffer;

#[derive(Debug)]
pub(crate) struct HexoFile {
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
mod byte_buffer;
pub(crate) mod encoding;
mod id_generator;

pub(crate) use byte_buffer::ByteBuffer;
pub(crate) use id_generator::next_identifier;
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fn run_sample() -> Result<(), Error> {
#[test]
fn run_test_cases() {
fn read_file(filename: &PathBuf) -> Vec<u8> {
let a = hexo_io::byte_buffer::ByteBuffer::new();

let mut f = File::open(filename).expect("no file found");
let metadata = std::fs::metadata(filename).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
Expand Down

0 comments on commit e502eef

Please sign in to comment.