From e5ca2717641e95ad3e2e29e542840123dc5c46fa Mon Sep 17 00:00:00 2001 From: nujz Date: Mon, 21 Sep 2020 01:04:18 +0800 Subject: [PATCH] actix-router: fix from_hex error (#196) --- router/CHANGES.md | 4 ++++ router/src/url.rs | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/router/CHANGES.md b/router/CHANGES.md index 17afeb5b..5b249f9f 100644 --- a/router/CHANGES.md +++ b/router/CHANGES.md @@ -1,5 +1,9 @@ # Changes +## Unreleased - 2020-xx-xx + +* Fix `from_hex()` method + ## [0.2.4] - 2019-12-31 * Add `ResourceDef::resource_path_named()` path generation method diff --git a/router/src/url.rs b/router/src/url.rs index 189a9e80..6a4195b8 100644 --- a/router/src/url.rs +++ b/router/src/url.rs @@ -186,7 +186,7 @@ fn from_hex(v: u8) -> Option { Some(v - 0x30) // ord('0') == 0x30 } else if v >= b'A' && v <= b'F' { Some(v - 0x41 + 10) // ord('A') == 0x41 - } else if v > b'a' && v <= b'f' { + } else if v >= b'a' && v <= b'f' { Some(v - 0x61 + 10) // ord('a') == 0x61 } else { None @@ -225,4 +225,25 @@ mod tests { assert!(re.match_path(&mut path)); assert_eq!(path.get("id").unwrap(), "qwe%rty"); } + + #[test] + fn test_from_hex() { + let hex = b"0123456789abcdefABCDEF"; + + for i in 0..256 { + let c = i as u8; + if hex.contains(&c) { + assert!(from_hex(c).is_some()) + } else { + assert!(from_hex(c).is_none()) + } + } + + let expected = [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15, + ]; + for i in 0..hex.len() { + assert_eq!(from_hex(hex[i]).unwrap(), expected[i]); + } + } }