@@ -57,7 +57,7 @@ impl<'self> ToHex for &'self str {
57
57
* # Example
58
58
*
59
59
* ~~~ {.rust}
60
- * extern mod extra;
60
+ * extern mod extra;
61
61
* use extra::ToHex;
62
62
*
63
63
* fn main () {
@@ -74,15 +74,16 @@ impl<'self> ToHex for &'self str {
74
74
75
75
/// A trait for converting hexadecimal encoded values
76
76
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.
79
79
fn from_hex ( & self ) -> Result < ~[ u8 ] , ~str > ;
80
80
}
81
81
82
82
impl < ' self > FromHex for & ' self [ u8 ] {
83
83
/**
84
84
* Convert hexadecimal `u8` vector into u8 byte values.
85
85
* Every 2 encoded characters is converted into 1 octet.
86
+ * Whitespace is ignored.
86
87
*
87
88
* # Example
88
89
*
@@ -104,18 +105,19 @@ impl<'self> FromHex for &'self [u8] {
104
105
let mut modulus = 0 ;
105
106
let mut buf = 0u8 ;
106
107
107
- for & byte in self . iter ( ) {
108
+ for ( idx , & byte) in self . iter ( ) . enumerate ( ) {
108
109
buf <<= 4 ;
109
110
110
111
match byte as char {
111
112
'A' ..'F' => buf |= byte - ( 'A' as u8 ) + 10 ,
112
113
'a' ..'f' => buf |= byte - ( 'a' as u8 ) + 10 ,
113
114
'0' ..'9' => buf |= byte - ( '0' as u8 ) ,
114
- ' ' |'\r' |'\n' => {
115
+ ' ' |'\r' |'\n' | '\t' => {
115
116
buf >>= 4 ;
116
117
loop
117
118
}
118
- _ => return Err ( ~"Invalid hex char")
119
+ _ => return Err ( fmt ! ( "Invalid byte '%c' found at position %u" ,
120
+ byte as char , idx) )
119
121
}
120
122
121
123
modulus += 1 ;
0 commit comments