Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav committed Dec 27, 2020
0 parents commit df24984
Show file tree
Hide file tree
Showing 8 changed files with 832 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
charset=utf-8
end_of_line=lf
indent_size=tab
indent_style=tab
insert_final_newline=true
max_line_length=100
tab_width=4
trim_trailing_whitespace=true

[*.py]
charset=utf-8
indent_size=4
indent_style=space

[*.{sh, yml, yaml}]
indent_size=2
indent_style=space
tab_width=8
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# IDE
.idea
.vscode
.ignore

# macOS
.DS_Store

# PM2
.pm2.json

# Release
release/

# Rust
**/*.rs.bk
**/target/
.cargo-ok
Cargo.toml.bak
expand.rs

# Added by cargo

/target
7 changes: 7 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
hard_tabs = true
max_width = 100
newline_style = "Unix"
reorder_imports = true
reorder_modules = true
tab_spaces = 4
use_field_init_shorthand = true
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.

16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors = ["Xavier Lau <[email protected]>"]
description = "array-bytes"
edition = "2018"
homepage = "https://array-bytes.l2ust.world"
license = "GPL-3.0"
name = "array-bytes"
readme = "README.md"
repository = "https://github.com/l2ust/array-bytes"
version = "0.3.0"

[badges]
maintenance = { status = "actively-developed" }

[dependencies]
# thiserror = { version = "1.0.23" }
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

Empty file added README.md
Empty file.
85 changes: 85 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#![no_std]

extern crate alloc;

// --- alloc ---
use alloc::{string::String, vec::Vec};
// --- core ---
use core::{char, num::ParseIntError};
// // --- crates.io ---
// use thiserror::Error as ThisError;

pub type ArrayBytesResult<T> = Result<T, Error>;

// #[derive(Debug, ThisError)]
// pub enum Error {
// #[error("Fail to convert {} to bytes", hex_str)]
// InvalidHexLength { hex_str: String },
// #[error("Fail to parse int")]
// InvalidChar(#[from] ParseIntError),
// }
#[derive(Debug)]
pub enum Error {
InvalidHexLength { hex_str: String },
InvalidChar(ParseIntError),
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Self::InvalidChar(e)
}
}

#[macro_export]
macro_rules! bytes_array_unchecked {
($bytes:expr, $len:expr) => {{
unsafe { *($bytes.as_ptr() as *const [u8; $len]) }
}};
}

#[macro_export]
macro_rules! hex_str_array_unchecked {
($hex_str:expr, $len:expr) => {{
$crate::bytes_array_unchecked!($crate::bytes_unchecked($hex_str), $len)
}};
}

pub fn bytes(hex_str: impl AsRef<str>) -> ArrayBytesResult<Vec<u8>> {
let hex_str = hex_str.as_ref();

if hex_str.len() % 2 != 0 {
return Err(Error::InvalidHexLength {
hex_str: hex_str.into(),
});
}

Ok(
(if hex_str.starts_with("0x") { 2 } else { 0 }..hex_str.len())
.step_by(2)
.map(|i| Ok(u8::from_str_radix(&hex_str[i..i + 2], 16)?))
.collect::<ArrayBytesResult<_>>()?,
)
}
pub fn bytes_unchecked(hex_str: impl AsRef<str>) -> Vec<u8> {
let hex_str = hex_str.as_ref();

(if hex_str.starts_with("0x") { 2 } else { 0 }..hex_str.len())
.step_by(2)
.map(|i| u8::from_str_radix(&hex_str[i..i + 2], 16).unwrap())
.collect()
}

pub fn hex_str(prefix: impl AsRef<str>, bytes: impl AsRef<[u8]>) -> String {
let prefix = prefix.as_ref();
let bytes = bytes.as_ref();
let mut hex_str = String::with_capacity(prefix.len() + bytes.len() * 2);

for byte in prefix.chars() {
hex_str.push(byte);
}
for byte in bytes.iter() {
hex_str.push(char::from_digit((byte >> 4) as _, 16).unwrap());
hex_str.push(char::from_digit((byte & 0xf) as _, 16).unwrap());
}

hex_str
}

0 comments on commit df24984

Please sign in to comment.