14
14
15
15
pub use self :: FromBase64Error :: * ;
16
16
pub use self :: CharacterSet :: * ;
17
- pub use self :: Newline :: * ;
18
17
19
18
use std:: fmt;
20
19
use std:: error;
@@ -55,15 +54,15 @@ impl Copy for Config {}
55
54
56
55
/// Configuration for RFC 4648 standard base64 encoding
57
56
pub static STANDARD : Config =
58
- Config { char_set : Standard , newline : CRLF , pad : true , line_length : None } ;
57
+ Config { char_set : Standard , newline : Newline :: CRLF , pad : true , line_length : None } ;
59
58
60
59
/// Configuration for RFC 4648 base64url encoding
61
60
pub static URL_SAFE : Config =
62
- Config { char_set : UrlSafe , newline : CRLF , pad : false , line_length : None } ;
61
+ Config { char_set : UrlSafe , newline : Newline :: CRLF , pad : false , line_length : None } ;
63
62
64
63
/// Configuration for RFC 2045 MIME base64 encoding
65
64
pub static MIME : Config =
66
- Config { char_set : Standard , newline : CRLF , pad : true , line_length : Some ( 76 ) } ;
65
+ Config { char_set : Standard , newline : Newline :: CRLF , pad : true , line_length : Some ( 76 ) } ;
67
66
68
67
static STANDARD_CHARS : & ' static [ u8 ] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
69
68
abcdefghijklmnopqrstuvwxyz\
@@ -108,14 +107,15 @@ impl ToBase64 for [u8] {
108
107
let len = self . len ( ) ;
109
108
let mod_len = len % 3 ;
110
109
let cond_len = len - mod_len;
110
+ let newline = match config. newline {
111
+ Newline :: LF => b"\n " ,
112
+ Newline :: CRLF => b"\r \n "
113
+ } ;
111
114
while i < cond_len {
112
115
let ( first, second, third) = ( self [ i] , self [ i + 1 ] , self [ i + 2 ] ) ;
113
116
if let Some ( line_length) = config. line_length {
114
117
if cur_length >= line_length {
115
- v. push_all ( match config. newline {
116
- LF => b"\n " ,
117
- CRLF => b"\r \n "
118
- } ) ;
118
+ v. push_all ( newline) ;
119
119
cur_length = 0 ;
120
120
}
121
121
}
@@ -137,10 +137,7 @@ impl ToBase64 for [u8] {
137
137
if mod_len != 0 {
138
138
if let Some ( line_length) = config. line_length {
139
139
if cur_length >= line_length {
140
- v. push_all ( match config. newline {
141
- LF => b"\n " ,
142
- CRLF => b"\r \n "
143
- } ) ;
140
+ v. push_all ( newline) ;
144
141
}
145
142
}
146
143
}
@@ -306,7 +303,7 @@ impl FromBase64 for [u8] {
306
303
mod tests {
307
304
extern crate test;
308
305
use self :: test:: Bencher ;
309
- use base64:: { Config , FromBase64 , ToBase64 , STANDARD , URL_SAFE , LF } ;
306
+ use base64:: { Config , Newline , FromBase64 , ToBase64 , STANDARD , URL_SAFE } ;
310
307
311
308
#[ test]
312
309
fn test_to_base64_basic ( ) {
@@ -330,12 +327,13 @@ mod tests {
330
327
331
328
#[ test]
332
329
fn test_to_base64_lf_line_break ( ) {
333
- assert ! ( ![ 0u8 , ..1000 ] . to_base64( Config { line_length: None , newline: LF ,
330
+ assert ! ( ![ 0u8 , ..1000 ] . to_base64( Config { line_length: None ,
331
+ newline: Newline :: LF ,
334
332
..STANDARD } )
335
333
. as_slice( )
336
334
. contains( "\n " ) ) ;
337
335
assert_eq ! ( "foobar" . as_bytes( ) . to_base64( Config { line_length: Some ( 4 ) ,
338
- newline: LF ,
336
+ newline: Newline :: LF ,
339
337
..STANDARD } ) ,
340
338
"Zm9v\n YmFy" . to_string( ) ) ;
341
339
}
0 commit comments