Skip to content

Commit 463e241

Browse files
committed
Some minor hex changes
1 parent 2266df5 commit 463e241

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/libextra/hex.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'self> ToHex for &'self str {
5757
* # Example
5858
*
5959
* ~~~ {.rust}
60-
* extern mod extra;
60+
* extern mod extra;
6161
* use extra::ToHex;
6262
*
6363
* fn main () {
@@ -74,15 +74,16 @@ impl<'self> ToHex for &'self str {
7474

7575
/// A trait for converting hexadecimal encoded values
7676
pub trait FromHex {
77-
/// Converts the value of `self`, interpreted as base64 encoded data, into
78-
/// an owned vector of bytes, returning the vector.
77+
/// Converts the value of `self`, interpreted as hexadecimal encoded data,
78+
/// into an owned vector of bytes, returning the vector.
7979
fn from_hex(&self) -> Result<~[u8], ~str>;
8080
}
8181

8282
impl<'self> FromHex for &'self [u8] {
8383
/**
8484
* Convert hexadecimal `u8` vector into u8 byte values.
8585
* Every 2 encoded characters is converted into 1 octet.
86+
* Whitespace is ignored.
8687
*
8788
* # Example
8889
*
@@ -104,18 +105,19 @@ impl<'self> FromHex for &'self [u8] {
104105
let mut modulus = 0;
105106
let mut buf = 0u8;
106107

107-
for &byte in self.iter() {
108+
for (idx, &byte) in self.iter().enumerate() {
108109
buf <<= 4;
109110

110111
match byte as char {
111112
'A'..'F' => buf |= byte - ('A' as u8) + 10,
112113
'a'..'f' => buf |= byte - ('a' as u8) + 10,
113114
'0'..'9' => buf |= byte - ('0' as u8),
114-
' '|'\r'|'\n' => {
115+
' '|'\r'|'\n'|'\t' => {
115116
buf >>= 4;
116117
loop
117118
}
118-
_ => return Err(~"Invalid hex char")
119+
_ => return Err(fmt!("Invalid byte '%c' found at position %u",
120+
byte as char, idx))
119121
}
120122

121123
modulus += 1;

0 commit comments

Comments
 (0)