]> git.lizzy.rs Git - rust.git/commitdiff
Some minor hex changes
authorSteven Fackler <sfackler@gmail.com>
Sun, 4 Aug 2013 20:09:04 +0000 (16:09 -0400)
committerSteven Fackler <sfackler@gmail.com>
Tue, 6 Aug 2013 16:58:36 +0000 (09:58 -0700)
src/libextra/hex.rs

index 7a1417e72d8cc5d614c0c1ac6a07656048ae263e..5609c566d927315cf598da9d64c21aa2b9c19dd3 100644 (file)
@@ -57,7 +57,7 @@ impl<'self> ToHex for &'self str {
      * # Example
      *
      * ~~~ {.rust}
-         * extern mod extra;
+     * extern mod extra;
      * use extra::ToHex;
      *
      * fn main () {
@@ -74,8 +74,8 @@ fn to_hex(&self) -> ~str {
 
 /// A trait for converting hexadecimal encoded values
 pub trait FromHex {
-    /// Converts the value of `self`, interpreted as base64 encoded data, into
-    /// an owned vector of bytes, returning the vector.
+    /// Converts the value of `self`, interpreted as hexadecimal encoded data,
+    /// into an owned vector of bytes, returning the vector.
     fn from_hex(&self) -> Result<~[u8], ~str>;
 }
 
@@ -83,6 +83,7 @@ impl<'self> FromHex for &'self [u8] {
     /**
      * Convert hexadecimal `u8` vector into u8 byte values.
      * Every 2 encoded characters is converted into 1 octet.
+     * Whitespace is ignored.
      *
      * # Example
      *
@@ -104,18 +105,19 @@ fn from_hex(&self) -> Result<~[u8], ~str> {
         let mut modulus = 0;
         let mut buf = 0u8;
 
-        for &byte in self.iter() {
+        for (idx, &byte) in self.iter().enumerate() {
             buf <<= 4;
 
             match byte as char {
                 'A'..'F' => buf |= byte - ('A' as u8) + 10,
                 'a'..'f' => buf |= byte - ('a' as u8) + 10,
                 '0'..'9' => buf |= byte - ('0' as u8),
-                ' '|'\r'|'\n' => {
+                ' '|'\r'|'\n'|'\t' => {
                     buf >>= 4;
                     loop
                 }
-                _ => return Err(~"Invalid hex char")
+                _ => return Err(fmt!("Invalid byte '%c' found at position %u",
+                                     byte as char, idx))
             }
 
             modulus += 1;