-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit df24984
Showing
8 changed files
with
832 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |