Skip to content

libstd: convert chained ifs to a match in base64. #6332

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

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
44 changes: 20 additions & 24 deletions src/libstd/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,27 @@ impl FromBase64 for ~[u8] {
let ch = self[i] as char;
n <<= 6u;

if ch >= 'A' && ch <= 'Z' {
n |= (ch as uint) - 0x41u;
} else if ch >= 'a' && ch <= 'z' {
n |= (ch as uint) - 0x47u;
} else if ch >= '0' && ch <= '9' {
n |= (ch as uint) + 0x04u;
} else if ch == '+' {
n |= 0x3Eu;
} else if ch == '/' {
n |= 0x3Fu;
} else if ch == '=' {
match len - i {
1u => {
r.push(((n >> 16u) & 0xFFu) as u8);
r.push(((n >> 8u ) & 0xFFu) as u8);
return copy r;
}
2u => {
r.push(((n >> 10u) & 0xFFu) as u8);
return copy r;
}
_ => fail!(~"invalid base64 padding")
match ch {
'A'..'Z' => n |= (ch as uint) - 0x41,
'a'..'z' => n |= (ch as uint) - 0x47,
'0'..'9' => n |= (ch as uint) + 0x04,
'+' => n |= 0x3E,
'/' => n |= 0x3F,
'=' => {
match len - i {
1u => {
r.push(((n >> 16u) & 0xFFu) as u8);
r.push(((n >> 8u ) & 0xFFu) as u8);
return copy r;
}
2u => {
r.push(((n >> 10u) & 0xFFu) as u8);
return copy r;
}
_ => fail!(~"invalid base64 padding")
}
}
} else {
fail!(~"invalid base64 character");
_ => fail!(~"invalid base64 character")
}

i += 1u;
Expand Down