Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalized implementation of FromBase64 #6796

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}