Skip to content

Commit

Permalink
lib: Support \0x\ prefix. Resolves #3
Browse files Browse the repository at this point in the history
  • Loading branch information
inspier committed Jul 10, 2022
1 parent ee9d078 commit 5bd84ff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "hexlit"
version = "0.5.3"
authors = ["inspier <[email protected]>"]
edition = "2018"
edition = "2021"
documentation = "https://docs.rs/hexlit"
repository = "https://github.com/inspier/hexlit"
description = "A zero-allocation no_std-compatible zero-cost way to convert hex-strings to byte-arrays at compile time."
Expand Down
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
edition = "2018"
edition = "2021"
max_width = 90
use_field_init_shorthand = true
use_try_shorthand = true
Expand Down
42 changes: 37 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,28 @@ pub mod internals {
pub const fn count_skipped(data: &[u8]) -> usize {
let mut char_count: usize = 0;
let mut char_index: usize = 0;

while char_index < data.len() {
if is_valid_delimiter(data[char_index]) {
if char_index + 1 < data.len() && !is_valid_delimiter(data[char_index]) {
let mut next_index = char_index + 1;
while next_index < data.len() && is_valid_delimiter(data[next_index]) {
char_count += 1;
next_index += 1;
}

if data[char_index] == b'0'
&& (data[next_index] == b'x' || data[next_index] == b'X')
{
char_count += 2;
}

char_index = next_index + 1;
} else {
char_index += 1;
char_count += 1;
}
char_index += 1;
}

char_count
}

Expand Down Expand Up @@ -113,10 +129,15 @@ pub mod internals {
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]);

if !(input[char_index] == b'0'
&& (input[next_index] == b'x' || input[next_index] == b'X'))
{
data[data_index] = to_ordinal(input[char_index]) * 16
+ to_ordinal(input[next_index]);
data_index += 1;
}
char_index = next_index + 1;
data_index += 1;
} else {
char_index += 1;
}
Expand Down Expand Up @@ -227,4 +248,15 @@ mod tests {
assert_eq!(b, c);
assert_eq!(c, d);
}

#[test]
fn test_hex_prefix() {
assert_eq!(
hex!("0xe9e7cea3dedca5984780bafc599bd69add087d56"),
[
0xE9, 0xE7, 0xCE, 0xA3, 0xDE, 0xDC, 0xA5, 0x98, 0x47, 0x80, 0xBA, 0xFC,
0x59, 0x9B, 0xD6, 0x9A, 0xDD, 0x08, 0x7D, 0x56
]
);
}
}

0 comments on commit 5bd84ff

Please sign in to comment.