Skip to content

Commit

Permalink
Release v5.1.0 (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Nov 25, 2022
1 parent c652df6 commit b070537
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v5.1.0
- Rename error fields.

## v5.0.0
- Optimize algorithm.
- Improve documentation.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ license = "Apache-2.0/GPL-3.0"
name = "array-bytes"
readme = "README.md"
repository = "https://github.com/hack-ink/array-bytes"
version = "5.0.0"
version = "5.1.0"

[dependencies]
serde = { version = "1.0", optional = true, default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions fuzz/Cargo.lock

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

4 changes: 2 additions & 2 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ edition = "2021"
metadata = { cargo-fuzz = true }
name = "array-bytes-fuzz"
publish = false
version = "5.0.0"
version = "5.1.0"

[dependencies]
array-bytes = { version = "5.0", path = ".." }
array-bytes = { version = "5.1", path = ".." }
libfuzzer-sys = { version = "0.4" }

[workspace]
Expand Down
24 changes: 15 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ pub enum Error {
/// The length must not be odd.
InvalidLength,
/// Found the invalid character at `index`.
InvalidChar {
InvalidCharacter {
/// The invalid character.
char: char,
character: char,
/// The invalid character's index.
index: usize,
},
Expand Down Expand Up @@ -246,7 +246,7 @@ where
pub fn hex_bytes2hex_str(bytes: &[u8]) -> Result<&str> {
for (i, byte) in bytes.iter().enumerate().skip(if bytes.starts_with(b"0x") { 2 } else { 0 }) {
if !is_hex_ascii(byte) {
Err(Error::InvalidChar { char: *byte as _, index: i })?;
Err(Error::InvalidCharacter { character: *byte as _, index: i })?;
}
}

Expand Down Expand Up @@ -360,17 +360,20 @@ where

for i in (0..hex.len()).step_by(2) {
let Some(str) = hex.get(i..i + 2) else {
Err(Error::InvalidChar { char: hex.as_bytes()[i] as _, index: i })?
Err(Error::InvalidCharacter { character: hex.as_bytes()[i] as _, index: i })?
};

match u8::from_str_radix(str, 16) {
Ok(byte_) => bytes.push(byte_),
Err(e) => {
if !is_hex_ascii(&hex.as_bytes()[i]) {
Err(Error::InvalidChar { char: hex.as_bytes()[i] as _, index: i })?;
Err(Error::InvalidCharacter { character: hex.as_bytes()[i] as _, index: i })?;
}
if !is_hex_ascii(&hex.as_bytes()[i + 1]) {
Err(Error::InvalidChar { char: hex.as_bytes()[i + 1] as _, index: i + 1 })?;
Err(Error::InvalidCharacter {
character: hex.as_bytes()[i + 1] as _,
index: i + 1,
})?;
}

// This will never happen, but for more safety
Expand Down Expand Up @@ -432,17 +435,20 @@ where

for (byte, i) in slice.iter_mut().zip((0..hex.len()).step_by(2)) {
let Some(str) = hex.get(i..i + 2) else {
Err(Error::InvalidChar { char: hex.as_bytes()[i] as _, index: i })?
Err(Error::InvalidCharacter { character: hex.as_bytes()[i] as _, index: i })?
};

match u8::from_str_radix(str, 16) {
Ok(byte_) => *byte = byte_,
Err(e) => {
if !is_hex_ascii(&hex.as_bytes()[i]) {
Err(Error::InvalidChar { char: hex.as_bytes()[i] as _, index: i })?;
Err(Error::InvalidCharacter { character: hex.as_bytes()[i] as _, index: i })?;
}
if !is_hex_ascii(&hex.as_bytes()[i + 1]) {
Err(Error::InvalidChar { char: hex.as_bytes()[i + 1] as _, index: i + 1 })?;
Err(Error::InvalidCharacter {
character: hex.as_bytes()[i + 1] as _,
index: i + 1,
})?;
}

// This will never happen, but for more safety
Expand Down
18 changes: 12 additions & 6 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ fn hex_bytes2hex_str_should_work() {

assert_eq!(
hex_bytes2hex_str(b"4c6f766 5204a616e6520466f7265766572"),
Err(Error::InvalidChar { char: ' ', index: 7 }),
Err(Error::InvalidCharacter { character: ' ', index: 7 }),
);
assert_eq!(
hex_bytes2hex_str(b"4c6f766520 4a616e6520466f7265766572"),
Err(Error::InvalidChar { char: ' ', index: 10 }),
Err(Error::InvalidCharacter { character: ' ', index: 10 }),
);
}

Expand Down Expand Up @@ -134,8 +134,8 @@ fn hex2bytes_should_work() {
assert_eq!(hex2bytes("我爱你"), Err(Error::InvalidLength));
assert_eq!(hex2bytes("0x我爱你"), Err(Error::InvalidLength));

assert_eq!(hex2bytes("我爱你 "), Err(Error::InvalidChar { char: 'æ', index: 0 }));
assert_eq!(hex2bytes(" 我爱你"), Err(Error::InvalidChar { char: ' ', index: 0 }));
assert_eq!(hex2bytes("我爱你 "), Err(Error::InvalidCharacter { character: 'æ', index: 0 }));
assert_eq!(hex2bytes(" 我爱你"), Err(Error::InvalidCharacter { character: ' ', index: 0 }));
}

#[test]
Expand Down Expand Up @@ -172,8 +172,14 @@ fn hex2slice_should_work() {
assert_eq!(hex2slice("00", &mut []), Err(Error::MismatchedLength { expect: 1 }));
assert_eq!(hex2slice("0x0001", &mut []), Err(Error::MismatchedLength { expect: 2 }));

assert_eq!(hex2slice("fg", &mut [0]), Err(Error::InvalidChar { char: 'g', index: 1 }));
assert_eq!(hex2slice("0xyz", &mut [0]), Err(Error::InvalidChar { char: 'y', index: 0 }));
assert_eq!(
hex2slice("fg", &mut [0]),
Err(Error::InvalidCharacter { character: 'g', index: 1 })
);
assert_eq!(
hex2slice("0xyz", &mut [0]),
Err(Error::InvalidCharacter { character: 'y', index: 0 })
);
}

#[test]
Expand Down

0 comments on commit b070537

Please sign in to comment.