@@ -161,15 +161,15 @@ pub trait FromBase64 {
161
161
/// Errors that can occur when decoding a base64 encoded string
162
162
pub enum FromBase64Error {
163
163
/// The input contained a character not part of the base64 format
164
- InvalidBase64Character ( char , uint ) ,
164
+ InvalidBase64Byte ( u8 , uint ) ,
165
165
/// The input had an invalid length
166
166
InvalidBase64Length ,
167
167
}
168
168
169
169
impl fmt:: Show for FromBase64Error {
170
170
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
171
171
match * self {
172
- InvalidBase64Character ( ch, idx) =>
172
+ InvalidBase64Byte ( ch, idx) =>
173
173
write ! ( f, "Invalid character '{}' at position {}" , ch, idx) ,
174
174
InvalidBase64Length => write ! ( f, "Invalid length" ) ,
175
175
}
@@ -205,24 +205,31 @@ impl<'a> FromBase64 for &'a str {
205
205
* }
206
206
* ```
207
207
*/
208
+ #[ inline]
209
+ fn from_base64 ( & self ) -> Result < Vec < u8 > , FromBase64Error > {
210
+ self . as_bytes ( ) . from_base64 ( )
211
+ }
212
+ }
213
+
214
+ impl < ' a > FromBase64 for & ' a [ u8 ] {
208
215
fn from_base64 ( & self ) -> Result < Vec < u8 > , FromBase64Error > {
209
216
let mut r = Vec :: new ( ) ;
210
217
let mut buf: u32 = 0 ;
211
218
let mut modulus = 0 i;
212
219
213
- let mut it = self . bytes ( ) . enumerate ( ) ;
214
- for ( idx, byte) in it {
220
+ let mut it = self . iter ( ) . enumerate ( ) ;
221
+ for ( idx, & byte) in it {
215
222
let val = byte as u32 ;
216
223
217
- match byte as char {
218
- 'A' ..'Z' => buf |= val - 0x41 ,
219
- 'a' ..'z' => buf |= val - 0x47 ,
220
- '0' ..'9' => buf |= val + 0x04 ,
221
- '+' | '-' => buf |= 0x3E ,
222
- '/' | '_' => buf |= 0x3F ,
223
- '\r' | '\n' => continue ,
224
- '=' => break ,
225
- _ => return Err ( InvalidBase64Character ( self . char_at ( idx) , idx) ) ,
224
+ match byte {
225
+ b 'A' ..b 'Z' => buf |= val - 0x41 ,
226
+ b 'a' ..b 'z' => buf |= val - 0x47 ,
227
+ b '0' ..b '9' => buf |= val + 0x04 ,
228
+ b '+' | b '-' => buf |= 0x3E ,
229
+ b '/' | b '_' => buf |= 0x3F ,
230
+ b '\r' | b '\n' => continue ,
231
+ b '=' => break ,
232
+ _ => return Err ( InvalidBase64Byte ( self [ idx] , idx) ) ,
226
233
}
227
234
228
235
buf <<= 6 ;
@@ -235,10 +242,10 @@ impl<'a> FromBase64 for &'a str {
235
242
}
236
243
}
237
244
238
- for ( idx, byte) in it {
239
- match byte as char {
240
- '=' | '\r' | '\n' => continue ,
241
- _ => return Err ( InvalidBase64Character ( self . char_at ( idx) , idx) ) ,
245
+ for ( idx, & byte) in it {
246
+ match byte {
247
+ b '=' | b '\r' | b '\n' => continue ,
248
+ _ => return Err ( InvalidBase64Byte ( self [ idx] , idx) ) ,
242
249
}
243
250
}
244
251
@@ -308,6 +315,11 @@ mod tests {
308
315
assert_eq ! ( "Zm9vYmFy" . from_base64( ) . unwrap( ) . as_slice( ) , "foobar" . as_bytes( ) ) ;
309
316
}
310
317
318
+ #[ test]
319
+ fn test_from_base64_bytes ( ) {
320
+ assert_eq ! ( b"Zm9vYmFy" . from_base64( ) . unwrap( ) . as_slice( ) , "foobar" . as_bytes( ) ) ;
321
+ }
322
+
311
323
#[ test]
312
324
fn test_from_base64_newlines ( ) {
313
325
assert_eq ! ( "Zm9v\r \n YmFy" . from_base64( ) . unwrap( ) . as_slice( ) ,
0 commit comments