Skip to content

Commit a943a7a

Browse files
committed
serialize: base64: improve newline handling speed
1 parent 553ab27 commit a943a7a

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/libserialize/base64.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
1515
pub use self::FromBase64Error::*;
1616
pub use self::CharacterSet::*;
17-
pub use self::Newline::*;
1817

1918
use std::fmt;
2019
use std::error;
@@ -55,15 +54,15 @@ impl Copy for Config {}
5554

5655
/// Configuration for RFC 4648 standard base64 encoding
5756
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};
5958

6059
/// Configuration for RFC 4648 base64url encoding
6160
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};
6362

6463
/// Configuration for RFC 2045 MIME base64 encoding
6564
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)};
6766

6867
static STANDARD_CHARS: &'static[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
6968
abcdefghijklmnopqrstuvwxyz\
@@ -108,14 +107,15 @@ impl ToBase64 for [u8] {
108107
let len = self.len();
109108
let mod_len = len % 3;
110109
let cond_len = len - mod_len;
110+
let newline = match config.newline {
111+
Newline::LF => b"\n",
112+
Newline::CRLF => b"\r\n"
113+
};
111114
while i < cond_len {
112115
let (first, second, third) = (self[i], self[i + 1], self[i + 2]);
113116
if let Some(line_length) = config.line_length {
114117
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);
119119
cur_length = 0;
120120
}
121121
}
@@ -137,10 +137,7 @@ impl ToBase64 for [u8] {
137137
if mod_len != 0 {
138138
if let Some(line_length) = config.line_length {
139139
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);
144141
}
145142
}
146143
}
@@ -306,7 +303,7 @@ impl FromBase64 for [u8] {
306303
mod tests {
307304
extern crate test;
308305
use self::test::Bencher;
309-
use base64::{Config, FromBase64, ToBase64, STANDARD, URL_SAFE, LF};
306+
use base64::{Config, Newline, FromBase64, ToBase64, STANDARD, URL_SAFE};
310307

311308
#[test]
312309
fn test_to_base64_basic() {
@@ -330,12 +327,13 @@ mod tests {
330327

331328
#[test]
332329
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,
334332
..STANDARD})
335333
.as_slice()
336334
.contains("\n"));
337335
assert_eq!("foobar".as_bytes().to_base64(Config {line_length: Some(4),
338-
newline: LF,
336+
newline: Newline::LF,
339337
..STANDARD}),
340338
"Zm9v\nYmFy".to_string());
341339
}

0 commit comments

Comments
 (0)