Skip to content

Commit 7434186

Browse files
Add STANDARD, URL_SAFE, URL_SAFE_NO_PAD consts
1 parent d00b4db commit 7434186

File tree

17 files changed

+96
-114
lines changed

17 files changed

+96
-114
lines changed

README.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ e.g. `decode_engine_slice` decodes into an existing `&mut [u8]` and is pretty fa
1414
whereas `decode_engine` allocates a new `Vec<u8>` and returns it, which might be more convenient in some cases, but is
1515
slower (although still fast enough for almost any purpose) at 2.1 GiB/s.
1616

17-
## Example
18-
19-
```rust
20-
use base64::{encode, decode};
21-
22-
fn main() {
23-
let a = b"hello world";
24-
let b = "aGVsbG8gd29ybGQ=";
25-
26-
assert_eq!(encode(a), b);
27-
assert_eq!(a, &decode(b).unwrap()[..]);
28-
}
29-
```
30-
3117
See the [docs](https://docs.rs/base64) for all the details.
3218

3319
## FAQ

RELEASE-NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
- `GeneralPurpose` and its config are now `pub use`'d in the `engine` module for convenience.
88
- Change a few `from()` functions to be `new()`. `from()` causes confusing compiler errors because of confusion with `From::from`, and is a little misleading because some of those invocations are not very cheap as one would usually expect from a `from` call.
99
- `encode*` and `decode*` top level functions are now methods on `Engine`.
10+
- `DEFAULT_ENGINE` was replaced by `engine::general_purpose::STANDARD`
11+
- Predefined engine consts `engine::general_purpose::{STANDARD, URL_SAFE, URL_SAFE_NO_PAD}`
12+
- These are `pub use`d into `engine` as well
1013

1114
# 0.20.0
1215

benches/benchmarks.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate criterion;
33

44
use base64::{
55
display,
6-
engine::{Engine, DEFAULT_ENGINE},
6+
engine::{Engine, STANDARD},
77
write,
88
};
99
use criterion::{black_box, Bencher, BenchmarkId, Criterion, Throughput};
@@ -13,22 +13,22 @@ use std::io::{self, Read, Write};
1313
fn do_decode_bench(b: &mut Bencher, &size: &usize) {
1414
let mut v: Vec<u8> = Vec::with_capacity(size * 3 / 4);
1515
fill(&mut v);
16-
let encoded = DEFAULT_ENGINE.encode(&v);
16+
let encoded = STANDARD.encode(&v);
1717

1818
b.iter(|| {
19-
let orig = DEFAULT_ENGINE.decode(&encoded);
19+
let orig = STANDARD.decode(&encoded);
2020
black_box(&orig);
2121
});
2222
}
2323

2424
fn do_decode_bench_reuse_buf(b: &mut Bencher, &size: &usize) {
2525
let mut v: Vec<u8> = Vec::with_capacity(size * 3 / 4);
2626
fill(&mut v);
27-
let encoded = DEFAULT_ENGINE.encode(&v);
27+
let encoded = STANDARD.encode(&v);
2828

2929
let mut buf = Vec::new();
3030
b.iter(|| {
31-
DEFAULT_ENGINE.decode_vec(&encoded, &mut buf).unwrap();
31+
STANDARD.decode_vec(&encoded, &mut buf).unwrap();
3232
black_box(&buf);
3333
buf.clear();
3434
});
@@ -37,28 +37,28 @@ fn do_decode_bench_reuse_buf(b: &mut Bencher, &size: &usize) {
3737
fn do_decode_bench_slice(b: &mut Bencher, &size: &usize) {
3838
let mut v: Vec<u8> = Vec::with_capacity(size * 3 / 4);
3939
fill(&mut v);
40-
let encoded = DEFAULT_ENGINE.encode(&v);
40+
let encoded = STANDARD.encode(&v);
4141

4242
let mut buf = Vec::new();
4343
buf.resize(size, 0);
4444
b.iter(|| {
45-
DEFAULT_ENGINE.decode_slice(&encoded, &mut buf).unwrap();
45+
STANDARD.decode_slice(&encoded, &mut buf).unwrap();
4646
black_box(&buf);
4747
});
4848
}
4949

5050
fn do_decode_bench_stream(b: &mut Bencher, &size: &usize) {
5151
let mut v: Vec<u8> = Vec::with_capacity(size * 3 / 4);
5252
fill(&mut v);
53-
let encoded = DEFAULT_ENGINE.encode(&v);
53+
let encoded = STANDARD.encode(&v);
5454

5555
let mut buf = Vec::new();
5656
buf.resize(size, 0);
5757
buf.truncate(0);
5858

5959
b.iter(|| {
6060
let mut cursor = io::Cursor::new(&encoded[..]);
61-
let mut decoder = base64::read::DecoderReader::new(&mut cursor, &DEFAULT_ENGINE);
61+
let mut decoder = base64::read::DecoderReader::new(&mut cursor, &STANDARD);
6262
decoder.read_to_end(&mut buf).unwrap();
6363
buf.clear();
6464
black_box(&buf);
@@ -69,7 +69,7 @@ fn do_encode_bench(b: &mut Bencher, &size: &usize) {
6969
let mut v: Vec<u8> = Vec::with_capacity(size);
7070
fill(&mut v);
7171
b.iter(|| {
72-
let e = DEFAULT_ENGINE.encode(&v);
72+
let e = STANDARD.encode(&v);
7373
black_box(&e);
7474
});
7575
}
@@ -78,7 +78,7 @@ fn do_encode_bench_display(b: &mut Bencher, &size: &usize) {
7878
let mut v: Vec<u8> = Vec::with_capacity(size);
7979
fill(&mut v);
8080
b.iter(|| {
81-
let e = format!("{}", display::Base64Display::new(&v, &DEFAULT_ENGINE));
81+
let e = format!("{}", display::Base64Display::new(&v, &STANDARD));
8282
black_box(&e);
8383
});
8484
}
@@ -88,7 +88,7 @@ fn do_encode_bench_reuse_buf(b: &mut Bencher, &size: &usize) {
8888
fill(&mut v);
8989
let mut buf = String::new();
9090
b.iter(|| {
91-
DEFAULT_ENGINE.encode_string(&v, &mut buf);
91+
STANDARD.encode_string(&v, &mut buf);
9292
buf.clear();
9393
});
9494
}
@@ -100,7 +100,7 @@ fn do_encode_bench_slice(b: &mut Bencher, &size: &usize) {
100100
// conservative estimate of encoded size
101101
buf.resize(v.len() * 2, 0);
102102
b.iter(|| {
103-
DEFAULT_ENGINE.encode_slice(&v, &mut buf);
103+
STANDARD.encode_slice(&v, &mut buf);
104104
});
105105
}
106106

@@ -112,7 +112,7 @@ fn do_encode_bench_stream(b: &mut Bencher, &size: &usize) {
112112
buf.reserve(size * 2);
113113
b.iter(|| {
114114
buf.clear();
115-
let mut stream_enc = write::EncoderWriter::new(&mut buf, &DEFAULT_ENGINE);
115+
let mut stream_enc = write::EncoderWriter::new(&mut buf, &STANDARD);
116116
stream_enc.write_all(&v).unwrap();
117117
stream_enc.flush().unwrap();
118118
});
@@ -123,7 +123,7 @@ fn do_encode_bench_string_stream(b: &mut Bencher, &size: &usize) {
123123
fill(&mut v);
124124

125125
b.iter(|| {
126-
let mut stream_enc = write::EncoderStringWriter::new(&DEFAULT_ENGINE);
126+
let mut stream_enc = write::EncoderStringWriter::new(&STANDARD);
127127
stream_enc.write_all(&v).unwrap();
128128
stream_enc.flush().unwrap();
129129
let _ = stream_enc.into_inner();
@@ -137,7 +137,7 @@ fn do_encode_bench_string_reuse_buf_stream(b: &mut Bencher, &size: &usize) {
137137
let mut buf = String::new();
138138
b.iter(|| {
139139
buf.clear();
140-
let mut stream_enc = write::EncoderStringWriter::from_consumer(&mut buf, &DEFAULT_ENGINE);
140+
let mut stream_enc = write::EncoderStringWriter::from_consumer(&mut buf, &STANDARD);
141141
stream_enc.write_all(&v).unwrap();
142142
stream_enc.flush().unwrap();
143143
let _ = stream_enc.into_inner();

fuzz/fuzzers/roundtrip.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#[macro_use] extern crate libfuzzer_sys;
33
extern crate base64;
44

5-
use base64::{Engine as _, engine::DEFAULT_ENGINE};
5+
use base64::{Engine as _, engine::STANDARD};
66

77
fuzz_target!(|data: &[u8]| {
8-
let encoded = DEFAULT_ENGINE.encode(data);
9-
let decoded = DEFAULT_ENGINE.decode(&encoded).unwrap();
8+
let encoded = STANDARD.encode(data);
9+
let decoded = STANDARD.decode(&encoded).unwrap();
1010
assert_eq!(data, decoded.as_slice());
1111
});

src/decode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::engine::Engine;
22
#[cfg(any(feature = "alloc", feature = "std", test))]
3-
use crate::engine::DEFAULT_ENGINE;
3+
use crate::engine::STANDARD;
44
#[cfg(any(feature = "alloc", feature = "std", test))]
55
use alloc::vec::Vec;
66
use core::fmt;
@@ -58,16 +58,16 @@ impl error::Error for DecodeError {
5858
}
5959
}
6060

61-
///Decode base64 using the [default engine](DEFAULT_ENGINE).
61+
/// Decode base64 using the [`STANDARD` engine](STANDARD).
6262
///
6363
/// See [Engine::decode].
6464
#[deprecated(since = "0.21.0", note = "Use Engine::decode")]
6565
#[cfg(any(feature = "alloc", feature = "std", test))]
6666
pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
67-
DEFAULT_ENGINE.decode(input)
67+
STANDARD.decode(input)
6868
}
6969

70-
///Decode from string reference as octets using the specified [Engine].
70+
/// Decode from string reference as octets using the specified [Engine].
7171
///
7272
/// See [Engine::decode].
7373
///Returns a `Result` containing a `Vec<u8>`.

src/display.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//!
33
//! ```
44
//! use base64::display::Base64Display;
5-
//! use base64::engine::DEFAULT_ENGINE;
5+
//! use base64::engine::STANDARD;
66
//!
77
//! let data = vec![0x0, 0x1, 0x2, 0x3];
8-
//! let wrapper = Base64Display::new(&data, &DEFAULT_ENGINE);
8+
//! let wrapper = Base64Display::new(&data, &STANDARD);
99
//!
1010
//! assert_eq!("base64: AAECAw==", format!("base64: {}", wrapper));
1111
//! ```
@@ -59,17 +59,17 @@ mod tests {
5959
chunked_encode_matches_normal_encode_random, SinkTestHelper,
6060
};
6161
use super::*;
62-
use crate::engine::DEFAULT_ENGINE;
62+
use crate::engine::STANDARD;
6363

6464
#[test]
6565
fn basic_display() {
6666
assert_eq!(
6767
"~$Zm9vYmFy#*",
68-
format!("~${}#*", Base64Display::new(b"foobar", &DEFAULT_ENGINE))
68+
format!("~${}#*", Base64Display::new(b"foobar", &STANDARD))
6969
);
7070
assert_eq!(
7171
"~$Zm9vYmFyZg==#*",
72-
format!("~${}#*", Base64Display::new(b"foobarf", &DEFAULT_ENGINE))
72+
format!("~${}#*", Base64Display::new(b"foobarf", &STANDARD))
7373
);
7474
}
7575

src/encode.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
use alloc::string::String;
33

44
#[cfg(any(feature = "alloc", feature = "std", test))]
5-
use crate::engine::DEFAULT_ENGINE;
5+
use crate::engine::STANDARD;
66
use crate::engine::{Config, Engine};
77
use crate::PAD_BYTE;
88

9-
/// Encode arbitrary octets as base64 using the [default engine](DEFAULT_ENGINE).
9+
/// Encode arbitrary octets as base64 using the [`STANDARD` engine](STANDARD).
1010
///
1111
/// See [Engine::encode].
1212
#[allow(unused)]
1313
#[deprecated(since = "0.21.0", note = "Use Engine::encode")]
1414
#[cfg(any(feature = "alloc", feature = "std", test))]
1515
pub fn encode<T: AsRef<[u8]>>(input: T) -> String {
16-
DEFAULT_ENGINE.encode(input)
16+
STANDARD.encode(input)
1717
}
1818

1919
///Encode arbitrary octets as base64 using the provided `Engine` into a new `String`.
@@ -133,10 +133,10 @@ mod tests {
133133
use super::*;
134134

135135
use crate::{
136-
alphabet::{IMAP_MUTF7, STANDARD, URL_SAFE},
136+
alphabet,
137137
engine::{
138138
general_purpose::{GeneralPurpose, NO_PAD},
139-
DEFAULT_ENGINE,
139+
STANDARD,
140140
},
141141
tests::{assert_encode_sanity, random_config, random_engine},
142142
};
@@ -146,31 +146,31 @@ mod tests {
146146
};
147147
use std::str;
148148

149-
const URL_SAFE_NO_PAD_ENGINE: GeneralPurpose = GeneralPurpose::new(&URL_SAFE, NO_PAD);
149+
const URL_SAFE_NO_PAD_ENGINE: GeneralPurpose = GeneralPurpose::new(&alphabet::URL_SAFE, NO_PAD);
150150

151151
#[test]
152152
fn encoded_size_correct_standard() {
153-
assert_encoded_length(0, 0, &DEFAULT_ENGINE, true);
153+
assert_encoded_length(0, 0, &STANDARD, true);
154154

155-
assert_encoded_length(1, 4, &DEFAULT_ENGINE, true);
156-
assert_encoded_length(2, 4, &DEFAULT_ENGINE, true);
157-
assert_encoded_length(3, 4, &DEFAULT_ENGINE, true);
155+
assert_encoded_length(1, 4, &STANDARD, true);
156+
assert_encoded_length(2, 4, &STANDARD, true);
157+
assert_encoded_length(3, 4, &STANDARD, true);
158158

159-
assert_encoded_length(4, 8, &DEFAULT_ENGINE, true);
160-
assert_encoded_length(5, 8, &DEFAULT_ENGINE, true);
161-
assert_encoded_length(6, 8, &DEFAULT_ENGINE, true);
159+
assert_encoded_length(4, 8, &STANDARD, true);
160+
assert_encoded_length(5, 8, &STANDARD, true);
161+
assert_encoded_length(6, 8, &STANDARD, true);
162162

163-
assert_encoded_length(7, 12, &DEFAULT_ENGINE, true);
164-
assert_encoded_length(8, 12, &DEFAULT_ENGINE, true);
165-
assert_encoded_length(9, 12, &DEFAULT_ENGINE, true);
163+
assert_encoded_length(7, 12, &STANDARD, true);
164+
assert_encoded_length(8, 12, &STANDARD, true);
165+
assert_encoded_length(9, 12, &STANDARD, true);
166166

167-
assert_encoded_length(54, 72, &DEFAULT_ENGINE, true);
167+
assert_encoded_length(54, 72, &STANDARD, true);
168168

169-
assert_encoded_length(55, 76, &DEFAULT_ENGINE, true);
170-
assert_encoded_length(56, 76, &DEFAULT_ENGINE, true);
171-
assert_encoded_length(57, 76, &DEFAULT_ENGINE, true);
169+
assert_encoded_length(55, 76, &STANDARD, true);
170+
assert_encoded_length(56, 76, &STANDARD, true);
171+
assert_encoded_length(57, 76, &STANDARD, true);
172172

173-
assert_encoded_length(58, 80, &DEFAULT_ENGINE, true);
173+
assert_encoded_length(58, 80, &STANDARD, true);
174174
}
175175

176176
#[test]
@@ -500,8 +500,8 @@ mod tests {
500500
#[test]
501501
fn encode_imap() {
502502
assert_eq!(
503-
&GeneralPurpose::new(&IMAP_MUTF7, NO_PAD).encode(b"\xFB\xFF"),
504-
&GeneralPurpose::new(&STANDARD, NO_PAD)
503+
&GeneralPurpose::new(&alphabet::IMAP_MUTF7, NO_PAD).encode(b"\xFB\xFF"),
504+
&GeneralPurpose::new(&alphabet::STANDARD, NO_PAD)
505505
.encode(b"\xFB\xFF")
506506
.replace('/', ",")
507507
);

src/engine/general_purpose/decode.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,14 @@ fn write_u64(output: &mut [u8], value: u64) {
327327
mod tests {
328328
use super::*;
329329

330-
use crate::engine::DEFAULT_ENGINE;
330+
use crate::engine::STANDARD;
331331

332332
#[test]
333333
fn decode_chunk_precise_writes_only_6_bytes() {
334334
let input = b"Zm9vYmFy"; // "foobar"
335335
let mut output = [0_u8, 1, 2, 3, 4, 5, 6, 7];
336336

337-
decode_chunk_precise(&input[..], 0, &DEFAULT_ENGINE.decode_table, &mut output).unwrap();
337+
decode_chunk_precise(&input[..], 0, &STANDARD.decode_table, &mut output).unwrap();
338338
assert_eq!(&vec![b'f', b'o', b'o', b'b', b'a', b'r', 6, 7], &output);
339339
}
340340

@@ -343,7 +343,7 @@ mod tests {
343343
let input = b"Zm9vYmFy"; // "foobar"
344344
let mut output = [0_u8, 1, 2, 3, 4, 5, 6, 7];
345345

346-
decode_chunk(&input[..], 0, &DEFAULT_ENGINE.decode_table, &mut output).unwrap();
346+
decode_chunk(&input[..], 0, &STANDARD.decode_table, &mut output).unwrap();
347347
assert_eq!(&vec![b'f', b'o', b'o', b'b', b'a', b'r', 0, 0], &output);
348348
}
349349
}

src/engine/general_purpose/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Provides the [GeneralPurpose] engine and associated config types.
22
use crate::{
3+
alphabet,
34
alphabet::Alphabet,
45
engine::{Config, DecodePaddingMode},
56
DecodeError,
@@ -324,6 +325,15 @@ impl Config for GeneralPurposeConfig {
324325
}
325326
}
326327

328+
/// A [GeneralPurpose] engine using the [crate::alphabet::STANDARD] base64 alphabet and [crate::engine::general_purpose::PAD] config.
329+
pub const STANDARD: GeneralPurpose = GeneralPurpose::new(&alphabet::STANDARD, PAD);
330+
331+
/// A [GeneralPurpose] engine using the [crate::alphabet::URL_SAFE] base64 alphabet and [crate::engine::general_purpose::PAD] config.
332+
pub const URL_SAFE: GeneralPurpose = GeneralPurpose::new(&alphabet::URL_SAFE, PAD);
333+
334+
/// A [GeneralPurpose] engine using the [crate::alphabet::URL_SAFE] base64 alphabet and [crate::engine::general_purpose::NO_PAD] config.
335+
pub const URL_SAFE_NO_PAD: GeneralPurpose = GeneralPurpose::new(&alphabet::URL_SAFE, NO_PAD);
336+
327337
/// Include padding bytes when encoding, and require that they be present when decoding.
328338
///
329339
/// This is the standard per the base64 RFC, but consider using [NO_PAD] instead as padding serves

0 commit comments

Comments
 (0)