Skip to content

Commit

Permalink
hexlit: Bumped version to 0.5.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
inspier committed Mar 25, 2021
1 parent b626f01 commit 413138f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hexlit"
version = "0.4.0"
version = "0.5.0"
authors = ["inspier <[email protected]>"]
edition = "2018"
documentation = "https://docs.rs/hexlit"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[![Current Crates.io Version](https://img.shields.io/crates/v/hexlit.svg)](https://crates.io/crates/hexlit)
[![docs-rs](https://docs.rs/hexlit/badge.svg)](https://docs.rs/hexlit)
![MSRV 1.46+](https://img.shields.io/badge/rustc-1.46+-blue.svg)
![MSRV 1.51+](https://img.shields.io/badge/rustc-1.51+-blue.svg)

# hexlit
A zero-allocation no_std-compatible zero-cost way to convert hex-strings to byte-arrays at compile time.

To add to your Cargo.toml:
```toml
hexlit = "0.4.0"
hexlit = "0.5.0"
```

## Example
Expand Down
4 changes: 2 additions & 2 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
edition = "2018"
max_width = 80
max_width = 90
use_field_init_shorthand = true
use_try_shorthand = true
use_small_heuristics = "Max"
format_code_in_doc_comments = true
merge_imports = true
imports_granularity= "Crate"
wrap_comments = true
format_macro_matchers = true
format_macro_bodies = true
53 changes: 28 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,7 @@ macro_rules! hex {
const SKIP_LENGTH: usize = $crate::internals::count_skipped(&DATA);
$crate::require_even_number_digits!($arg.len() - SKIP_LENGTH);
const ARRAY_LENGTH: usize = ($arg.len() - SKIP_LENGTH) / 2;
const RESULT: [u8; ARRAY_LENGTH] = {
// Converts a hex-string to its byte array representation.
let mut data = [0u8; ARRAY_LENGTH];
let mut data_index: usize = 0;
let mut char_index: usize = 0;
let string_length = $arg.len();
while data_index < string_length && char_index + 1 < string_length {
if !$crate::internals::is_valid_delimiter(DATA[char_index]) {
let mut next_index = char_index + 1;
while next_index < string_length
&& $crate::internals::is_valid_delimiter(DATA[next_index]) {
next_index += 1;
}
data[data_index] = $crate::internals::to_ordinal(DATA[char_index]) * 16
+ $crate::internals::to_ordinal(DATA[next_index]);
char_index = next_index + 1;
data_index += 1;
} else {
char_index += 1;
}
}
data
};
RESULT
$crate::internals::convert::<ARRAY_LENGTH, {$arg.len()}>(&DATA)
}};
($($tt:tt)*) => {
hex!(@string stringify!($($tt)*))
Expand All @@ -66,7 +43,8 @@ pub mod internals {

const DELIMITERS: [u8; 5] = [b' ', b'"', b'_', b'|', b'-'];

pub type Even<T> = <<T as HexStringLength>::Marker as LengthIsEvenNumberOfHexDigits>::Check;
pub type Even<T> =
<<T as HexStringLength>::Marker as LengthIsEvenNumberOfHexDigits>::Check;

pub enum IsEvenNumberofDigits {}
pub enum IsOddNumberofDigits {}
Expand Down Expand Up @@ -134,6 +112,31 @@ pub mod internals {
}
}
}

// Converts a hex-string to its byte array representation.
pub const fn convert<const RESULT_SIZE: usize, const STRING_SIZE: usize>(
input: &[u8],
) -> [u8; RESULT_SIZE] {
let mut data = [0_u8; RESULT_SIZE];
let mut data_index: usize = 0;
let mut char_index: usize = 0;

while data_index < STRING_SIZE && char_index + 1 < STRING_SIZE {
if !is_valid_delimiter(input[char_index]) {
let mut next_index = char_index + 1;
while next_index < STRING_SIZE && is_valid_delimiter(input[next_index]) {
next_index += 1;
}
data[data_index] =
to_ordinal(input[char_index]) * 16 + to_ordinal(input[next_index]);
char_index = next_index + 1;
data_index += 1;
} else {
char_index += 1;
}
}
data
}
}

#[cfg(test)]
Expand Down

0 comments on commit 413138f

Please sign in to comment.