@@ -150,52 +150,38 @@ impl<'self> ToBase64 for &'self [u8] {
150
150
}
151
151
}
152
152
153
- impl < ' self > ToBase64 for & ' self str {
154
- /**
155
- * Convert any string (literal, `@`, `&`, or `~`) to base64 encoding.
156
- *
157
- *
158
- * # Example
159
- *
160
- * ~~~ {.rust}
161
- * extern mod extra;
162
- * use extra::base64::{ToBase64, standard};
163
- *
164
- * fn main () {
165
- * let str = "Hello, World".to_base64(standard);
166
- * printfln!("%s", str);
167
- * }
168
- * ~~~
169
- *
170
- */
171
- fn to_base64 ( & self , config : Config ) -> ~str {
172
- self . as_bytes ( ) . to_base64 ( config)
173
- }
174
- }
175
-
176
153
/// A trait for converting from base64 encoded values.
177
154
pub trait FromBase64 {
178
155
/// Converts the value of `self`, interpreted as base64 encoded data, into
179
156
/// an owned vector of bytes, returning the vector.
180
157
fn from_base64 ( & self ) -> Result < ~[ u8 ] , ~str > ;
181
158
}
182
159
183
- impl < ' self > FromBase64 for & ' self [ u8 ] {
160
+ impl < ' self > FromBase64 for & ' self str {
184
161
/**
185
- * Convert base64 `u8` vector into u8 byte values.
186
- * Every 4 encoded characters is converted into 3 octets, modulo padding.
162
+ * Convert any base64 encoded string (literal, `@`, `&`, or `~`)
163
+ * to the byte values it encodes.
164
+ *
165
+ * You can use the `from_bytes` function in `std::str`
166
+ * to turn a `[u8]` into a string with characters corresponding to those
167
+ * values.
187
168
*
188
169
* # Example
189
170
*
171
+ * This converts a string literal to base64 and back.
172
+ *
190
173
* ~~~ {.rust}
191
174
* extern mod extra;
192
175
* use extra::base64::{ToBase64, FromBase64, standard};
176
+ * use std::str;
193
177
*
194
178
* fn main () {
195
- * let str = [52,32] .to_base64(standard);
196
- * printfln!("%s", str );
197
- * let bytes = str .from_base64();
179
+ * let hello_str = "Hello, World" .to_base64(standard);
180
+ * printfln!("%s", hello_str );
181
+ * let bytes = hello_str .from_base64();
198
182
* printfln!("%?", bytes);
183
+ * let result_str = str::from_bytes(bytes);
184
+ * printfln!("%s", result_str);
199
185
* }
200
186
* ~~~
201
187
*/
@@ -204,20 +190,20 @@ impl<'self> FromBase64 for &'self [u8] {
204
190
let mut buf: u32 = 0 ;
205
191
let mut modulus = 0 ;
206
192
207
- let mut it = self . iter ( ) ;
208
- for & byte in it {
209
- let ch = byte as char ;
193
+ let mut it = self . byte_iter ( ) . enumerate ( ) ;
194
+ for ( idx, byte) in it {
210
195
let val = byte as u32 ;
211
196
212
- match ch {
197
+ match byte as char {
213
198
'A' ..'Z' => buf |= val - 0x41 ,
214
199
'a' ..'z' => buf |= val - 0x47 ,
215
200
'0' ..'9' => buf |= val + 0x04 ,
216
201
'+' |'-' => buf |= 0x3E ,
217
202
'/' |'_' => buf |= 0x3F ,
218
203
'\r' |'\n' => loop ,
219
204
'=' => break ,
220
- _ => return Err ( ~"Invalid Base64 character")
205
+ _ => return Err ( fmt ! ( "Invalid character '%c' at position %u" ,
206
+ self . char_at( idx) , idx) )
221
207
}
222
208
223
209
buf <<= 6 ;
@@ -230,8 +216,11 @@ impl<'self> FromBase64 for &'self [u8] {
230
216
}
231
217
}
232
218
233
- if !it. all ( |& byte| { byte as char == '=' } ) {
234
- return Err ( ~"Invalid Base64 character") ;
219
+ for ( idx, byte) in it {
220
+ if ( byte as char ) != '=' {
221
+ return Err ( fmt ! ( "Invalid character '%c' at position %u" ,
222
+ self . char_at( idx) , idx) ) ;
223
+ }
235
224
}
236
225
237
226
match modulus {
@@ -250,67 +239,35 @@ impl<'self> FromBase64 for &'self [u8] {
250
239
}
251
240
}
252
241
253
- impl < ' self > FromBase64 for & ' self str {
254
- /**
255
- * Convert any base64 encoded string (literal, `@`, `&`, or `~`)
256
- * to the byte values it encodes.
257
- *
258
- * You can use the `from_bytes` function in `std::str`
259
- * to turn a `[u8]` into a string with characters corresponding to those
260
- * values.
261
- *
262
- * # Example
263
- *
264
- * This converts a string literal to base64 and back.
265
- *
266
- * ~~~ {.rust}
267
- * extern mod extra;
268
- * use extra::base64::{ToBase64, FromBase64, standard};
269
- * use std::str;
270
- *
271
- * fn main () {
272
- * let hello_str = "Hello, World".to_base64(standard);
273
- * printfln!("%s", hello_str);
274
- * let bytes = hello_str.from_base64();
275
- * printfln!("%?", bytes);
276
- * let result_str = str::from_bytes(bytes);
277
- * printfln!("%s", result_str);
278
- * }
279
- * ~~~
280
- */
281
- fn from_base64 ( & self ) -> Result < ~[ u8 ] , ~str > {
282
- self . as_bytes ( ) . from_base64 ( )
283
- }
284
- }
285
-
286
242
#[ cfg( test) ]
287
243
mod test {
288
244
use test:: BenchHarness ;
289
245
use base64:: * ;
290
246
291
247
#[ test]
292
248
fn test_to_base64_basic ( ) {
293
- assert_eq ! ( "" . to_base64( STANDARD ) , ~"") ;
294
- assert_eq ! ( "f" . to_base64( STANDARD ) , ~"Zg ==");
295
- assert_eq!(" fo".to_base64(STANDARD), ~" Zm8 =");
296
- assert_eq!(" foo".to_base64(STANDARD), ~" Zm9v ");
297
- assert_eq!(" foob".to_base64(STANDARD), ~" Zm9vYg ==");
298
- assert_eq!(" fooba".to_base64(STANDARD), ~" Zm9vYmE =");
299
- assert_eq!(" foobar".to_base64(STANDARD), ~" Zm9vYmFy ");
249
+ assert_eq ! ( "" . as_bytes ( ) . to_base64( STANDARD ) , ~"") ;
250
+ assert_eq ! ( "f" . as_bytes ( ) . to_base64( STANDARD ) , ~"Zg ==");
251
+ assert_eq!(" fo".as_bytes(). to_base64(STANDARD), ~" Zm8 =");
252
+ assert_eq!(" foo".as_bytes(). to_base64(STANDARD), ~" Zm9v ");
253
+ assert_eq!(" foob".as_bytes(). to_base64(STANDARD), ~" Zm9vYg ==");
254
+ assert_eq!(" fooba".as_bytes(). to_base64(STANDARD), ~" Zm9vYmE =");
255
+ assert_eq!(" foobar".as_bytes(). to_base64(STANDARD), ~" Zm9vYmFy ");
300
256
}
301
257
302
258
#[test]
303
259
fn test_to_base64_line_break() {
304
260
assert!(![0u8, 1000].to_base64(Config {line_length: None, ..STANDARD})
305
261
.contains("\r \n " ) ) ;
306
- assert_eq ! ( "foobar" . to_base64( Config { line_length: Some ( 4 ) , ..STANDARD } ) ,
262
+ assert_eq ! ( "foobar" . as_bytes( ) . to_base64( Config { line_length: Some ( 4 ) ,
263
+ ..STANDARD } ) ,
307
264
~"Zm9v \r \n YmFy ");
308
265
}
309
266
310
267
#[test]
311
268
fn test_to_base64_padding() {
312
- assert_eq!(" f".to_base64(Config {pad: false, ..STANDARD}), ~" Zg ");
313
- assert_eq!(" fo".to_base64(Config {pad: false, ..STANDARD}), ~" Zm8 ");
269
+ assert_eq!(" f".as_bytes(). to_base64(Config {pad: false, ..STANDARD}), ~" Zg ");
270
+ assert_eq!(" fo".as_bytes(). to_base64(Config {pad: false, ..STANDARD}), ~" Zm8 ");
314
271
}
315
272
316
273
#[test]
@@ -344,7 +301,7 @@ mod test {
344
301
#[test]
345
302
fn test_from_base64_invalid_char() {
346
303
assert!(" Zm $=".from_base64().is_err())
347
- assert!(" Zg ==$".from_base64().is_err());
304
+ assert!(" Zg ==$".from_base64().is_err());
348
305
}
349
306
350
307
#[test]
@@ -368,20 +325,20 @@ mod test {
368
325
}
369
326
370
327
#[bench]
371
- pub fn to_base64 (bh: & mut BenchHarness) {
328
+ pub fn bench_to_base64 (bh: & mut BenchHarness) {
372
329
let s = " イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
373
330
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
374
331
do bh.iter {
375
- s.to_base64(STANDARD);
332
+ s.as_bytes(). to_base64(STANDARD);
376
333
}
377
334
bh.bytes = s.len() as u64;
378
335
}
379
336
380
337
#[bench]
381
- pub fn from_base64 (bh: & mut BenchHarness) {
338
+ pub fn bench_from_base64 (bh: & mut BenchHarness) {
382
339
let s = " イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
383
340
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン" ;
384
- let b = s. to_base64( STANDARD ) ;
341
+ let b = s. as_bytes ( ) . to_base64( STANDARD ) ;
385
342
do bh. iter {
386
343
b. from_base64( ) ;
387
344
}
0 commit comments