Skip to content

Commit

Permalink
Generalized implementation of FromBase64
Browse files Browse the repository at this point in the history
Previously, FromBase64 was only implemented on ~[u8] and ~str when
any pointer would do. The implementations of FromBase64 are now
consistent with the implementations of ToBase64.
  • Loading branch information
sfackler committed May 29, 2013
1 parent e3d0c1e commit b1e7d49
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/libextra/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub trait FromBase64 {
fn from_base64(&self) -> ~[u8];
}

impl FromBase64 for ~[u8] {
impl<'self> FromBase64 for &'self [u8] {
/**
* Convert base64 `u8` vector into u8 byte values.
* Every 4 encoded characters is converted into 3 octets, modulo padding.
Expand Down Expand Up @@ -188,7 +188,7 @@ impl FromBase64 for ~[u8] {
}
}

impl FromBase64 for ~str {
impl<'self> FromBase64 for &'self str {
/**
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
* to the byte values it encodes.
Expand Down Expand Up @@ -227,23 +227,23 @@ mod tests {

#[test]
fn test_to_base64() {
assert_eq!((~"").to_base64(), ~"");
assert!((~"f").to_base64() == ~"Zg==");
assert_eq!((~"fo").to_base64(), ~"Zm8=");
assert_eq!((~"foo").to_base64(), ~"Zm9v");
assert!((~"foob").to_base64() == ~"Zm9vYg==");
assert_eq!((~"fooba").to_base64(), ~"Zm9vYmE=");
assert_eq!((~"foobar").to_base64(), ~"Zm9vYmFy");
assert_eq!("".to_base64(), ~"");
assert_eq!("f".to_base64(), ~"Zg==");
assert_eq!("fo".to_base64(), ~"Zm8=");
assert_eq!("foo".to_base64(), ~"Zm9v");
assert_eq!("foob".to_base64(), ~"Zm9vYg==");
assert_eq!("fooba".to_base64(), ~"Zm9vYmE=");
assert_eq!("foobar".to_base64(), ~"Zm9vYmFy");
}

#[test]
fn test_from_base64() {
assert_eq!((~"").from_base64(), str::to_bytes(""));
assert!((~"Zg==").from_base64() == str::to_bytes("f"));
assert_eq!((~"Zm8=").from_base64(), str::to_bytes("fo"));
assert_eq!((~"Zm9v").from_base64(), str::to_bytes("foo"));
assert!((~"Zm9vYg==").from_base64() == str::to_bytes("foob"));
assert_eq!((~"Zm9vYmE=").from_base64(), str::to_bytes("fooba"))
assert_eq!((~"Zm9vYmFy").from_base64(), str::to_bytes("foobar"));
assert_eq!("".from_base64(), str::to_bytes(""));
assert_eq!("Zg==".from_base64(), str::to_bytes("f"));
assert_eq!("Zm8=".from_base64(), str::to_bytes("fo"));
assert_eq!("Zm9v".from_base64(), str::to_bytes("foo"));
assert_eq!("Zm9vYg==".from_base64(), str::to_bytes("foob"));
assert_eq!("Zm9vYmE=".from_base64(), str::to_bytes("fooba"))
assert_eq!("Zm9vYmFy".from_base64(), str::to_bytes("foobar"));
}
}

5 comments on commit b1e7d49

@bors
Copy link
Contributor

@bors bors commented on b1e7d49 May 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from bstrie
at sfackler@b1e7d49

@bors
Copy link
Contributor

@bors bors commented on b1e7d49 May 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sfackler/rust/FromBase64-type-fix = b1e7d49 into auto

@bors
Copy link
Contributor

@bors bors commented on b1e7d49 May 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sfackler/rust/FromBase64-type-fix = b1e7d49 merged ok, testing candidate = a037fa4

@bors
Copy link
Contributor

@bors bors commented on b1e7d49 May 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on b1e7d49 May 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = a037fa4

Please sign in to comment.