aboutsummaryrefslogtreecommitdiff
path: root/hex.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <[email protected]>2013-12-18 08:51:10 -0800
committerErick Tryzelaar <[email protected]>2013-12-18 08:51:10 -0800
commita9ce2a36d54ac08608d689a5f0ec441f74e56d61 (patch)
tree813ceb833817cfac21a1cc920446f8a7927acf76 /hex.rs
parentUpdate to rust 0.9x-pre (diff)
downloadrust-openssl-a9ce2a36d54ac08608d689a5f0ec441f74e56d61.tar.xz
rust-openssl-a9ce2a36d54ac08608d689a5f0ec441f74e56d61.zip
Switch over to rustpkg
Diffstat (limited to 'hex.rs')
-rw-r--r--hex.rs87
1 files changed, 0 insertions, 87 deletions
diff --git a/hex.rs b/hex.rs
deleted file mode 100644
index f55dc1a9..00000000
--- a/hex.rs
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright 2013 Jack Lloyd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-use std::vec;
-
-pub trait ToHex {
- fn to_hex(&self) -> ~str;
-}
-
-impl<'a> ToHex for &'a [u8] {
- fn to_hex(&self) -> ~str {
-
- let chars = "0123456789ABCDEF".chars().collect::<~[char]>();
-
- let mut s = ~"";
-
- for i in range(0u, self.len()) {
-
- let x = self[i];
-
- let xhi = (x >> 4) & 0x0F;
- let xlo = (x ) & 0x0F;
-
- s.push_char(chars[xhi]);
- s.push_char(chars[xlo]);
- }
-
- s
- }
-}
-
-pub trait FromHex {
- fn from_hex(&self) -> ~[u8];
-}
-
-impl<'a> FromHex for &'a str {
- fn from_hex(&self) -> ~[u8] {
- let mut vec = vec::with_capacity(self.len() / 2);
-
- for (i,c) in self.chars().enumerate() {
- let nibble =
- if c >= '0' && c <= '9' { (c as u8) - 0x30 }
- else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) }
- else if c >= 'A' && c <= 'F' { (c as u8) - (0x41 - 10) }
- else { fail!(~"bad hex character"); };
-
- if i % 2 == 0 {
- vec.push(nibble << 4);
- }
- else {
- vec[i/2] |= nibble;
- }
- }
-
- vec
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- pub fn test() {
-
- assert!([05u8, 0xffu8, 0x00u8, 0x59u8].to_hex() == ~"05FF0059");
-
- assert!("00FFA9D1F5".from_hex() == ~[0, 0xff, 0xa9, 0xd1, 0xf5]);
-
- assert!("00FFA9D1F5".from_hex().to_hex() == ~"00FFA9D1F5");
- }
-
-
-}